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 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win. In that case the |
| 5 | module actually calls itself 'nt', not 'posix', and a few |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 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 |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11 | #ifdef __APPLE__ |
| 12 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13 | * 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] | 14 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 15 | * at the end of this file for more information. |
| 16 | */ |
| 17 | # pragma weak lchown |
| 18 | # pragma weak statvfs |
| 19 | # pragma weak fstatvfs |
| 20 | |
| 21 | #endif /* __APPLE__ */ |
| 22 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 23 | #define PY_SSIZE_T_CLEAN |
| 24 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 25 | #include "Python.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 26 | #ifndef MS_WINDOWS |
| 27 | #include "posixmodule.h" |
| 28 | #endif |
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 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 46 | #ifdef HAVE_SYS_UIO_H |
| 47 | #include <sys/uio.h> |
| 48 | #endif |
| 49 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 50 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 52 | #endif /* HAVE_SYS_TYPES_H */ |
| 53 | |
| 54 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 56 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 58 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 59 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 60 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 62 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 63 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 66 | #ifdef HAVE_FCNTL_H |
| 67 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 68 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | #ifdef HAVE_GRP_H |
| 71 | #include <grp.h> |
| 72 | #endif |
| 73 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYSEXITS_H |
| 75 | #include <sysexits.h> |
| 76 | #endif /* HAVE_SYSEXITS_H */ |
| 77 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SYS_LOADAVG_H |
| 79 | #include <sys/loadavg.h> |
| 80 | #endif |
| 81 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 82 | #ifdef HAVE_LANGINFO_H |
| 83 | #include <langinfo.h> |
| 84 | #endif |
| 85 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 86 | #ifdef HAVE_SYS_SENDFILE_H |
| 87 | #include <sys/sendfile.h> |
| 88 | #endif |
| 89 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 90 | #ifdef HAVE_SCHED_H |
| 91 | #include <sched.h> |
| 92 | #endif |
| 93 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 94 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 95 | #undef HAVE_SCHED_SETAFFINITY |
| 96 | #endif |
| 97 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 98 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) |
| 99 | #define USE_XATTRS |
| 100 | #endif |
| 101 | |
| 102 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 103 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 104 | #endif |
| 105 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 106 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 107 | #ifdef HAVE_SYS_SOCKET_H |
| 108 | #include <sys/socket.h> |
| 109 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 112 | #ifdef HAVE_DLFCN_H |
| 113 | #include <dlfcn.h> |
| 114 | #endif |
| 115 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 116 | #ifdef __hpux |
| 117 | #include <sys/mpctl.h> |
| 118 | #endif |
| 119 | |
| 120 | #if defined(__DragonFly__) || \ |
| 121 | defined(__OpenBSD__) || \ |
| 122 | defined(__FreeBSD__) || \ |
| 123 | defined(__NetBSD__) || \ |
| 124 | defined(__APPLE__) |
| 125 | #include <sys/sysctl.h> |
| 126 | #endif |
| 127 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 128 | #if defined(MS_WINDOWS) |
| 129 | # define TERMSIZE_USE_CONIO |
| 130 | #elif defined(HAVE_SYS_IOCTL_H) |
| 131 | # include <sys/ioctl.h> |
| 132 | # if defined(HAVE_TERMIOS_H) |
| 133 | # include <termios.h> |
| 134 | # endif |
| 135 | # if defined(TIOCGWINSZ) |
| 136 | # define TERMSIZE_USE_IOCTL |
| 137 | # endif |
| 138 | #endif /* MS_WINDOWS */ |
| 139 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 141 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 142 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 143 | #define HAVE_GETCWD 1 |
| 144 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 145 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #include <process.h> |
| 147 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 148 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 149 | #define HAVE_EXECV 1 |
| 150 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 151 | #define HAVE_OPENDIR 1 |
| 152 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 153 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #define HAVE_WAIT 1 |
| 155 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 156 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 157 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 158 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 159 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 160 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 161 | #define HAVE_EXECV 1 |
| 162 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 163 | #define HAVE_SYSTEM 1 |
| 164 | #define HAVE_CWAIT 1 |
| 165 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 166 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 167 | #else |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 168 | #if defined(__VMS) |
| 169 | /* Everything needed is defined in vms/pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 170 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 171 | /* Unix functions that the configure script doesn't check for */ |
| 172 | #define HAVE_EXECV 1 |
| 173 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 174 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 175 | #define HAVE_FORK1 1 |
| 176 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 177 | #define HAVE_GETCWD 1 |
| 178 | #define HAVE_GETEGID 1 |
| 179 | #define HAVE_GETEUID 1 |
| 180 | #define HAVE_GETGID 1 |
| 181 | #define HAVE_GETPPID 1 |
| 182 | #define HAVE_GETUID 1 |
| 183 | #define HAVE_KILL 1 |
| 184 | #define HAVE_OPENDIR 1 |
| 185 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 186 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 187 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 188 | #define HAVE_TTYNAME 1 |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 189 | #endif /* __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 190 | #endif /* _MSC_VER */ |
| 191 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 192 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 193 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 194 | |
| 195 | |
| 196 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 197 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 198 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 199 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 200 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 201 | (default) */ |
| 202 | extern char *ctermid_r(char *); |
| 203 | #endif |
| 204 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 205 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 206 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 207 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 208 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 209 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 210 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 211 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 212 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 213 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 214 | #endif |
| 215 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 216 | extern int chdir(char *); |
| 217 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 218 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 219 | extern int chdir(const char *); |
| 220 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 221 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 222 | #ifdef __BORLANDC__ |
| 223 | extern int chmod(const char *, int); |
| 224 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 225 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 226 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 227 | /*#ifdef HAVE_FCHMOD |
| 228 | extern int fchmod(int, mode_t); |
| 229 | #endif*/ |
| 230 | /*#ifdef HAVE_LCHMOD |
| 231 | extern int lchmod(const char *, mode_t); |
| 232 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 233 | extern int chown(const char *, uid_t, gid_t); |
| 234 | extern char *getcwd(char *, int); |
| 235 | extern char *strerror(int); |
| 236 | extern int link(const char *, const char *); |
| 237 | extern int rename(const char *, const char *); |
| 238 | extern int stat(const char *, struct stat *); |
| 239 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 240 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 241 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 242 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 244 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 245 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 246 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 248 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | #ifdef HAVE_UTIME_H |
| 251 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 252 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 254 | #ifdef HAVE_SYS_UTIME_H |
| 255 | #include <sys/utime.h> |
| 256 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 257 | #endif /* HAVE_SYS_UTIME_H */ |
| 258 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | #ifdef HAVE_SYS_TIMES_H |
| 260 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 261 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | |
| 263 | #ifdef HAVE_SYS_PARAM_H |
| 264 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 265 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | |
| 267 | #ifdef HAVE_SYS_UTSNAME_H |
| 268 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 269 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 271 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 273 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 274 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 275 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 276 | #include <direct.h> |
| 277 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 278 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 280 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 281 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 282 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 283 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 284 | #endif |
| 285 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 287 | #endif |
| 288 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 289 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 290 | #endif |
| 291 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 293 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 294 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 295 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 296 | #endif |
| 297 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 298 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 299 | #endif |
| 300 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 301 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 302 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 303 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 304 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 305 | #endif |
| 306 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 307 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 308 | #endif |
| 309 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 310 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 311 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 312 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 313 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 314 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 315 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 316 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 317 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 318 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 319 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 320 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 321 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 322 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 323 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 324 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 325 | #define MAXPATHLEN PATH_MAX |
| 326 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 327 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 328 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 329 | #endif /* MAXPATHLEN */ |
| 330 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 331 | #ifdef UNION_WAIT |
| 332 | /* Emulate some macros on systems that have a union instead of macros */ |
| 333 | |
| 334 | #ifndef WIFEXITED |
| 335 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 336 | #endif |
| 337 | |
| 338 | #ifndef WEXITSTATUS |
| 339 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 340 | #endif |
| 341 | |
| 342 | #ifndef WTERMSIG |
| 343 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 344 | #endif |
| 345 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | #define WAIT_TYPE union wait |
| 347 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 348 | |
| 349 | #else /* !UNION_WAIT */ |
| 350 | #define WAIT_TYPE int |
| 351 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 352 | #endif /* UNION_WAIT */ |
| 353 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 354 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 355 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 356 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 357 | #define USE_CTERMID_R |
| 358 | #endif |
| 359 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 360 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 361 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 362 | #undef FSTAT |
| 363 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 364 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 365 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 366 | # define LSTAT win32_lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 367 | # define FSTAT win32_fstat |
| 368 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 369 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 370 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 371 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 372 | # define FSTAT fstat |
| 373 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 374 | #endif |
| 375 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 376 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 377 | #include <sys/mkdev.h> |
| 378 | #else |
| 379 | #if defined(MAJOR_IN_SYSMACROS) |
| 380 | #include <sys/sysmacros.h> |
| 381 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 382 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 383 | #include <sys/mkdev.h> |
| 384 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 385 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 386 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 387 | |
| 388 | #ifdef MS_WINDOWS |
| 389 | static int |
| 390 | win32_warn_bytes_api() |
| 391 | { |
| 392 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 393 | "The Windows bytes API has been deprecated, " |
| 394 | "use Unicode filenames instead", |
| 395 | 1); |
| 396 | } |
| 397 | #endif |
| 398 | |
| 399 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 400 | #ifndef MS_WINDOWS |
| 401 | PyObject * |
| 402 | _PyLong_FromUid(uid_t uid) |
| 403 | { |
| 404 | if (uid == (uid_t)-1) |
| 405 | return PyLong_FromLong(-1); |
| 406 | return PyLong_FromUnsignedLong(uid); |
| 407 | } |
| 408 | |
| 409 | PyObject * |
| 410 | _PyLong_FromGid(gid_t gid) |
| 411 | { |
| 412 | if (gid == (gid_t)-1) |
| 413 | return PyLong_FromLong(-1); |
| 414 | return PyLong_FromUnsignedLong(gid); |
| 415 | } |
| 416 | |
| 417 | int |
| 418 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 419 | { |
| 420 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 421 | long result; |
| 422 | if (PyFloat_Check(obj)) { |
| 423 | PyErr_SetString(PyExc_TypeError, |
| 424 | "integer argument expected, got float"); |
| 425 | return 0; |
| 426 | } |
| 427 | result = PyLong_AsLongAndOverflow(obj, &overflow); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 428 | if (overflow < 0) |
| 429 | goto OverflowDown; |
| 430 | if (!overflow && result == -1) { |
| 431 | /* error or -1 */ |
| 432 | if (PyErr_Occurred()) |
| 433 | return 0; |
| 434 | *(uid_t *)p = (uid_t)-1; |
| 435 | } |
| 436 | else { |
| 437 | /* unsigned uid_t */ |
| 438 | unsigned long uresult; |
| 439 | if (overflow > 0) { |
| 440 | uresult = PyLong_AsUnsignedLong(obj); |
| 441 | if (PyErr_Occurred()) { |
| 442 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 443 | goto OverflowUp; |
| 444 | return 0; |
| 445 | } |
| 446 | if ((uid_t)uresult == (uid_t)-1) |
| 447 | goto OverflowUp; |
| 448 | } else { |
| 449 | if (result < 0) |
| 450 | goto OverflowDown; |
| 451 | uresult = result; |
| 452 | } |
| 453 | if (sizeof(uid_t) < sizeof(long) && |
| 454 | (unsigned long)(uid_t)uresult != uresult) |
| 455 | goto OverflowUp; |
| 456 | *(uid_t *)p = (uid_t)uresult; |
| 457 | } |
| 458 | return 1; |
| 459 | |
| 460 | OverflowDown: |
| 461 | PyErr_SetString(PyExc_OverflowError, |
| 462 | "user id is less than minimum"); |
| 463 | return 0; |
| 464 | |
| 465 | OverflowUp: |
| 466 | PyErr_SetString(PyExc_OverflowError, |
| 467 | "user id is greater than maximum"); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | int |
| 472 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 473 | { |
| 474 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 475 | long result; |
| 476 | if (PyFloat_Check(obj)) { |
| 477 | PyErr_SetString(PyExc_TypeError, |
| 478 | "integer argument expected, got float"); |
| 479 | return 0; |
| 480 | } |
| 481 | result = PyLong_AsLongAndOverflow(obj, &overflow); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 482 | if (overflow < 0) |
| 483 | goto OverflowDown; |
| 484 | if (!overflow && result == -1) { |
| 485 | /* error or -1 */ |
| 486 | if (PyErr_Occurred()) |
| 487 | return 0; |
| 488 | *(gid_t *)p = (gid_t)-1; |
| 489 | } |
| 490 | else { |
| 491 | /* unsigned gid_t */ |
| 492 | unsigned long uresult; |
| 493 | if (overflow > 0) { |
| 494 | uresult = PyLong_AsUnsignedLong(obj); |
| 495 | if (PyErr_Occurred()) { |
| 496 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 497 | goto OverflowUp; |
| 498 | return 0; |
| 499 | } |
| 500 | if ((gid_t)uresult == (gid_t)-1) |
| 501 | goto OverflowUp; |
| 502 | } else { |
| 503 | if (result < 0) |
| 504 | goto OverflowDown; |
| 505 | uresult = result; |
| 506 | } |
| 507 | if (sizeof(gid_t) < sizeof(long) && |
| 508 | (unsigned long)(gid_t)uresult != uresult) |
| 509 | goto OverflowUp; |
| 510 | *(gid_t *)p = (gid_t)uresult; |
| 511 | } |
| 512 | return 1; |
| 513 | |
| 514 | OverflowDown: |
| 515 | PyErr_SetString(PyExc_OverflowError, |
| 516 | "group id is less than minimum"); |
| 517 | return 0; |
| 518 | |
| 519 | OverflowUp: |
| 520 | PyErr_SetString(PyExc_OverflowError, |
| 521 | "group id is greater than maximum"); |
| 522 | return 0; |
| 523 | } |
| 524 | #endif /* MS_WINDOWS */ |
| 525 | |
| 526 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 527 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 528 | /* |
| 529 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 530 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 531 | * which doesn't play nicely with all the initializer lines in this file that |
| 532 | * look like this: |
| 533 | * int dir_fd = DEFAULT_DIR_FD; |
| 534 | */ |
| 535 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 536 | #else |
| 537 | #define DEFAULT_DIR_FD (-100) |
| 538 | #endif |
| 539 | |
| 540 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 541 | _fd_converter(PyObject *o, int *p, const char *allowed) |
| 542 | { |
| 543 | int overflow; |
| 544 | long long_value = PyLong_AsLongAndOverflow(o, &overflow); |
| 545 | if (PyFloat_Check(o) || |
| 546 | (long_value == -1 && !overflow && PyErr_Occurred())) { |
| 547 | PyErr_Clear(); |
| 548 | PyErr_Format(PyExc_TypeError, |
| 549 | "argument should be %s, not %.200s", |
| 550 | allowed, Py_TYPE(o)->tp_name); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 551 | return 0; |
| 552 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 553 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 554 | PyErr_SetString(PyExc_OverflowError, |
| 555 | "signed integer is greater than maximum"); |
| 556 | return 0; |
| 557 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 558 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 559 | PyErr_SetString(PyExc_OverflowError, |
| 560 | "signed integer is less than minimum"); |
| 561 | return 0; |
| 562 | } |
| 563 | *p = (int)long_value; |
| 564 | return 1; |
| 565 | } |
| 566 | |
| 567 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 568 | dir_fd_converter(PyObject *o, void *p) |
| 569 | { |
| 570 | if (o == Py_None) { |
| 571 | *(int *)p = DEFAULT_DIR_FD; |
| 572 | return 1; |
| 573 | } |
| 574 | return _fd_converter(o, (int *)p, "integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
| 578 | |
| 579 | /* |
| 580 | * A PyArg_ParseTuple "converter" function |
| 581 | * that handles filesystem paths in the manner |
| 582 | * preferred by the os module. |
| 583 | * |
| 584 | * path_converter accepts (Unicode) strings and their |
| 585 | * subclasses, and bytes and their subclasses. What |
| 586 | * it does with the argument depends on the platform: |
| 587 | * |
| 588 | * * On Windows, if we get a (Unicode) string we |
| 589 | * extract the wchar_t * and return it; if we get |
| 590 | * bytes we extract the char * and return that. |
| 591 | * |
| 592 | * * On all other platforms, strings are encoded |
| 593 | * to bytes using PyUnicode_FSConverter, then we |
| 594 | * extract the char * from the bytes object and |
| 595 | * return that. |
| 596 | * |
| 597 | * path_converter also optionally accepts signed |
| 598 | * integers (representing open file descriptors) instead |
| 599 | * of path strings. |
| 600 | * |
| 601 | * Input fields: |
| 602 | * path.nullable |
| 603 | * If nonzero, the path is permitted to be None. |
| 604 | * path.allow_fd |
| 605 | * If nonzero, the path is permitted to be a file handle |
| 606 | * (a signed int) instead of a string. |
| 607 | * path.function_name |
| 608 | * If non-NULL, path_converter will use that as the name |
| 609 | * of the function in error messages. |
| 610 | * (If path.argument_name is NULL it omits the function name.) |
| 611 | * path.argument_name |
| 612 | * If non-NULL, path_converter will use that as the name |
| 613 | * of the parameter in error messages. |
| 614 | * (If path.argument_name is NULL it uses "path".) |
| 615 | * |
| 616 | * Output fields: |
| 617 | * path.wide |
| 618 | * Points to the path if it was expressed as Unicode |
| 619 | * and was not encoded. (Only used on Windows.) |
| 620 | * path.narrow |
| 621 | * Points to the path if it was expressed as bytes, |
| 622 | * or it was Unicode and was encoded to bytes. |
| 623 | * path.fd |
| 624 | * Contains a file descriptor if path.accept_fd was true |
| 625 | * and the caller provided a signed integer instead of any |
| 626 | * sort of string. |
| 627 | * |
| 628 | * WARNING: if your "path" parameter is optional, and is |
| 629 | * unspecified, path_converter will never get called. |
| 630 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 631 | * yourself! |
| 632 | * path.length |
| 633 | * The length of the path in characters, if specified as |
| 634 | * a string. |
| 635 | * path.object |
| 636 | * The original object passed in. |
| 637 | * path.cleanup |
| 638 | * For internal use only. May point to a temporary object. |
| 639 | * (Pay no attention to the man behind the curtain.) |
| 640 | * |
| 641 | * At most one of path.wide or path.narrow will be non-NULL. |
| 642 | * If path was None and path.nullable was set, |
| 643 | * or if path was an integer and path.allow_fd was set, |
| 644 | * both path.wide and path.narrow will be NULL |
| 645 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 646 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 647 | * path_converter takes care to not write to the path_t |
| 648 | * unless it's successful. However it must reset the |
| 649 | * "cleanup" field each time it's called. |
| 650 | * |
| 651 | * Use as follows: |
| 652 | * path_t path; |
| 653 | * memset(&path, 0, sizeof(path)); |
| 654 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 655 | * // ... use values from path ... |
| 656 | * path_cleanup(&path); |
| 657 | * |
| 658 | * (Note that if PyArg_Parse fails you don't need to call |
| 659 | * path_cleanup(). However it is safe to do so.) |
| 660 | */ |
| 661 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 662 | const char *function_name; |
| 663 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 664 | int nullable; |
| 665 | int allow_fd; |
| 666 | wchar_t *wide; |
| 667 | char *narrow; |
| 668 | int fd; |
| 669 | Py_ssize_t length; |
| 670 | PyObject *object; |
| 671 | PyObject *cleanup; |
| 672 | } path_t; |
| 673 | |
| 674 | static void |
| 675 | path_cleanup(path_t *path) { |
| 676 | if (path->cleanup) { |
| 677 | Py_DECREF(path->cleanup); |
| 678 | path->cleanup = NULL; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | static int |
| 683 | path_converter(PyObject *o, void *p) { |
| 684 | path_t *path = (path_t *)p; |
| 685 | PyObject *unicode, *bytes; |
| 686 | Py_ssize_t length; |
| 687 | char *narrow; |
| 688 | |
| 689 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 690 | PyErr_Format(exc, "%s%s" fmt, \ |
| 691 | path->function_name ? path->function_name : "", \ |
| 692 | path->function_name ? ": " : "", \ |
| 693 | path->argument_name ? path->argument_name : "path") |
| 694 | |
| 695 | /* Py_CLEANUP_SUPPORTED support */ |
| 696 | if (o == NULL) { |
| 697 | path_cleanup(path); |
| 698 | return 1; |
| 699 | } |
| 700 | |
| 701 | /* ensure it's always safe to call path_cleanup() */ |
| 702 | path->cleanup = NULL; |
| 703 | |
| 704 | if (o == Py_None) { |
| 705 | if (!path->nullable) { |
| 706 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 707 | "can't specify None for %s argument"); |
| 708 | return 0; |
| 709 | } |
| 710 | path->wide = NULL; |
| 711 | path->narrow = NULL; |
| 712 | path->length = 0; |
| 713 | path->object = o; |
| 714 | path->fd = -1; |
| 715 | return 1; |
| 716 | } |
| 717 | |
| 718 | unicode = PyUnicode_FromObject(o); |
| 719 | if (unicode) { |
| 720 | #ifdef MS_WINDOWS |
| 721 | wchar_t *wide; |
| 722 | length = PyUnicode_GET_SIZE(unicode); |
| 723 | if (length > 32767) { |
| 724 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 725 | Py_DECREF(unicode); |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | wide = PyUnicode_AsUnicode(unicode); |
| 730 | if (!wide) { |
| 731 | Py_DECREF(unicode); |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | path->wide = wide; |
| 736 | path->narrow = NULL; |
| 737 | path->length = length; |
| 738 | path->object = o; |
| 739 | path->fd = -1; |
| 740 | path->cleanup = unicode; |
| 741 | return Py_CLEANUP_SUPPORTED; |
| 742 | #else |
| 743 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 744 | Py_DECREF(unicode); |
| 745 | if (!converted) |
| 746 | bytes = NULL; |
| 747 | #endif |
| 748 | } |
| 749 | else { |
| 750 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 751 | if (PyObject_CheckBuffer(o)) |
| 752 | bytes = PyBytes_FromObject(o); |
| 753 | else |
| 754 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 755 | if (!bytes) { |
| 756 | PyErr_Clear(); |
| 757 | if (path->allow_fd) { |
| 758 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 759 | int result = _fd_converter(o, &fd, |
| 760 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 761 | if (result) { |
| 762 | path->wide = NULL; |
| 763 | path->narrow = NULL; |
| 764 | path->length = 0; |
| 765 | path->object = o; |
| 766 | path->fd = fd; |
| 767 | return result; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if (!bytes) { |
| 774 | if (!PyErr_Occurred()) |
| 775 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | #ifdef MS_WINDOWS |
| 780 | if (win32_warn_bytes_api()) { |
| 781 | Py_DECREF(bytes); |
| 782 | return 0; |
| 783 | } |
| 784 | #endif |
| 785 | |
| 786 | length = PyBytes_GET_SIZE(bytes); |
| 787 | #ifdef MS_WINDOWS |
| 788 | if (length > MAX_PATH) { |
| 789 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 790 | Py_DECREF(bytes); |
| 791 | return 0; |
| 792 | } |
| 793 | #endif |
| 794 | |
| 795 | narrow = PyBytes_AS_STRING(bytes); |
| 796 | if (length != strlen(narrow)) { |
| 797 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); |
| 798 | Py_DECREF(bytes); |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | path->wide = NULL; |
| 803 | path->narrow = narrow; |
| 804 | path->length = length; |
| 805 | path->object = o; |
| 806 | path->fd = -1; |
| 807 | path->cleanup = bytes; |
| 808 | return Py_CLEANUP_SUPPORTED; |
| 809 | } |
| 810 | |
| 811 | static void |
| 812 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 813 | PyErr_Format(PyExc_NotImplementedError, |
| 814 | "%s%s%s unavailable on this platform", |
| 815 | (function_name != NULL) ? function_name : "", |
| 816 | (function_name != NULL) ? ": ": "", |
| 817 | argument_name); |
| 818 | } |
| 819 | |
| 820 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 821 | dir_fd_unavailable(PyObject *o, void *p) |
| 822 | { |
| 823 | int dir_fd; |
| 824 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 825 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 826 | if (dir_fd != DEFAULT_DIR_FD) { |
| 827 | argument_unavailable_error(NULL, "dir_fd"); |
| 828 | return 0; |
| 829 | } |
| 830 | *(int *)p = dir_fd; |
| 831 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static int |
| 835 | fd_specified(char *function_name, int fd) { |
| 836 | if (fd == -1) |
| 837 | return 0; |
| 838 | |
| 839 | argument_unavailable_error(function_name, "fd"); |
| 840 | return 1; |
| 841 | } |
| 842 | |
| 843 | static int |
| 844 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 845 | if (follow_symlinks) |
| 846 | return 0; |
| 847 | |
| 848 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 849 | return 1; |
| 850 | } |
| 851 | |
| 852 | static int |
| 853 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 854 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 855 | PyErr_Format(PyExc_ValueError, |
| 856 | "%s: can't specify dir_fd without matching path", |
| 857 | function_name); |
| 858 | return 1; |
| 859 | } |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | static int |
| 864 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 865 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 866 | PyErr_Format(PyExc_ValueError, |
| 867 | "%s: can't specify both dir_fd and fd", |
| 868 | function_name); |
| 869 | return 1; |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int |
| 875 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 876 | int follow_symlinks) { |
| 877 | if ((fd > 0) && (!follow_symlinks)) { |
| 878 | PyErr_Format(PyExc_ValueError, |
| 879 | "%s: cannot use fd and follow_symlinks together", |
| 880 | function_name); |
| 881 | return 1; |
| 882 | } |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | static int |
| 887 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 888 | int follow_symlinks) { |
| 889 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 890 | PyErr_Format(PyExc_ValueError, |
| 891 | "%s: cannot use dir_fd and follow_symlinks together", |
| 892 | function_name); |
| 893 | return 1; |
| 894 | } |
| 895 | return 0; |
| 896 | } |
| 897 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 898 | /* A helper used by a number of POSIX-only functions */ |
| 899 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 900 | static int |
| 901 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 902 | { |
| 903 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 904 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 905 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 906 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 907 | #endif |
| 908 | if (PyErr_Occurred()) |
| 909 | return 0; |
| 910 | return 1; |
| 911 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 912 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 913 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 914 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 915 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 916 | * valid and raise an assertion if it isn't. |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 917 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 918 | * an assertion can be useful, but it does contradict the POSIX standard |
| 919 | * which for write(2) states: |
| 920 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 921 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 922 | * writing." |
| 923 | * Furthermore, python allows the user to enter any old integer |
| 924 | * as a fd and should merely raise a python exception on error. |
| 925 | * The Microsoft CRT doesn't provide an official way to check for the |
| 926 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 927 | * 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] | 928 | * internal structures involved. |
| 929 | * The structures below must be updated for each version of visual studio |
| 930 | * according to the file internal.h in the CRT source, until MS comes |
| 931 | * up with a less hacky way to do this. |
| 932 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 933 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 934 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 935 | /* The actual size of the structure is determined at runtime. |
| 936 | * Only the first items must be present. |
| 937 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 938 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 939 | intptr_t osfhnd; |
| 940 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 941 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 942 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 943 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 944 | #define IOINFO_L2E 5 |
| 945 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 946 | #define IOINFO_ARRAYS 64 |
| 947 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 948 | #define FOPEN 0x01 |
| 949 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 950 | |
| 951 | /* This function emulates what the windows CRT does to validate file handles */ |
| 952 | int |
| 953 | _PyVerify_fd(int fd) |
| 954 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 955 | const int i1 = fd >> IOINFO_L2E; |
| 956 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 958 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 959 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 960 | /* Determine the actual size of the ioinfo structure, |
| 961 | * as used by the CRT loaded in memory |
| 962 | */ |
| 963 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 964 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 965 | } |
| 966 | if (sizeof_ioinfo == 0) { |
| 967 | /* This should not happen... */ |
| 968 | goto fail; |
| 969 | } |
| 970 | |
| 971 | /* See that it isn't a special CLEAR fileno */ |
| 972 | if (fd != _NO_CONSOLE_FILENO) { |
| 973 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 974 | * we check pointer validity and other info |
| 975 | */ |
| 976 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 977 | /* finally, check that the file is open */ |
| 978 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 979 | if (info->osfile & FOPEN) { |
| 980 | return 1; |
| 981 | } |
| 982 | } |
| 983 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 984 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 985 | errno = EBADF; |
| 986 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 990 | static int |
| 991 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 992 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 993 | if (!_PyVerify_fd(fd1)) |
| 994 | return 0; |
| 995 | if (fd2 == _NO_CONSOLE_FILENO) |
| 996 | return 0; |
| 997 | if ((unsigned)fd2 < _NHANDLE_) |
| 998 | return 1; |
| 999 | else |
| 1000 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1001 | } |
| 1002 | #else |
| 1003 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 1004 | #define _PyVerify_fd_dup2(A, B) (1) |
| 1005 | #endif |
| 1006 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1007 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1008 | /* The following structure was copied from |
| 1009 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 1010 | include doesn't seem to be present in the Windows SDK (at least as included |
| 1011 | with Visual Studio Express). */ |
| 1012 | typedef struct _REPARSE_DATA_BUFFER { |
| 1013 | ULONG ReparseTag; |
| 1014 | USHORT ReparseDataLength; |
| 1015 | USHORT Reserved; |
| 1016 | union { |
| 1017 | struct { |
| 1018 | USHORT SubstituteNameOffset; |
| 1019 | USHORT SubstituteNameLength; |
| 1020 | USHORT PrintNameOffset; |
| 1021 | USHORT PrintNameLength; |
| 1022 | ULONG Flags; |
| 1023 | WCHAR PathBuffer[1]; |
| 1024 | } SymbolicLinkReparseBuffer; |
| 1025 | |
| 1026 | struct { |
| 1027 | USHORT SubstituteNameOffset; |
| 1028 | USHORT SubstituteNameLength; |
| 1029 | USHORT PrintNameOffset; |
| 1030 | USHORT PrintNameLength; |
| 1031 | WCHAR PathBuffer[1]; |
| 1032 | } MountPointReparseBuffer; |
| 1033 | |
| 1034 | struct { |
| 1035 | UCHAR DataBuffer[1]; |
| 1036 | } GenericReparseBuffer; |
| 1037 | }; |
| 1038 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 1039 | |
| 1040 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 1041 | GenericReparseBuffer) |
| 1042 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 1043 | |
| 1044 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1045 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1046 | { |
| 1047 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1048 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1049 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1050 | |
| 1051 | if (0 == DeviceIoControl( |
| 1052 | reparse_point_handle, |
| 1053 | FSCTL_GET_REPARSE_POINT, |
| 1054 | NULL, 0, /* in buffer */ |
| 1055 | target_buffer, sizeof(target_buffer), |
| 1056 | &n_bytes_returned, |
| 1057 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1058 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1059 | |
| 1060 | if (reparse_tag) |
| 1061 | *reparse_tag = rdb->ReparseTag; |
| 1062 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1063 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1064 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1065 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1066 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1067 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1068 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1069 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1070 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1071 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1072 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1073 | */ |
| 1074 | #include <crt_externs.h> |
| 1075 | static char **environ; |
| 1076 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1077 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1078 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1079 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1080 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1081 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1083 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1084 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1085 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1086 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1087 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1088 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1089 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1090 | d = PyDict_New(); |
| 1091 | if (d == NULL) |
| 1092 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1093 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1094 | if (environ == NULL) |
| 1095 | environ = *_NSGetEnviron(); |
| 1096 | #endif |
| 1097 | #ifdef MS_WINDOWS |
| 1098 | /* _wenviron must be initialized in this way if the program is started |
| 1099 | through main() instead of wmain(). */ |
| 1100 | _wgetenv(L""); |
| 1101 | if (_wenviron == NULL) |
| 1102 | return d; |
| 1103 | /* This part ignores errors */ |
| 1104 | for (e = _wenviron; *e != NULL; e++) { |
| 1105 | PyObject *k; |
| 1106 | PyObject *v; |
| 1107 | wchar_t *p = wcschr(*e, L'='); |
| 1108 | if (p == NULL) |
| 1109 | continue; |
| 1110 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1111 | if (k == NULL) { |
| 1112 | PyErr_Clear(); |
| 1113 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1114 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1115 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1116 | if (v == NULL) { |
| 1117 | PyErr_Clear(); |
| 1118 | Py_DECREF(k); |
| 1119 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1120 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1121 | if (PyDict_GetItem(d, k) == NULL) { |
| 1122 | if (PyDict_SetItem(d, k, v) != 0) |
| 1123 | PyErr_Clear(); |
| 1124 | } |
| 1125 | Py_DECREF(k); |
| 1126 | Py_DECREF(v); |
| 1127 | } |
| 1128 | #else |
| 1129 | if (environ == NULL) |
| 1130 | return d; |
| 1131 | /* This part ignores errors */ |
| 1132 | for (e = environ; *e != NULL; e++) { |
| 1133 | PyObject *k; |
| 1134 | PyObject *v; |
| 1135 | char *p = strchr(*e, '='); |
| 1136 | if (p == NULL) |
| 1137 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1138 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1139 | if (k == NULL) { |
| 1140 | PyErr_Clear(); |
| 1141 | continue; |
| 1142 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1143 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1144 | if (v == NULL) { |
| 1145 | PyErr_Clear(); |
| 1146 | Py_DECREF(k); |
| 1147 | continue; |
| 1148 | } |
| 1149 | if (PyDict_GetItem(d, k) == NULL) { |
| 1150 | if (PyDict_SetItem(d, k, v) != 0) |
| 1151 | PyErr_Clear(); |
| 1152 | } |
| 1153 | Py_DECREF(k); |
| 1154 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1155 | } |
| 1156 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1157 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1160 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1161 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1162 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1163 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1164 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1165 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1166 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1167 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1168 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1169 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1170 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1171 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1172 | /* XXX We should pass the function name along in the future. |
| 1173 | (winreg.c also wants to pass the function name.) |
| 1174 | This would however require an additional param to the |
| 1175 | Windows error object, which is non-trivial. |
| 1176 | */ |
| 1177 | errno = GetLastError(); |
| 1178 | if (filename) |
| 1179 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1180 | else |
| 1181 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1182 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1183 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1184 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1185 | win32_error_object(char* function, PyObject* filename) |
| 1186 | { |
| 1187 | /* XXX - see win32_error for comments on 'function' */ |
| 1188 | errno = GetLastError(); |
| 1189 | if (filename) |
| 1190 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1191 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1192 | errno, |
| 1193 | filename); |
| 1194 | else |
| 1195 | return PyErr_SetFromWindowsErr(errno); |
| 1196 | } |
| 1197 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1198 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1199 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1200 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1201 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1202 | { |
| 1203 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1204 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1205 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1206 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1207 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1208 | #endif |
| 1209 | } |
| 1210 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1211 | /* POSIX generic methods */ |
| 1212 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1213 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1214 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1215 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1216 | int fd; |
| 1217 | int res; |
| 1218 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1219 | if (fd < 0) |
| 1220 | return NULL; |
| 1221 | if (!_PyVerify_fd(fd)) |
| 1222 | return posix_error(); |
| 1223 | Py_BEGIN_ALLOW_THREADS |
| 1224 | res = (*func)(fd); |
| 1225 | Py_END_ALLOW_THREADS |
| 1226 | if (res < 0) |
| 1227 | return posix_error(); |
| 1228 | Py_INCREF(Py_None); |
| 1229 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1230 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1231 | |
| 1232 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1233 | posix_1str(const char *func_name, PyObject *args, char *format, |
| 1234 | int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1235 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1236 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1237 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1238 | memset(&path, 0, sizeof(path)); |
| 1239 | path.function_name = func_name; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1240 | if (!PyArg_ParseTuple(args, format, |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1241 | path_converter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1242 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1243 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1244 | res = (*func)(path.narrow); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1245 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1246 | if (res < 0) { |
| 1247 | path_error(&path); |
| 1248 | path_cleanup(&path); |
| 1249 | return NULL; |
| 1250 | } |
| 1251 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1252 | Py_INCREF(Py_None); |
| 1253 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1256 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1257 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1258 | /* This is a reimplementation of the C library's chdir function, |
| 1259 | but one that produces Win32 errors instead of DOS error codes. |
| 1260 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1261 | it also needs to set "magic" environment variables indicating |
| 1262 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1263 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1264 | win32_chdir(LPCSTR path) |
| 1265 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1266 | char new_path[MAX_PATH+1]; |
| 1267 | int result; |
| 1268 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1269 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1270 | if(!SetCurrentDirectoryA(path)) |
| 1271 | return FALSE; |
| 1272 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 1273 | if (!result) |
| 1274 | return FALSE; |
| 1275 | /* In the ANSI API, there should not be any paths longer |
| 1276 | than MAX_PATH. */ |
| 1277 | assert(result <= MAX_PATH+1); |
| 1278 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1279 | strncmp(new_path, "//", 2) == 0) |
| 1280 | /* UNC path, nothing to do. */ |
| 1281 | return TRUE; |
| 1282 | env[1] = new_path[0]; |
| 1283 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | /* The Unicode version differs from the ANSI version |
| 1287 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1288 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1289 | win32_wchdir(LPCWSTR path) |
| 1290 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1291 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 1292 | int result; |
| 1293 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1294 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1295 | if(!SetCurrentDirectoryW(path)) |
| 1296 | return FALSE; |
| 1297 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 1298 | if (!result) |
| 1299 | return FALSE; |
| 1300 | if (result > MAX_PATH+1) { |
| 1301 | new_path = malloc(result * sizeof(wchar_t)); |
| 1302 | if (!new_path) { |
| 1303 | SetLastError(ERROR_OUTOFMEMORY); |
| 1304 | return FALSE; |
| 1305 | } |
| 1306 | result = GetCurrentDirectoryW(result, new_path); |
| 1307 | if (!result) { |
| 1308 | free(new_path); |
| 1309 | return FALSE; |
| 1310 | } |
| 1311 | } |
| 1312 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1313 | wcsncmp(new_path, L"//", 2) == 0) |
| 1314 | /* UNC path, nothing to do. */ |
| 1315 | return TRUE; |
| 1316 | env[1] = new_path[0]; |
| 1317 | result = SetEnvironmentVariableW(env, new_path); |
| 1318 | if (new_path != _new_path) |
| 1319 | free(new_path); |
| 1320 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1321 | } |
| 1322 | #endif |
| 1323 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1324 | #ifdef MS_WINDOWS |
| 1325 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1326 | - time stamps are restricted to second resolution |
| 1327 | - file modification times suffer from forth-and-back conversions between |
| 1328 | UTC and local time |
| 1329 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1330 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1331 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1332 | |
| 1333 | struct win32_stat{ |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1334 | unsigned long st_dev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1335 | __int64 st_ino; |
| 1336 | unsigned short st_mode; |
| 1337 | int st_nlink; |
| 1338 | int st_uid; |
| 1339 | int st_gid; |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1340 | unsigned long st_rdev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1341 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1342 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1343 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1344 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1345 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1346 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1347 | int st_ctime_nsec; |
| 1348 | }; |
| 1349 | |
| 1350 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1351 | |
| 1352 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1353 | 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] | 1354 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1355 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1356 | /* Cannot simply cast and dereference in_ptr, |
| 1357 | since it might not be aligned properly */ |
| 1358 | __int64 in; |
| 1359 | memcpy(&in, in_ptr, sizeof(in)); |
| 1360 | *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] | 1361 | *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] | 1362 | } |
| 1363 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1364 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1365 | 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] | 1366 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1367 | /* XXX endianness */ |
| 1368 | __int64 out; |
| 1369 | out = time_in + secs_between_epochs; |
| 1370 | out = out * 10000000 + nsec_in / 100; |
| 1371 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1374 | /* Below, we *know* that ugo+r is 0444 */ |
| 1375 | #if _S_IREAD != 0400 |
| 1376 | #error Unsupported C library |
| 1377 | #endif |
| 1378 | static int |
| 1379 | attributes_to_mode(DWORD attr) |
| 1380 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1381 | int m = 0; |
| 1382 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1383 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1384 | else |
| 1385 | m |= _S_IFREG; |
| 1386 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1387 | m |= 0444; |
| 1388 | else |
| 1389 | m |= 0666; |
| 1390 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1394 | 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] | 1395 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1396 | memset(result, 0, sizeof(*result)); |
| 1397 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1398 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 1399 | result->st_dev = info->dwVolumeSerialNumber; |
| 1400 | result->st_rdev = result->st_dev; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1401 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1402 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1403 | 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] | 1404 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1405 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1406 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1407 | /* first clear the S_IFMT bits */ |
| 1408 | result->st_mode ^= (result->st_mode & 0170000); |
| 1409 | /* now set the bits that make this a symlink */ |
| 1410 | result->st_mode |= 0120000; |
| 1411 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1412 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1413 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1416 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1417 | 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] | 1418 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1419 | HANDLE hFindFile; |
| 1420 | WIN32_FIND_DATAA FileData; |
| 1421 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1422 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1423 | return FALSE; |
| 1424 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1425 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1426 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1427 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1428 | info->ftCreationTime = FileData.ftCreationTime; |
| 1429 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1430 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1431 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1432 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1433 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1434 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1435 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1436 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1440 | 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] | 1441 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1442 | HANDLE hFindFile; |
| 1443 | WIN32_FIND_DATAW FileData; |
| 1444 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1445 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1446 | return FALSE; |
| 1447 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1448 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1449 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1450 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1451 | info->ftCreationTime = FileData.ftCreationTime; |
| 1452 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1453 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1454 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1455 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1456 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1457 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1458 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1459 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1462 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1463 | static int has_GetFinalPathNameByHandle = -1; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1464 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1465 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1466 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1467 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1468 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1469 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1470 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1471 | DWORD); |
| 1472 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1473 | /* only recheck */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1474 | if (-1 == has_GetFinalPathNameByHandle) |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1475 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1476 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1477 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1478 | "GetFinalPathNameByHandleA"); |
| 1479 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1480 | "GetFinalPathNameByHandleW"); |
| 1481 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1482 | Py_GetFinalPathNameByHandleW; |
| 1483 | } |
| 1484 | return has_GetFinalPathNameByHandle; |
| 1485 | } |
| 1486 | |
| 1487 | static BOOL |
| 1488 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1489 | { |
| 1490 | int buf_size, result_length; |
| 1491 | wchar_t *buf; |
| 1492 | |
| 1493 | /* We have a good handle to the target, use it to determine |
| 1494 | the target path name (then we'll call lstat on it). */ |
| 1495 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1496 | VOLUME_NAME_DOS); |
| 1497 | if(!buf_size) |
| 1498 | return FALSE; |
| 1499 | |
| 1500 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1501 | if (!buf) { |
| 1502 | SetLastError(ERROR_OUTOFMEMORY); |
| 1503 | return FALSE; |
| 1504 | } |
| 1505 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1506 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1507 | buf, buf_size, VOLUME_NAME_DOS); |
| 1508 | |
| 1509 | if(!result_length) { |
| 1510 | free(buf); |
| 1511 | return FALSE; |
| 1512 | } |
| 1513 | |
| 1514 | if(!CloseHandle(hdl)) { |
| 1515 | free(buf); |
| 1516 | return FALSE; |
| 1517 | } |
| 1518 | |
| 1519 | buf[result_length] = 0; |
| 1520 | |
| 1521 | *target_path = buf; |
| 1522 | return TRUE; |
| 1523 | } |
| 1524 | |
| 1525 | static int |
| 1526 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1527 | BOOL traverse); |
| 1528 | static int |
| 1529 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1530 | BOOL traverse) |
| 1531 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1532 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1533 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1534 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1535 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1536 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1537 | const char *dot; |
| 1538 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1539 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1540 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1541 | traverse reparse point. */ |
| 1542 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1545 | hFile = CreateFileA( |
| 1546 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1547 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1548 | 0, /* share mode */ |
| 1549 | NULL, /* security attributes */ |
| 1550 | OPEN_EXISTING, |
| 1551 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1552 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1553 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1554 | the symlink path agin and not the actual final path. */ |
| 1555 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1556 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1557 | NULL); |
| 1558 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1559 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1560 | /* Either the target doesn't exist, or we don't have access to |
| 1561 | get a handle to it. If the former, we need to return an error. |
| 1562 | If the latter, we can use attributes_from_dir. */ |
| 1563 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1564 | return -1; |
| 1565 | /* Could not get attributes on open file. Fall back to |
| 1566 | reading the directory. */ |
| 1567 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1568 | /* Very strange. This should not fail now */ |
| 1569 | return -1; |
| 1570 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1571 | if (traverse) { |
| 1572 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1573 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1574 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1575 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1576 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1577 | } else { |
| 1578 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1579 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1580 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1581 | } |
| 1582 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1583 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1584 | return -1; |
| 1585 | |
| 1586 | /* Close the outer open file handle now that we're about to |
| 1587 | reopen it with different flags. */ |
| 1588 | if (!CloseHandle(hFile)) |
| 1589 | return -1; |
| 1590 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1591 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1592 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1593 | the file without the reparse handling flag set. */ |
| 1594 | hFile2 = CreateFileA( |
| 1595 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1596 | NULL, OPEN_EXISTING, |
| 1597 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1598 | NULL); |
| 1599 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1600 | return -1; |
| 1601 | |
| 1602 | if (!get_target_path(hFile2, &target_path)) |
| 1603 | return -1; |
| 1604 | |
| 1605 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1606 | free(target_path); |
| 1607 | return code; |
| 1608 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1609 | } else |
| 1610 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1611 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1612 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1613 | |
| 1614 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1615 | dot = strrchr(path, '.'); |
| 1616 | if (dot) { |
| 1617 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1618 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1619 | result->st_mode |= 0111; |
| 1620 | } |
| 1621 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1625 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1626 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1627 | { |
| 1628 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1629 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1630 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1631 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1632 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1633 | const wchar_t *dot; |
| 1634 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1635 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1636 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1637 | traverse reparse point. */ |
| 1638 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1641 | hFile = CreateFileW( |
| 1642 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1643 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1644 | 0, /* share mode */ |
| 1645 | NULL, /* security attributes */ |
| 1646 | OPEN_EXISTING, |
| 1647 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1648 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1649 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1650 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1651 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1652 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1653 | NULL); |
| 1654 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1655 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1656 | /* Either the target doesn't exist, or we don't have access to |
| 1657 | get a handle to it. If the former, we need to return an error. |
| 1658 | If the latter, we can use attributes_from_dir. */ |
| 1659 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1660 | return -1; |
| 1661 | /* Could not get attributes on open file. Fall back to |
| 1662 | reading the directory. */ |
| 1663 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1664 | /* Very strange. This should not fail now */ |
| 1665 | return -1; |
| 1666 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1667 | if (traverse) { |
| 1668 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1669 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1670 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1671 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1672 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1673 | } else { |
| 1674 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1675 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1676 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1677 | } |
| 1678 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1679 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1680 | return -1; |
| 1681 | |
| 1682 | /* Close the outer open file handle now that we're about to |
| 1683 | reopen it with different flags. */ |
| 1684 | if (!CloseHandle(hFile)) |
| 1685 | return -1; |
| 1686 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1687 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1688 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1689 | the file without the reparse handling flag set. */ |
| 1690 | hFile2 = CreateFileW( |
| 1691 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1692 | NULL, OPEN_EXISTING, |
| 1693 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1694 | NULL); |
| 1695 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1696 | return -1; |
| 1697 | |
| 1698 | if (!get_target_path(hFile2, &target_path)) |
| 1699 | return -1; |
| 1700 | |
| 1701 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1702 | free(target_path); |
| 1703 | return code; |
| 1704 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1705 | } else |
| 1706 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1707 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1708 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1709 | |
| 1710 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1711 | dot = wcsrchr(path, '.'); |
| 1712 | if (dot) { |
| 1713 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1714 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1715 | result->st_mode |= 0111; |
| 1716 | } |
| 1717 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1721 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1722 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1723 | /* Protocol violation: we explicitly clear errno, instead of |
| 1724 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1725 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1726 | errno = 0; |
| 1727 | return code; |
| 1728 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1729 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1730 | static int |
| 1731 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1732 | { |
| 1733 | /* Protocol violation: we explicitly clear errno, instead of |
| 1734 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1735 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1736 | errno = 0; |
| 1737 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1738 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1739 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1740 | |
| 1741 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1742 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1743 | default does not traverse symlinks and instead returns attributes for |
| 1744 | the symlink. |
| 1745 | |
| 1746 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1747 | win32_stat will first explicitly resolve the symlink target and then will |
| 1748 | call win32_lstat on that result. |
| 1749 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1750 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1751 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1752 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1753 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1754 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1755 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1759 | 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] | 1760 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1761 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | static int |
| 1765 | win32_stat(const char* path, struct win32_stat *result) |
| 1766 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1767 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1770 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1771 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1772 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1773 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | static int |
| 1777 | win32_fstat(int file_number, struct win32_stat *result) |
| 1778 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1779 | BY_HANDLE_FILE_INFORMATION info; |
| 1780 | HANDLE h; |
| 1781 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1782 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 1783 | if (!_PyVerify_fd(file_number)) |
| 1784 | h = INVALID_HANDLE_VALUE; |
| 1785 | else |
| 1786 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1787 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1788 | /* Protocol violation: we explicitly clear errno, instead of |
| 1789 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1790 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1791 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1792 | if (h == INVALID_HANDLE_VALUE) { |
| 1793 | /* This is really a C library error (invalid file handle). |
| 1794 | We set the Win32 error to the closes one matching. */ |
| 1795 | SetLastError(ERROR_INVALID_HANDLE); |
| 1796 | return -1; |
| 1797 | } |
| 1798 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1799 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1800 | type = GetFileType(h); |
| 1801 | if (type == FILE_TYPE_UNKNOWN) { |
| 1802 | DWORD error = GetLastError(); |
| 1803 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1804 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1805 | } |
| 1806 | /* else: valid but unknown file */ |
| 1807 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1808 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1809 | if (type != FILE_TYPE_DISK) { |
| 1810 | if (type == FILE_TYPE_CHAR) |
| 1811 | result->st_mode = _S_IFCHR; |
| 1812 | else if (type == FILE_TYPE_PIPE) |
| 1813 | result->st_mode = _S_IFIFO; |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
| 1817 | if (!GetFileInformationByHandle(h, &info)) { |
| 1818 | return -1; |
| 1819 | } |
| 1820 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1821 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1822 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1823 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1824 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | #endif /* MS_WINDOWS */ |
| 1828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1829 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1830 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1831 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1832 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1833 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1834 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1835 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1836 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1837 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1838 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1839 | |
| 1840 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1841 | {"st_mode", "protection bits"}, |
| 1842 | {"st_ino", "inode"}, |
| 1843 | {"st_dev", "device"}, |
| 1844 | {"st_nlink", "number of hard links"}, |
| 1845 | {"st_uid", "user ID of owner"}, |
| 1846 | {"st_gid", "group ID of owner"}, |
| 1847 | {"st_size", "total size, in bytes"}, |
| 1848 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1849 | {NULL, "integer time of last access"}, |
| 1850 | {NULL, "integer time of last modification"}, |
| 1851 | {NULL, "integer time of last change"}, |
| 1852 | {"st_atime", "time of last access"}, |
| 1853 | {"st_mtime", "time of last modification"}, |
| 1854 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1855 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1856 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1857 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1858 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1859 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1860 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1861 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1862 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1863 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1864 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1865 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1866 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1867 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1868 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1869 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1870 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1871 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1872 | #endif |
| 1873 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1874 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1875 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1876 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1877 | }; |
| 1878 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1879 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1880 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1881 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1882 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1883 | #endif |
| 1884 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1885 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1886 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1887 | #else |
| 1888 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1889 | #endif |
| 1890 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1891 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1892 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1893 | #else |
| 1894 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1895 | #endif |
| 1896 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1897 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1898 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1899 | #else |
| 1900 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1901 | #endif |
| 1902 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1903 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1904 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1905 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1906 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1907 | #endif |
| 1908 | |
| 1909 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1910 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1911 | #else |
| 1912 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1913 | #endif |
| 1914 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1915 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1916 | "stat_result", /* name */ |
| 1917 | stat_result__doc__, /* doc */ |
| 1918 | stat_result_fields, |
| 1919 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1920 | }; |
| 1921 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1922 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1923 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1924 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1925 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1926 | 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] | 1927 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1928 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1929 | |
| 1930 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1931 | {"f_bsize", }, |
| 1932 | {"f_frsize", }, |
| 1933 | {"f_blocks", }, |
| 1934 | {"f_bfree", }, |
| 1935 | {"f_bavail", }, |
| 1936 | {"f_files", }, |
| 1937 | {"f_ffree", }, |
| 1938 | {"f_favail", }, |
| 1939 | {"f_flag", }, |
| 1940 | {"f_namemax",}, |
| 1941 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1942 | }; |
| 1943 | |
| 1944 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1945 | "statvfs_result", /* name */ |
| 1946 | statvfs_result__doc__, /* doc */ |
| 1947 | statvfs_result_fields, |
| 1948 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1949 | }; |
| 1950 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1951 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1952 | PyDoc_STRVAR(waitid_result__doc__, |
| 1953 | "waitid_result: Result from waitid.\n\n\ |
| 1954 | This object may be accessed either as a tuple of\n\ |
| 1955 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1956 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1957 | \n\ |
| 1958 | See os.waitid for more information."); |
| 1959 | |
| 1960 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1961 | {"si_pid", }, |
| 1962 | {"si_uid", }, |
| 1963 | {"si_signo", }, |
| 1964 | {"si_status", }, |
| 1965 | {"si_code", }, |
| 1966 | {0} |
| 1967 | }; |
| 1968 | |
| 1969 | static PyStructSequence_Desc waitid_result_desc = { |
| 1970 | "waitid_result", /* name */ |
| 1971 | waitid_result__doc__, /* doc */ |
| 1972 | waitid_result_fields, |
| 1973 | 5 |
| 1974 | }; |
| 1975 | static PyTypeObject WaitidResultType; |
| 1976 | #endif |
| 1977 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1978 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1979 | static PyTypeObject StatResultType; |
| 1980 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1981 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1982 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1983 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1984 | static newfunc structseq_new; |
| 1985 | |
| 1986 | static PyObject * |
| 1987 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1988 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1989 | PyStructSequence *result; |
| 1990 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1991 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1992 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1993 | if (!result) |
| 1994 | return NULL; |
| 1995 | /* If we have been initialized from a tuple, |
| 1996 | st_?time might be set to None. Initialize it |
| 1997 | from the int slots. */ |
| 1998 | for (i = 7; i <= 9; i++) { |
| 1999 | if (result->ob_item[i+3] == Py_None) { |
| 2000 | Py_DECREF(Py_None); |
| 2001 | Py_INCREF(result->ob_item[i]); |
| 2002 | result->ob_item[i+3] = result->ob_item[i]; |
| 2003 | } |
| 2004 | } |
| 2005 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | |
| 2009 | |
| 2010 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 2011 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2012 | |
| 2013 | PyDoc_STRVAR(stat_float_times__doc__, |
| 2014 | "stat_float_times([newval]) -> oldval\n\n\ |
| 2015 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 2016 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 2017 | future calls return ints. \n\ |
| 2018 | If newval is omitted, return the current setting.\n"); |
| 2019 | |
| 2020 | static PyObject* |
| 2021 | stat_float_times(PyObject* self, PyObject *args) |
| 2022 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2023 | int newval = -1; |
| 2024 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 2025 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 2026 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2027 | "stat_float_times() is deprecated", |
| 2028 | 1)) |
| 2029 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2030 | if (newval == -1) |
| 2031 | /* Return old value */ |
| 2032 | return PyBool_FromLong(_stat_float_times); |
| 2033 | _stat_float_times = newval; |
| 2034 | Py_INCREF(Py_None); |
| 2035 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2036 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2037 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2038 | static PyObject *billion = NULL; |
| 2039 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2040 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2041 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2042 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2043 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2044 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2045 | PyObject *s_in_ns = NULL; |
| 2046 | PyObject *ns_total = NULL; |
| 2047 | PyObject *float_s = NULL; |
| 2048 | |
| 2049 | if (!(s && ns_fractional)) |
| 2050 | goto exit; |
| 2051 | |
| 2052 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2053 | if (!s_in_ns) |
| 2054 | goto exit; |
| 2055 | |
| 2056 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2057 | if (!ns_total) |
| 2058 | goto exit; |
| 2059 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2060 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2061 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2062 | if (!float_s) |
| 2063 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2064 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2065 | else { |
| 2066 | float_s = s; |
| 2067 | Py_INCREF(float_s); |
| 2068 | } |
| 2069 | |
| 2070 | PyStructSequence_SET_ITEM(v, index, s); |
| 2071 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2072 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2073 | s = NULL; |
| 2074 | float_s = NULL; |
| 2075 | ns_total = NULL; |
| 2076 | exit: |
| 2077 | Py_XDECREF(s); |
| 2078 | Py_XDECREF(ns_fractional); |
| 2079 | Py_XDECREF(s_in_ns); |
| 2080 | Py_XDECREF(ns_total); |
| 2081 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2084 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2085 | (used by posix_stat() and posix_fstat()) */ |
| 2086 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2087 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2088 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2089 | unsigned long ansec, mnsec, cnsec; |
| 2090 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2091 | if (v == NULL) |
| 2092 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2093 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2094 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2095 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2096 | PyStructSequence_SET_ITEM(v, 1, |
| 2097 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2098 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2099 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2100 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2101 | #ifdef MS_WINDOWS |
| 2102 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
| 2103 | #elif defined(HAVE_LONG_LONG) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2104 | PyStructSequence_SET_ITEM(v, 2, |
| 2105 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2106 | #else |
Brian Curtin | 9cc4321 | 2013-01-01 12:31:06 -0600 | [diff] [blame] | 2107 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2108 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2109 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2110 | #if defined(MS_WINDOWS) |
| 2111 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2112 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2113 | #else |
| 2114 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2115 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2116 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2117 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2118 | PyStructSequence_SET_ITEM(v, 6, |
| 2119 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2120 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2121 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2122 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2123 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2124 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2125 | ansec = st->st_atim.tv_nsec; |
| 2126 | mnsec = st->st_mtim.tv_nsec; |
| 2127 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2128 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2129 | ansec = st->st_atimespec.tv_nsec; |
| 2130 | mnsec = st->st_mtimespec.tv_nsec; |
| 2131 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2132 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2133 | ansec = st->st_atime_nsec; |
| 2134 | mnsec = st->st_mtime_nsec; |
| 2135 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2136 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2137 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2138 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2139 | fill_time(v, 7, st->st_atime, ansec); |
| 2140 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2141 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2142 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2143 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2144 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2145 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2146 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2147 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2148 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2149 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2150 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2151 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2152 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2153 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2154 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2155 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2156 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2157 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2158 | #endif |
| 2159 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2160 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2161 | PyObject *val; |
| 2162 | unsigned long bsec,bnsec; |
| 2163 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2164 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2165 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2166 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2167 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2168 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2169 | if (_stat_float_times) { |
| 2170 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2171 | } else { |
| 2172 | val = PyLong_FromLong((long)bsec); |
| 2173 | } |
| 2174 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2175 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2176 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2177 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2178 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2179 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2180 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2181 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2182 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2183 | if (PyErr_Occurred()) { |
| 2184 | Py_DECREF(v); |
| 2185 | return NULL; |
| 2186 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2187 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2188 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2191 | /* POSIX methods */ |
| 2192 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2193 | |
| 2194 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2195 | posix_do_stat(char *function_name, path_t *path, |
| 2196 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2197 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2198 | STRUCT_STAT st; |
| 2199 | int result; |
| 2200 | |
| 2201 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2202 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2203 | return NULL; |
| 2204 | #endif |
| 2205 | |
| 2206 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2207 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2208 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2209 | return NULL; |
| 2210 | |
| 2211 | Py_BEGIN_ALLOW_THREADS |
| 2212 | if (path->fd != -1) |
| 2213 | result = FSTAT(path->fd, &st); |
| 2214 | else |
| 2215 | #ifdef MS_WINDOWS |
| 2216 | if (path->wide) { |
| 2217 | if (follow_symlinks) |
| 2218 | result = win32_stat_w(path->wide, &st); |
| 2219 | else |
| 2220 | result = win32_lstat_w(path->wide, &st); |
| 2221 | } |
| 2222 | else |
| 2223 | #endif |
| 2224 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2225 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2226 | result = LSTAT(path->narrow, &st); |
| 2227 | else |
| 2228 | #endif |
| 2229 | #ifdef HAVE_FSTATAT |
| 2230 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2231 | result = fstatat(dir_fd, path->narrow, &st, |
| 2232 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2233 | else |
| 2234 | #endif |
| 2235 | result = STAT(path->narrow, &st); |
| 2236 | Py_END_ALLOW_THREADS |
| 2237 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2238 | if (result != 0) { |
| 2239 | return path_error(path); |
| 2240 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2241 | |
| 2242 | return _pystat_fromstructstat(&st); |
| 2243 | } |
| 2244 | |
| 2245 | PyDoc_STRVAR(posix_stat__doc__, |
| 2246 | "stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result\n\n\ |
| 2247 | Perform a stat system call on the given path.\n\ |
| 2248 | \n\ |
| 2249 | path may be specified as either a string or as an open file descriptor.\n\ |
| 2250 | \n\ |
| 2251 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2252 | and path should be relative; path will then be relative to that directory.\n\ |
| 2253 | dir_fd may not be supported on your platform; if it is unavailable, using\n\ |
| 2254 | it will raise a NotImplementedError.\n\ |
| 2255 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2256 | link, stat will examine the symbolic link itself instead of the file the\n\ |
| 2257 | link points to.\n\ |
| 2258 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2259 | an open file descriptor."); |
| 2260 | |
| 2261 | static PyObject * |
| 2262 | posix_stat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2263 | { |
| 2264 | static char *keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2265 | path_t path; |
| 2266 | int dir_fd = DEFAULT_DIR_FD; |
| 2267 | int follow_symlinks = 1; |
| 2268 | PyObject *return_value; |
| 2269 | |
| 2270 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2271 | path.function_name = "stat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2272 | path.allow_fd = 1; |
| 2273 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords, |
| 2274 | path_converter, &path, |
| 2275 | #ifdef HAVE_FSTATAT |
| 2276 | dir_fd_converter, &dir_fd, |
| 2277 | #else |
| 2278 | dir_fd_unavailable, &dir_fd, |
| 2279 | #endif |
| 2280 | &follow_symlinks)) |
| 2281 | return NULL; |
| 2282 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2283 | path_cleanup(&path); |
| 2284 | return return_value; |
| 2285 | } |
| 2286 | |
| 2287 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2288 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2289 | Like stat(), but do not follow symbolic links.\n\ |
| 2290 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2291 | |
| 2292 | static PyObject * |
| 2293 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2294 | { |
| 2295 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2296 | path_t path; |
| 2297 | int dir_fd = DEFAULT_DIR_FD; |
| 2298 | int follow_symlinks = 0; |
| 2299 | PyObject *return_value; |
| 2300 | |
| 2301 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2302 | path.function_name = "lstat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2303 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2304 | path_converter, &path, |
| 2305 | #ifdef HAVE_FSTATAT |
| 2306 | dir_fd_converter, &dir_fd |
| 2307 | #else |
| 2308 | dir_fd_unavailable, &dir_fd |
| 2309 | #endif |
| 2310 | )) |
| 2311 | return NULL; |
| 2312 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2313 | path_cleanup(&path); |
| 2314 | return return_value; |
| 2315 | } |
| 2316 | |
| 2317 | PyDoc_STRVAR(posix_access__doc__, |
| 2318 | "access(path, mode, *, dir_fd=None, effective_ids=False,\ |
| 2319 | follow_symlinks=True)\n\n\ |
| 2320 | Use the real uid/gid to test for access to a path. Returns True if granted,\n\ |
| 2321 | False otherwise.\n\ |
| 2322 | \n\ |
| 2323 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2324 | and path should be relative; path will then be relative to that directory.\n\ |
| 2325 | If effective_ids is True, access will use the effective uid/gid instead of\n\ |
| 2326 | the real uid/gid.\n\ |
| 2327 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2328 | link, access will examine the symbolic link itself instead of the file the\n\ |
| 2329 | link points to.\n\ |
| 2330 | dir_fd, effective_ids, and follow_symlinks may not be implemented\n\ |
| 2331 | on your platform. If they are unavailable, using them will raise a\n\ |
| 2332 | NotImplementedError.\n\ |
| 2333 | \n\ |
| 2334 | Note that most operations will use the effective uid/gid, therefore this\n\ |
| 2335 | routine can be used in a suid/sgid environment to test if the invoking user\n\ |
| 2336 | has the specified access to the path.\n\ |
| 2337 | The mode argument can be F_OK to test existence, or the inclusive-OR\n\ |
| 2338 | of R_OK, W_OK, and X_OK."); |
| 2339 | |
| 2340 | static PyObject * |
| 2341 | posix_access(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2342 | { |
| 2343 | static char *keywords[] = {"path", "mode", "dir_fd", "effective_ids", |
| 2344 | "follow_symlinks", NULL}; |
| 2345 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2346 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2347 | int dir_fd = DEFAULT_DIR_FD; |
| 2348 | int effective_ids = 0; |
| 2349 | int follow_symlinks = 1; |
| 2350 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2351 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2352 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2353 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2354 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2355 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2356 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2357 | |
| 2358 | memset(&path, 0, sizeof(path)); |
| 2359 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", keywords, |
| 2360 | path_converter, &path, &mode, |
| 2361 | #ifdef HAVE_FACCESSAT |
| 2362 | dir_fd_converter, &dir_fd, |
| 2363 | #else |
| 2364 | dir_fd_unavailable, &dir_fd, |
| 2365 | #endif |
| 2366 | &effective_ids, &follow_symlinks)) |
| 2367 | return NULL; |
| 2368 | |
| 2369 | #ifndef HAVE_FACCESSAT |
| 2370 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2371 | goto exit; |
| 2372 | |
| 2373 | if (effective_ids) { |
| 2374 | argument_unavailable_error("access", "effective_ids"); |
| 2375 | goto exit; |
| 2376 | } |
| 2377 | #endif |
| 2378 | |
| 2379 | #ifdef MS_WINDOWS |
| 2380 | Py_BEGIN_ALLOW_THREADS |
| 2381 | if (path.wide != NULL) |
| 2382 | attr = GetFileAttributesW(path.wide); |
| 2383 | else |
| 2384 | attr = GetFileAttributesA(path.narrow); |
| 2385 | Py_END_ALLOW_THREADS |
| 2386 | |
| 2387 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2388 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2389 | * * we didn't get a -1, and |
| 2390 | * * write access wasn't requested, |
| 2391 | * * or the file isn't read-only, |
| 2392 | * * or it's a directory. |
| 2393 | * (Directories cannot be read-only on Windows.) |
| 2394 | */ |
| 2395 | return_value = PyBool_FromLong( |
| 2396 | (attr != 0xFFFFFFFF) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2397 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2398 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2399 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2400 | #else |
| 2401 | |
| 2402 | Py_BEGIN_ALLOW_THREADS |
| 2403 | #ifdef HAVE_FACCESSAT |
| 2404 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2405 | effective_ids || |
| 2406 | !follow_symlinks) { |
| 2407 | int flags = 0; |
| 2408 | if (!follow_symlinks) |
| 2409 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2410 | if (effective_ids) |
| 2411 | flags |= AT_EACCESS; |
| 2412 | result = faccessat(dir_fd, path.narrow, mode, flags); |
| 2413 | } |
| 2414 | else |
| 2415 | #endif |
| 2416 | result = access(path.narrow, mode); |
| 2417 | Py_END_ALLOW_THREADS |
| 2418 | return_value = PyBool_FromLong(!result); |
| 2419 | #endif |
| 2420 | |
| 2421 | #ifndef HAVE_FACCESSAT |
| 2422 | exit: |
| 2423 | #endif |
| 2424 | path_cleanup(&path); |
| 2425 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2428 | #ifndef F_OK |
| 2429 | #define F_OK 0 |
| 2430 | #endif |
| 2431 | #ifndef R_OK |
| 2432 | #define R_OK 4 |
| 2433 | #endif |
| 2434 | #ifndef W_OK |
| 2435 | #define W_OK 2 |
| 2436 | #endif |
| 2437 | #ifndef X_OK |
| 2438 | #define X_OK 1 |
| 2439 | #endif |
| 2440 | |
| 2441 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2442 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2443 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2444 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2445 | |
| 2446 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2447 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2448 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2449 | int id; |
| 2450 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2451 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2452 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 2453 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2454 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2455 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2456 | /* file descriptor 0 only, the default input device (stdin) */ |
| 2457 | if (id == 0) { |
| 2458 | ret = ttyname(); |
| 2459 | } |
| 2460 | else { |
| 2461 | ret = NULL; |
| 2462 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2463 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2464 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2465 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2466 | if (ret == NULL) |
| 2467 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2468 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2469 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2470 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2471 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2472 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2473 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2474 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2475 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2476 | |
| 2477 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2478 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2479 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2480 | char *ret; |
| 2481 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2482 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2483 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2484 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2485 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2486 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2487 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2488 | if (ret == NULL) |
| 2489 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2490 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2491 | } |
| 2492 | #endif |
| 2493 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2494 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2495 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2496 | Change the current working directory to the specified path.\n\ |
| 2497 | \n\ |
| 2498 | path may always be specified as a string.\n\ |
| 2499 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2500 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2501 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2502 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2503 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2504 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2505 | path_t path; |
| 2506 | int result; |
| 2507 | PyObject *return_value = NULL; |
| 2508 | static char *keywords[] = {"path", NULL}; |
| 2509 | |
| 2510 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2511 | path.function_name = "chdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2512 | #ifdef HAVE_FCHDIR |
| 2513 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2514 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2515 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2516 | path_converter, &path |
| 2517 | )) |
| 2518 | return NULL; |
| 2519 | |
| 2520 | Py_BEGIN_ALLOW_THREADS |
| 2521 | #ifdef MS_WINDOWS |
| 2522 | if (path.wide) |
| 2523 | result = win32_wchdir(path.wide); |
| 2524 | else |
| 2525 | result = win32_chdir(path.narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2526 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2527 | #else |
| 2528 | #ifdef HAVE_FCHDIR |
| 2529 | if (path.fd != -1) |
| 2530 | result = fchdir(path.fd); |
| 2531 | else |
| 2532 | #endif |
| 2533 | result = chdir(path.narrow); |
| 2534 | #endif |
| 2535 | Py_END_ALLOW_THREADS |
| 2536 | |
| 2537 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2538 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2539 | goto exit; |
| 2540 | } |
| 2541 | |
| 2542 | return_value = Py_None; |
| 2543 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2544 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2545 | exit: |
| 2546 | path_cleanup(&path); |
| 2547 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2550 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2551 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2552 | "fchdir(fd)\n\n\ |
| 2553 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2554 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2555 | |
| 2556 | static PyObject * |
| 2557 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2558 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2559 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2560 | } |
| 2561 | #endif /* HAVE_FCHDIR */ |
| 2562 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2563 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2564 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2565 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2566 | Change the access permissions of a file.\n\ |
| 2567 | \n\ |
| 2568 | path may always be specified as a string.\n\ |
| 2569 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2570 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2571 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2572 | and path should be relative; path will then be relative to that directory.\n\ |
| 2573 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2574 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2575 | link points to.\n\ |
| 2576 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2577 | an open file descriptor.\n\ |
| 2578 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2579 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2581 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2582 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2583 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2584 | path_t path; |
| 2585 | int mode; |
| 2586 | int dir_fd = DEFAULT_DIR_FD; |
| 2587 | int follow_symlinks = 1; |
| 2588 | int result; |
| 2589 | PyObject *return_value = NULL; |
| 2590 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2591 | "follow_symlinks", NULL}; |
| 2592 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2593 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2594 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2595 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2596 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2597 | #ifdef HAVE_FCHMODAT |
| 2598 | int fchmodat_nofollow_unsupported = 0; |
| 2599 | #endif |
| 2600 | |
| 2601 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2602 | path.function_name = "chmod"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2603 | #ifdef HAVE_FCHMOD |
| 2604 | path.allow_fd = 1; |
| 2605 | #endif |
| 2606 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2607 | path_converter, &path, |
| 2608 | &mode, |
| 2609 | #ifdef HAVE_FCHMODAT |
| 2610 | dir_fd_converter, &dir_fd, |
| 2611 | #else |
| 2612 | dir_fd_unavailable, &dir_fd, |
| 2613 | #endif |
| 2614 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2615 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2616 | |
| 2617 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2618 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2619 | goto exit; |
| 2620 | #endif |
| 2621 | |
| 2622 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2623 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2624 | if (path.wide) |
| 2625 | attr = GetFileAttributesW(path.wide); |
| 2626 | else |
| 2627 | attr = GetFileAttributesA(path.narrow); |
| 2628 | if (attr == 0xFFFFFFFF) |
| 2629 | result = 0; |
| 2630 | else { |
| 2631 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2632 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2633 | else |
| 2634 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2635 | if (path.wide) |
| 2636 | result = SetFileAttributesW(path.wide, attr); |
| 2637 | else |
| 2638 | result = SetFileAttributesA(path.narrow, attr); |
| 2639 | } |
| 2640 | Py_END_ALLOW_THREADS |
| 2641 | |
| 2642 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 2643 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2644 | goto exit; |
| 2645 | } |
| 2646 | #else /* MS_WINDOWS */ |
| 2647 | Py_BEGIN_ALLOW_THREADS |
| 2648 | #ifdef HAVE_FCHMOD |
| 2649 | if (path.fd != -1) |
| 2650 | result = fchmod(path.fd, mode); |
| 2651 | else |
| 2652 | #endif |
| 2653 | #ifdef HAVE_LCHMOD |
| 2654 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2655 | result = lchmod(path.narrow, mode); |
| 2656 | else |
| 2657 | #endif |
| 2658 | #ifdef HAVE_FCHMODAT |
| 2659 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2660 | /* |
| 2661 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2662 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2663 | * and then says it isn't implemented yet. |
| 2664 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2665 | * |
| 2666 | * Once it is supported, os.chmod will automatically |
| 2667 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2668 | * Until then, we need to be careful what exception we raise. |
| 2669 | */ |
| 2670 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2671 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2672 | /* |
| 2673 | * But wait! We can't throw the exception without allowing threads, |
| 2674 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2675 | */ |
| 2676 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2677 | result && |
| 2678 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2679 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2680 | } |
| 2681 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2682 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2683 | result = chmod(path.narrow, mode); |
| 2684 | Py_END_ALLOW_THREADS |
| 2685 | |
| 2686 | if (result) { |
| 2687 | #ifdef HAVE_FCHMODAT |
| 2688 | if (fchmodat_nofollow_unsupported) { |
| 2689 | if (dir_fd != DEFAULT_DIR_FD) |
| 2690 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2691 | dir_fd, follow_symlinks); |
| 2692 | else |
| 2693 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2694 | } |
| 2695 | else |
| 2696 | #endif |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2697 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2698 | goto exit; |
| 2699 | } |
| 2700 | #endif |
| 2701 | |
| 2702 | Py_INCREF(Py_None); |
| 2703 | return_value = Py_None; |
| 2704 | exit: |
| 2705 | path_cleanup(&path); |
| 2706 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2709 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2710 | #ifdef HAVE_FCHMOD |
| 2711 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2712 | "fchmod(fd, mode)\n\n\ |
| 2713 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2714 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2715 | |
| 2716 | static PyObject * |
| 2717 | posix_fchmod(PyObject *self, PyObject *args) |
| 2718 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2719 | int fd, mode, res; |
| 2720 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2721 | return NULL; |
| 2722 | Py_BEGIN_ALLOW_THREADS |
| 2723 | res = fchmod(fd, mode); |
| 2724 | Py_END_ALLOW_THREADS |
| 2725 | if (res < 0) |
| 2726 | return posix_error(); |
| 2727 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2728 | } |
| 2729 | #endif /* HAVE_FCHMOD */ |
| 2730 | |
| 2731 | #ifdef HAVE_LCHMOD |
| 2732 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2733 | "lchmod(path, mode)\n\n\ |
| 2734 | Change the access permissions of a file. If path is a symlink, this\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2735 | affects the link itself rather than the target.\n\ |
| 2736 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2737 | |
| 2738 | static PyObject * |
| 2739 | posix_lchmod(PyObject *self, PyObject *args) |
| 2740 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2741 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2742 | int i; |
| 2743 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2744 | memset(&path, 0, sizeof(path)); |
| 2745 | path.function_name = "lchmod"; |
| 2746 | if (!PyArg_ParseTuple(args, "O&i:lchmod", |
| 2747 | path_converter, &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2748 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2749 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2750 | res = lchmod(path.narrow, i); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2751 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2752 | if (res < 0) { |
| 2753 | path_error(&path); |
| 2754 | path_cleanup(&path); |
| 2755 | return NULL; |
| 2756 | } |
| 2757 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2758 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2759 | } |
| 2760 | #endif /* HAVE_LCHMOD */ |
| 2761 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2762 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2763 | #ifdef HAVE_CHFLAGS |
| 2764 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2765 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 2766 | Set file flags.\n\ |
| 2767 | \n\ |
| 2768 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2769 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 2770 | file the link points to.\n\ |
| 2771 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 2772 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2773 | |
| 2774 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2775 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2776 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2777 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2778 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2779 | int follow_symlinks = 1; |
| 2780 | int result; |
| 2781 | PyObject *return_value; |
| 2782 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 2783 | |
| 2784 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2785 | path.function_name = "chflags"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2786 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 2787 | path_converter, &path, |
| 2788 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2789 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2790 | |
| 2791 | #ifndef HAVE_LCHFLAGS |
| 2792 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 2793 | goto exit; |
| 2794 | #endif |
| 2795 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2796 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2797 | #ifdef HAVE_LCHFLAGS |
| 2798 | if (!follow_symlinks) |
| 2799 | result = lchflags(path.narrow, flags); |
| 2800 | else |
| 2801 | #endif |
| 2802 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2803 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2804 | |
| 2805 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2806 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2807 | goto exit; |
| 2808 | } |
| 2809 | |
| 2810 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2811 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2812 | |
| 2813 | exit: |
| 2814 | path_cleanup(&path); |
| 2815 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2816 | } |
| 2817 | #endif /* HAVE_CHFLAGS */ |
| 2818 | |
| 2819 | #ifdef HAVE_LCHFLAGS |
| 2820 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2821 | "lchflags(path, flags)\n\n\ |
| 2822 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2823 | This function will not follow symbolic links.\n\ |
| 2824 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2825 | |
| 2826 | static PyObject * |
| 2827 | posix_lchflags(PyObject *self, PyObject *args) |
| 2828 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2829 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2830 | unsigned long flags; |
| 2831 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2832 | memset(&path, 0, sizeof(path)); |
| 2833 | path.function_name = "lchflags"; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2834 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2835 | path_converter, &path, &flags)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2836 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2837 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2838 | res = lchflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2839 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2840 | if (res < 0) { |
| 2841 | path_error(&path); |
| 2842 | path_cleanup(&path); |
| 2843 | return NULL; |
| 2844 | } |
| 2845 | path_cleanup(&path); |
| 2846 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2847 | } |
| 2848 | #endif /* HAVE_LCHFLAGS */ |
| 2849 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2850 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2851 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2852 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2853 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2854 | |
| 2855 | static PyObject * |
| 2856 | posix_chroot(PyObject *self, PyObject *args) |
| 2857 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2858 | return posix_1str("chroot", args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2859 | } |
| 2860 | #endif |
| 2861 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2862 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2863 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2864 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2865 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2866 | |
| 2867 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2868 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2869 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2870 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2871 | } |
| 2872 | #endif /* HAVE_FSYNC */ |
| 2873 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2874 | #ifdef HAVE_SYNC |
| 2875 | PyDoc_STRVAR(posix_sync__doc__, |
| 2876 | "sync()\n\n\ |
| 2877 | Force write of everything to disk."); |
| 2878 | |
| 2879 | static PyObject * |
| 2880 | posix_sync(PyObject *self, PyObject *noargs) |
| 2881 | { |
| 2882 | Py_BEGIN_ALLOW_THREADS |
| 2883 | sync(); |
| 2884 | Py_END_ALLOW_THREADS |
| 2885 | Py_RETURN_NONE; |
| 2886 | } |
| 2887 | #endif |
| 2888 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2889 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2890 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2891 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2892 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2893 | #endif |
| 2894 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2895 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2896 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2897 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2898 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2899 | |
| 2900 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2901 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2902 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2903 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2904 | } |
| 2905 | #endif /* HAVE_FDATASYNC */ |
| 2906 | |
| 2907 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2908 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2909 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2910 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2911 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2912 | \n\ |
| 2913 | path may always be specified as a string.\n\ |
| 2914 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2915 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2916 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2917 | and path should be relative; path will then be relative to that directory.\n\ |
| 2918 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2919 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 2920 | link points to.\n\ |
| 2921 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2922 | an open file descriptor.\n\ |
| 2923 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2924 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2925 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2926 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2927 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2928 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2929 | path_t path; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2930 | uid_t uid; |
| 2931 | gid_t gid; |
| 2932 | int dir_fd = DEFAULT_DIR_FD; |
| 2933 | int follow_symlinks = 1; |
| 2934 | int result; |
| 2935 | PyObject *return_value = NULL; |
| 2936 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 2937 | "follow_symlinks", NULL}; |
| 2938 | |
| 2939 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2940 | path.function_name = "chown"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2941 | #ifdef HAVE_FCHOWN |
| 2942 | path.allow_fd = 1; |
| 2943 | #endif |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2944 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&|$O&p:chown", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2945 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2946 | _Py_Uid_Converter, &uid, |
| 2947 | _Py_Gid_Converter, &gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2948 | #ifdef HAVE_FCHOWNAT |
| 2949 | dir_fd_converter, &dir_fd, |
| 2950 | #else |
| 2951 | dir_fd_unavailable, &dir_fd, |
| 2952 | #endif |
| 2953 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2954 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2955 | |
| 2956 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 2957 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 2958 | goto exit; |
| 2959 | #endif |
| 2960 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 2961 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 2962 | goto exit; |
| 2963 | |
| 2964 | #ifdef __APPLE__ |
| 2965 | /* |
| 2966 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 2967 | * (But we still have an lchown symbol because of weak-linking.) |
| 2968 | * It doesn't have fchownat either. So there's no possibility |
| 2969 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2970 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2971 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 2972 | follow_symlinks_specified("chown", follow_symlinks); |
| 2973 | goto exit; |
| 2974 | } |
| 2975 | #endif |
| 2976 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2977 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2978 | #ifdef HAVE_FCHOWN |
| 2979 | if (path.fd != -1) |
| 2980 | result = fchown(path.fd, uid, gid); |
| 2981 | else |
| 2982 | #endif |
| 2983 | #ifdef HAVE_LCHOWN |
| 2984 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2985 | result = lchown(path.narrow, uid, gid); |
| 2986 | else |
| 2987 | #endif |
| 2988 | #ifdef HAVE_FCHOWNAT |
| 2989 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 2990 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 2991 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2992 | else |
| 2993 | #endif |
| 2994 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2995 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2996 | |
| 2997 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2998 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2999 | goto exit; |
| 3000 | } |
| 3001 | |
| 3002 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3003 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3004 | |
| 3005 | exit: |
| 3006 | path_cleanup(&path); |
| 3007 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3008 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3009 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3010 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3011 | #ifdef HAVE_FCHOWN |
| 3012 | PyDoc_STRVAR(posix_fchown__doc__, |
| 3013 | "fchown(fd, uid, gid)\n\n\ |
| 3014 | Change the owner and group id of the file given by file descriptor\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3015 | fd to the numeric uid and gid. Equivalent to os.chown(fd, uid, gid)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3016 | |
| 3017 | static PyObject * |
| 3018 | posix_fchown(PyObject *self, PyObject *args) |
| 3019 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3020 | int fd; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3021 | uid_t uid; |
| 3022 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3023 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3024 | if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd, |
| 3025 | _Py_Uid_Converter, &uid, |
| 3026 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3027 | return NULL; |
| 3028 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3029 | res = fchown(fd, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3030 | Py_END_ALLOW_THREADS |
| 3031 | if (res < 0) |
| 3032 | return posix_error(); |
| 3033 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3034 | } |
| 3035 | #endif /* HAVE_FCHOWN */ |
| 3036 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3037 | #ifdef HAVE_LCHOWN |
| 3038 | PyDoc_STRVAR(posix_lchown__doc__, |
| 3039 | "lchown(path, uid, gid)\n\n\ |
| 3040 | Change the owner and group id of path to the numeric uid and gid.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3041 | This function will not follow symbolic links.\n\ |
| 3042 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3043 | |
| 3044 | static PyObject * |
| 3045 | posix_lchown(PyObject *self, PyObject *args) |
| 3046 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3047 | path_t path; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3048 | uid_t uid; |
| 3049 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3050 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3051 | memset(&path, 0, sizeof(path)); |
| 3052 | path.function_name = "lchown"; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3053 | if (!PyArg_ParseTuple(args, "O&O&O&:lchown", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3054 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3055 | _Py_Uid_Converter, &uid, |
| 3056 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3057 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3058 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | c2d0200 | 2013-02-10 22:03:08 +0200 | [diff] [blame] | 3059 | res = lchown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3060 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3061 | if (res < 0) { |
| 3062 | path_error(&path); |
| 3063 | path_cleanup(&path); |
| 3064 | return NULL; |
| 3065 | } |
| 3066 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3067 | Py_INCREF(Py_None); |
| 3068 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3069 | } |
| 3070 | #endif /* HAVE_LCHOWN */ |
| 3071 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3072 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3073 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3074 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3075 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3076 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3077 | char buf[1026]; |
| 3078 | char *res; |
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 | if (!use_bytes) { |
| 3082 | wchar_t wbuf[1026]; |
| 3083 | wchar_t *wbuf2 = wbuf; |
| 3084 | PyObject *resobj; |
| 3085 | DWORD len; |
| 3086 | Py_BEGIN_ALLOW_THREADS |
| 3087 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 3088 | /* If the buffer is large enough, len does not include the |
| 3089 | terminating \0. If the buffer is too small, len includes |
| 3090 | the space needed for the terminator. */ |
| 3091 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 3092 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 3093 | if (wbuf2) |
| 3094 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3095 | } |
| 3096 | Py_END_ALLOW_THREADS |
| 3097 | if (!wbuf2) { |
| 3098 | PyErr_NoMemory(); |
| 3099 | return NULL; |
| 3100 | } |
| 3101 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3102 | if (wbuf2 != wbuf) |
| 3103 | free(wbuf2); |
| 3104 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3105 | } |
| 3106 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3107 | if (wbuf2 != wbuf) |
| 3108 | free(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3109 | return resobj; |
| 3110 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3111 | |
| 3112 | if (win32_warn_bytes_api()) |
| 3113 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3114 | #endif |
| 3115 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3116 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3117 | res = getcwd(buf, sizeof buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3118 | Py_END_ALLOW_THREADS |
| 3119 | if (res == NULL) |
| 3120 | return posix_error(); |
| 3121 | if (use_bytes) |
| 3122 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3123 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3124 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3125 | |
| 3126 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 3127 | "getcwd() -> path\n\n\ |
| 3128 | Return a unicode string representing the current working directory."); |
| 3129 | |
| 3130 | static PyObject * |
| 3131 | posix_getcwd_unicode(PyObject *self) |
| 3132 | { |
| 3133 | return posix_getcwd(0); |
| 3134 | } |
| 3135 | |
| 3136 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 3137 | "getcwdb() -> path\n\n\ |
| 3138 | Return a bytes string representing the current working directory."); |
| 3139 | |
| 3140 | static PyObject * |
| 3141 | posix_getcwd_bytes(PyObject *self) |
| 3142 | { |
| 3143 | return posix_getcwd(1); |
| 3144 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3145 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3146 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3147 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3148 | #define HAVE_LINK 1 |
| 3149 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3150 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3151 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3152 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3153 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 3154 | Create a hard link to a file.\n\ |
| 3155 | \n\ |
| 3156 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3157 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3158 | should be relative; the path will then be relative to that directory.\n\ |
| 3159 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3160 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3161 | file the link points to.\n\ |
| 3162 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3163 | platform. If they are unavailable, using them will raise a\n\ |
| 3164 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3165 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3166 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3167 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3168 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3169 | path_t src, dst; |
| 3170 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3171 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3172 | int follow_symlinks = 1; |
| 3173 | PyObject *return_value = NULL; |
| 3174 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3175 | "follow_symlinks", NULL}; |
| 3176 | #ifdef MS_WINDOWS |
| 3177 | BOOL result; |
| 3178 | #else |
| 3179 | int result; |
| 3180 | #endif |
| 3181 | |
| 3182 | memset(&src, 0, sizeof(src)); |
| 3183 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3184 | src.function_name = "link"; |
| 3185 | dst.function_name = "link"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3186 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3187 | path_converter, &src, |
| 3188 | path_converter, &dst, |
| 3189 | dir_fd_converter, &src_dir_fd, |
| 3190 | dir_fd_converter, &dst_dir_fd, |
| 3191 | &follow_symlinks)) |
| 3192 | return NULL; |
| 3193 | |
| 3194 | #ifndef HAVE_LINKAT |
| 3195 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3196 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3197 | goto exit; |
| 3198 | } |
| 3199 | #endif |
| 3200 | |
| 3201 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3202 | PyErr_SetString(PyExc_NotImplementedError, |
| 3203 | "link: src and dst must be the same type"); |
| 3204 | goto exit; |
| 3205 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3206 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3207 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3208 | Py_BEGIN_ALLOW_THREADS |
| 3209 | if (src.wide) |
| 3210 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3211 | else |
| 3212 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3213 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3214 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3215 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 3216 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3217 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3218 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3219 | #else |
| 3220 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3221 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3222 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3223 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3224 | (!follow_symlinks)) |
| 3225 | result = linkat(src_dir_fd, src.narrow, |
| 3226 | dst_dir_fd, dst.narrow, |
| 3227 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3228 | else |
| 3229 | #endif |
| 3230 | result = link(src.narrow, dst.narrow); |
| 3231 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3232 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3233 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3234 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3235 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3236 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3237 | #endif |
| 3238 | |
| 3239 | return_value = Py_None; |
| 3240 | Py_INCREF(Py_None); |
| 3241 | |
| 3242 | exit: |
| 3243 | path_cleanup(&src); |
| 3244 | path_cleanup(&dst); |
| 3245 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3246 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3247 | #endif |
| 3248 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3249 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3251 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3252 | "listdir(path='.') -> list_of_filenames\n\n\ |
| 3253 | Return a list containing the names of the files in the directory.\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3254 | The list is in arbitrary order. It does not include the special\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3255 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3256 | \n\ |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3257 | path can be specified as either str or bytes. If path is bytes,\n\ |
| 3258 | the filenames returned will also be bytes; in all other circumstances\n\ |
| 3259 | the filenames returned will be str.\n\ |
| 3260 | On some platforms, path may also be specified as an open file descriptor;\n\ |
| 3261 | the file descriptor must refer to a directory.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3262 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3263 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3264 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3265 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3266 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3267 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3268 | static char *keywords[] = {"path", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3269 | PyObject *v; |
| 3270 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3271 | BOOL result; |
| 3272 | WIN32_FIND_DATA FileData; |
| 3273 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 3274 | char *bufptr = namebuf; |
| 3275 | /* only claim to have space for MAX_PATH */ |
| 3276 | Py_ssize_t len = sizeof(namebuf)-5; |
| 3277 | PyObject *po = NULL; |
| 3278 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3279 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3280 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3281 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3282 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3283 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3284 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3285 | po_wchars = L"."; |
| 3286 | len = 1; |
| 3287 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3288 | po_wchars = path->wide; |
| 3289 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3290 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3291 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3292 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 3293 | if (!wnamebuf) { |
| 3294 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3295 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3296 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3297 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3298 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3299 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3300 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 3301 | wnamebuf[len++] = L'\\'; |
| 3302 | wcscpy(wnamebuf + len, L"*.*"); |
| 3303 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3304 | if ((list = PyList_New(0)) == NULL) { |
| 3305 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3306 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3307 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3308 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3309 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3310 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3311 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3312 | if (error == ERROR_FILE_NOT_FOUND) |
| 3313 | goto exit; |
| 3314 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3315 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3316 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3317 | } |
| 3318 | do { |
| 3319 | /* Skip over . and .. */ |
| 3320 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3321 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3322 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3323 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3324 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3325 | Py_DECREF(list); |
| 3326 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3327 | break; |
| 3328 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3329 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3330 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3331 | Py_DECREF(list); |
| 3332 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3333 | break; |
| 3334 | } |
| 3335 | Py_DECREF(v); |
| 3336 | } |
| 3337 | Py_BEGIN_ALLOW_THREADS |
| 3338 | result = FindNextFileW(hFindFile, &wFileData); |
| 3339 | Py_END_ALLOW_THREADS |
| 3340 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3341 | it got to the end of the directory. */ |
| 3342 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3343 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3344 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3345 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3346 | } |
| 3347 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3348 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3349 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3350 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3351 | strcpy(namebuf, path->narrow); |
| 3352 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3353 | if (len > 0) { |
| 3354 | char ch = namebuf[len-1]; |
| 3355 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 3356 | namebuf[len++] = '/'; |
| 3357 | strcpy(namebuf + len, "*.*"); |
| 3358 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3359 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3360 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3361 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3362 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3363 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3364 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3365 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3366 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3367 | int error = GetLastError(); |
| 3368 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3369 | goto exit; |
| 3370 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3371 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3372 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3373 | } |
| 3374 | do { |
| 3375 | /* Skip over . and .. */ |
| 3376 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3377 | strcmp(FileData.cFileName, "..") != 0) { |
| 3378 | v = PyBytes_FromString(FileData.cFileName); |
| 3379 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3380 | Py_DECREF(list); |
| 3381 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3382 | break; |
| 3383 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3384 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3385 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3386 | Py_DECREF(list); |
| 3387 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3388 | break; |
| 3389 | } |
| 3390 | Py_DECREF(v); |
| 3391 | } |
| 3392 | Py_BEGIN_ALLOW_THREADS |
| 3393 | result = FindNextFile(hFindFile, &FileData); |
| 3394 | Py_END_ALLOW_THREADS |
| 3395 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3396 | it got to the end of the directory. */ |
| 3397 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3398 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3399 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3400 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3401 | } |
| 3402 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3403 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3404 | exit: |
| 3405 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3406 | if (FindClose(hFindFile) == FALSE) { |
| 3407 | if (list != NULL) { |
| 3408 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3409 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3410 | } |
| 3411 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3412 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3413 | if (wnamebuf) |
| 3414 | free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3415 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3416 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3417 | } /* end of _listdir_windows_no_opendir */ |
| 3418 | |
| 3419 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3420 | |
| 3421 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3422 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3423 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3424 | int fd = -1; |
| 3425 | |
| 3426 | PyObject *v; |
| 3427 | DIR *dirp = NULL; |
| 3428 | struct dirent *ep; |
| 3429 | int return_str; /* if false, return bytes */ |
| 3430 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3431 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3432 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3433 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3434 | /* closedir() closes the FD, so we duplicate it */ |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3435 | Py_BEGIN_ALLOW_THREADS |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3436 | fd = dup(path->fd); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3437 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3438 | |
| 3439 | if (fd == -1) { |
| 3440 | list = posix_error(); |
| 3441 | goto exit; |
| 3442 | } |
| 3443 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3444 | return_str = 1; |
| 3445 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3446 | Py_BEGIN_ALLOW_THREADS |
| 3447 | dirp = fdopendir(fd); |
| 3448 | Py_END_ALLOW_THREADS |
| 3449 | } |
| 3450 | else |
| 3451 | #endif |
| 3452 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3453 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3454 | if (path->narrow) { |
| 3455 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3456 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3457 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3458 | } |
| 3459 | else { |
| 3460 | name = "."; |
| 3461 | return_str = 1; |
| 3462 | } |
| 3463 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3464 | Py_BEGIN_ALLOW_THREADS |
| 3465 | dirp = opendir(name); |
| 3466 | Py_END_ALLOW_THREADS |
| 3467 | } |
| 3468 | |
| 3469 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3470 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3471 | goto exit; |
| 3472 | } |
| 3473 | if ((list = PyList_New(0)) == NULL) { |
| 3474 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3475 | } |
| 3476 | for (;;) { |
| 3477 | errno = 0; |
| 3478 | Py_BEGIN_ALLOW_THREADS |
| 3479 | ep = readdir(dirp); |
| 3480 | Py_END_ALLOW_THREADS |
| 3481 | if (ep == NULL) { |
| 3482 | if (errno == 0) { |
| 3483 | break; |
| 3484 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3485 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3486 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3487 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3488 | } |
| 3489 | } |
| 3490 | if (ep->d_name[0] == '.' && |
| 3491 | (NAMLEN(ep) == 1 || |
| 3492 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3493 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3494 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3495 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3496 | else |
| 3497 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3498 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3499 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3500 | break; |
| 3501 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3502 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3503 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3504 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3505 | break; |
| 3506 | } |
| 3507 | Py_DECREF(v); |
| 3508 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3509 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3510 | exit: |
| 3511 | if (dirp != NULL) { |
| 3512 | Py_BEGIN_ALLOW_THREADS |
| 3513 | if (fd > -1) |
| 3514 | rewinddir(dirp); |
| 3515 | closedir(dirp); |
| 3516 | Py_END_ALLOW_THREADS |
| 3517 | } |
| 3518 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3519 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3520 | } /* end of _posix_listdir */ |
| 3521 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3522 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3523 | static PyObject * |
| 3524 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3525 | { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3526 | path_t path; |
| 3527 | PyObject *list = NULL; |
| 3528 | static char *keywords[] = {"path", NULL}; |
| 3529 | PyObject *return_value; |
| 3530 | |
| 3531 | memset(&path, 0, sizeof(path)); |
| 3532 | path.function_name = "listdir"; |
| 3533 | path.nullable = 1; |
| 3534 | #ifdef HAVE_FDOPENDIR |
| 3535 | path.allow_fd = 1; |
| 3536 | path.fd = -1; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3537 | #endif |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3538 | |
| 3539 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3540 | path_converter, &path)) { |
| 3541 | return NULL; |
| 3542 | } |
| 3543 | |
| 3544 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3545 | return_value = _listdir_windows_no_opendir(&path, list); |
| 3546 | #else |
| 3547 | return_value = _posix_listdir(&path, list); |
| 3548 | #endif |
| 3549 | path_cleanup(&path); |
| 3550 | return return_value; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3551 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3552 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3553 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3554 | /* A helper function for abspath on win32 */ |
| 3555 | static PyObject * |
| 3556 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3557 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3558 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3559 | char outbuf[MAX_PATH*2]; |
| 3560 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3561 | PyObject *po; |
| 3562 | |
| 3563 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3564 | { |
| 3565 | wchar_t *wpath; |
| 3566 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 3567 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3568 | DWORD result; |
| 3569 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3570 | |
| 3571 | wpath = PyUnicode_AsUnicode(po); |
| 3572 | if (wpath == NULL) |
| 3573 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3574 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3575 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3576 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3577 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3578 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3579 | if (!woutbufp) |
| 3580 | return PyErr_NoMemory(); |
| 3581 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3582 | } |
| 3583 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3584 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3585 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3586 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3587 | if (woutbufp != woutbuf) |
| 3588 | free(woutbufp); |
| 3589 | return v; |
| 3590 | } |
| 3591 | /* Drop the argument parsing error as narrow strings |
| 3592 | are also valid. */ |
| 3593 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3594 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3595 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3596 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3597 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3598 | if (win32_warn_bytes_api()) |
| 3599 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3600 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3601 | outbuf, &temp)) { |
| 3602 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3603 | return NULL; |
| 3604 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3605 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3606 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3607 | Py_FileSystemDefaultEncoding, NULL); |
| 3608 | } |
| 3609 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3610 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3611 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3612 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3613 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3614 | /* A helper function for samepath on windows */ |
| 3615 | static PyObject * |
| 3616 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3617 | { |
| 3618 | HANDLE hFile; |
| 3619 | int buf_size; |
| 3620 | wchar_t *target_path; |
| 3621 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3622 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3623 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3624 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3625 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3626 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3627 | path = PyUnicode_AsUnicode(po); |
| 3628 | if (path == NULL) |
| 3629 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3630 | |
| 3631 | if(!check_GetFinalPathNameByHandle()) { |
| 3632 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3633 | NotImplementedError. */ |
| 3634 | return PyErr_Format(PyExc_NotImplementedError, |
| 3635 | "GetFinalPathNameByHandle not available on this platform"); |
| 3636 | } |
| 3637 | |
| 3638 | hFile = CreateFileW( |
| 3639 | path, |
| 3640 | 0, /* desired access */ |
| 3641 | 0, /* share mode */ |
| 3642 | NULL, /* security attributes */ |
| 3643 | OPEN_EXISTING, |
| 3644 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3645 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3646 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3647 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3648 | if(hFile == INVALID_HANDLE_VALUE) |
| 3649 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3650 | |
| 3651 | /* We have a good handle to the target, use it to determine the |
| 3652 | target path name. */ |
| 3653 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3654 | |
| 3655 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3656 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3657 | |
| 3658 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3659 | if(!target_path) |
| 3660 | return PyErr_NoMemory(); |
| 3661 | |
| 3662 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3663 | buf_size, VOLUME_NAME_DOS); |
| 3664 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3665 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3666 | |
| 3667 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3668 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3669 | |
| 3670 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3671 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3672 | free(target_path); |
| 3673 | return result; |
| 3674 | |
| 3675 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3676 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3677 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3678 | "Return true if the pathname refers to an existing directory."); |
| 3679 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3680 | static PyObject * |
| 3681 | posix__isdir(PyObject *self, PyObject *args) |
| 3682 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3683 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3684 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3685 | DWORD attributes; |
| 3686 | |
| 3687 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3688 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3689 | if (wpath == NULL) |
| 3690 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3691 | |
| 3692 | attributes = GetFileAttributesW(wpath); |
| 3693 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3694 | Py_RETURN_FALSE; |
| 3695 | goto check; |
| 3696 | } |
| 3697 | /* Drop the argument parsing error as narrow strings |
| 3698 | are also valid. */ |
| 3699 | PyErr_Clear(); |
| 3700 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3701 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3702 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3703 | if (win32_warn_bytes_api()) |
| 3704 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3705 | attributes = GetFileAttributesA(path); |
| 3706 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3707 | Py_RETURN_FALSE; |
| 3708 | |
| 3709 | check: |
| 3710 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3711 | Py_RETURN_TRUE; |
| 3712 | else |
| 3713 | Py_RETURN_FALSE; |
| 3714 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3715 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3717 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3718 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3719 | Create a directory.\n\ |
| 3720 | \n\ |
| 3721 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3722 | and path should be relative; path will then be relative to that directory.\n\ |
| 3723 | dir_fd may not be implemented on your platform.\n\ |
| 3724 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 3725 | \n\ |
| 3726 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3727 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3728 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3729 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3730 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3731 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3732 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3733 | int dir_fd = DEFAULT_DIR_FD; |
| 3734 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 3735 | PyObject *return_value = NULL; |
| 3736 | int result; |
| 3737 | |
| 3738 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3739 | path.function_name = "mkdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3740 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 3741 | path_converter, &path, &mode, |
| 3742 | #ifdef HAVE_MKDIRAT |
| 3743 | dir_fd_converter, &dir_fd |
| 3744 | #else |
| 3745 | dir_fd_unavailable, &dir_fd |
| 3746 | #endif |
| 3747 | )) |
| 3748 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3749 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3750 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3751 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3752 | if (path.wide) |
| 3753 | result = CreateDirectoryW(path.wide, NULL); |
| 3754 | else |
| 3755 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3756 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3757 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3758 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3759 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3760 | goto exit; |
| 3761 | } |
| 3762 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3763 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3764 | #if HAVE_MKDIRAT |
| 3765 | if (dir_fd != DEFAULT_DIR_FD) |
| 3766 | result = mkdirat(dir_fd, path.narrow, mode); |
| 3767 | else |
| 3768 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3769 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3770 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3771 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3772 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3773 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3774 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3775 | if (result < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3776 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3777 | goto exit; |
| 3778 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3779 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3780 | return_value = Py_None; |
| 3781 | Py_INCREF(Py_None); |
| 3782 | exit: |
| 3783 | path_cleanup(&path); |
| 3784 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3787 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3788 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3789 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3790 | #include <sys/resource.h> |
| 3791 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3792 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3793 | |
| 3794 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3795 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3796 | "nice(inc) -> new_priority\n\n\ |
| 3797 | 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] | 3798 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3799 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3800 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3801 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3802 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3803 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3804 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3805 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3807 | /* There are two flavours of 'nice': one that returns the new |
| 3808 | priority (as required by almost all standards out there) and the |
| 3809 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3810 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3811 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3812 | If we are of the nice family that returns the new priority, we |
| 3813 | need to clear errno before the call, and check if errno is filled |
| 3814 | before calling posix_error() on a returnvalue of -1, because the |
| 3815 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3816 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3817 | errno = 0; |
| 3818 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3819 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3820 | if (value == 0) |
| 3821 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3822 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3823 | if (value == -1 && errno != 0) |
| 3824 | /* either nice() or getpriority() returned an error */ |
| 3825 | return posix_error(); |
| 3826 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3827 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3828 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3829 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3830 | |
| 3831 | #ifdef HAVE_GETPRIORITY |
| 3832 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3833 | "getpriority(which, who) -> current_priority\n\n\ |
| 3834 | Get program scheduling priority."); |
| 3835 | |
| 3836 | static PyObject * |
| 3837 | posix_getpriority(PyObject *self, PyObject *args) |
| 3838 | { |
| 3839 | int which, who, retval; |
| 3840 | |
| 3841 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3842 | return NULL; |
| 3843 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3844 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3845 | if (errno != 0) |
| 3846 | return posix_error(); |
| 3847 | return PyLong_FromLong((long)retval); |
| 3848 | } |
| 3849 | #endif /* HAVE_GETPRIORITY */ |
| 3850 | |
| 3851 | |
| 3852 | #ifdef HAVE_SETPRIORITY |
| 3853 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3854 | "setpriority(which, who, prio) -> None\n\n\ |
| 3855 | Set program scheduling priority."); |
| 3856 | |
| 3857 | static PyObject * |
| 3858 | posix_setpriority(PyObject *self, PyObject *args) |
| 3859 | { |
| 3860 | int which, who, prio, retval; |
| 3861 | |
| 3862 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3863 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3864 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3865 | if (retval == -1) |
| 3866 | return posix_error(); |
| 3867 | Py_RETURN_NONE; |
| 3868 | } |
| 3869 | #endif /* HAVE_SETPRIORITY */ |
| 3870 | |
| 3871 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3872 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3873 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3874 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3875 | char *function_name = is_replace ? "replace" : "rename"; |
| 3876 | path_t src; |
| 3877 | path_t dst; |
| 3878 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3879 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3880 | int dir_fd_specified; |
| 3881 | PyObject *return_value = NULL; |
| 3882 | char format[24]; |
| 3883 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 3884 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3885 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3886 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3887 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3888 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3889 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3890 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3891 | |
| 3892 | memset(&src, 0, sizeof(src)); |
| 3893 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3894 | src.function_name = function_name; |
| 3895 | dst.function_name = function_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3896 | strcpy(format, "O&O&|$O&O&:"); |
| 3897 | strcat(format, function_name); |
| 3898 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 3899 | path_converter, &src, |
| 3900 | path_converter, &dst, |
| 3901 | dir_fd_converter, &src_dir_fd, |
| 3902 | dir_fd_converter, &dst_dir_fd)) |
| 3903 | return NULL; |
| 3904 | |
| 3905 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 3906 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 3907 | #ifndef HAVE_RENAMEAT |
| 3908 | if (dir_fd_specified) { |
| 3909 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 3910 | goto exit; |
| 3911 | } |
| 3912 | #endif |
| 3913 | |
| 3914 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3915 | PyErr_Format(PyExc_ValueError, |
| 3916 | "%s: src and dst must be the same type", function_name); |
| 3917 | goto exit; |
| 3918 | } |
| 3919 | |
| 3920 | #ifdef MS_WINDOWS |
| 3921 | Py_BEGIN_ALLOW_THREADS |
| 3922 | if (src.wide) |
| 3923 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 3924 | else |
| 3925 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 3926 | Py_END_ALLOW_THREADS |
| 3927 | |
| 3928 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 3929 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3930 | goto exit; |
| 3931 | } |
| 3932 | |
| 3933 | #else |
| 3934 | Py_BEGIN_ALLOW_THREADS |
| 3935 | #ifdef HAVE_RENAMEAT |
| 3936 | if (dir_fd_specified) |
| 3937 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 3938 | else |
| 3939 | #endif |
| 3940 | result = rename(src.narrow, dst.narrow); |
| 3941 | Py_END_ALLOW_THREADS |
| 3942 | |
| 3943 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3944 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3945 | goto exit; |
| 3946 | } |
| 3947 | #endif |
| 3948 | |
| 3949 | Py_INCREF(Py_None); |
| 3950 | return_value = Py_None; |
| 3951 | exit: |
| 3952 | path_cleanup(&src); |
| 3953 | path_cleanup(&dst); |
| 3954 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3957 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3958 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 3959 | Rename a file or directory.\n\ |
| 3960 | \n\ |
| 3961 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3962 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3963 | should be relative; the path will then be relative to that directory.\n\ |
| 3964 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 3965 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3966 | |
| 3967 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3968 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3969 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3970 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3974 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 3975 | Rename a file or directory, overwriting the destination.\n\ |
| 3976 | \n\ |
| 3977 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3978 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3979 | should be relative; the path will then be relative to that directory.\n\ |
| 3980 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 3981 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3982 | |
| 3983 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3984 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3985 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3986 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3987 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3988 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3989 | PyDoc_STRVAR(posix_rmdir__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3990 | "rmdir(path, *, dir_fd=None)\n\n\ |
| 3991 | Remove a directory.\n\ |
| 3992 | \n\ |
| 3993 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3994 | and path should be relative; path will then be relative to that directory.\n\ |
| 3995 | dir_fd may not be implemented on your platform.\n\ |
| 3996 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3997 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3998 | static PyObject * |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3999 | posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4000 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4001 | path_t path; |
| 4002 | int dir_fd = DEFAULT_DIR_FD; |
| 4003 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 4004 | int result; |
| 4005 | PyObject *return_value = NULL; |
| 4006 | |
| 4007 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4008 | path.function_name = "rmdir"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4009 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords, |
| 4010 | path_converter, &path, |
| 4011 | #ifdef HAVE_UNLINKAT |
| 4012 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4013 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4014 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4015 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4016 | )) |
| 4017 | return NULL; |
| 4018 | |
| 4019 | Py_BEGIN_ALLOW_THREADS |
| 4020 | #ifdef MS_WINDOWS |
| 4021 | if (path.wide) |
| 4022 | result = RemoveDirectoryW(path.wide); |
| 4023 | else |
| 4024 | result = RemoveDirectoryA(path.narrow); |
| 4025 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4026 | #else |
| 4027 | #ifdef HAVE_UNLINKAT |
| 4028 | if (dir_fd != DEFAULT_DIR_FD) |
| 4029 | result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR); |
| 4030 | else |
| 4031 | #endif |
| 4032 | result = rmdir(path.narrow); |
| 4033 | #endif |
| 4034 | Py_END_ALLOW_THREADS |
| 4035 | |
| 4036 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4037 | return_value = path_error(&path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4038 | goto exit; |
| 4039 | } |
| 4040 | |
| 4041 | return_value = Py_None; |
| 4042 | Py_INCREF(Py_None); |
| 4043 | |
| 4044 | exit: |
| 4045 | path_cleanup(&path); |
| 4046 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4049 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4050 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4051 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4052 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4053 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4054 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4055 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4056 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4057 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4058 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 4059 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4060 | wchar_t *command; |
| 4061 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 4062 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4063 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4064 | Py_BEGIN_ALLOW_THREADS |
| 4065 | sts = _wsystem(command); |
| 4066 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4067 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4068 | PyObject *command_obj; |
| 4069 | char *command; |
| 4070 | if (!PyArg_ParseTuple(args, "O&:system", |
| 4071 | PyUnicode_FSConverter, &command_obj)) |
| 4072 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4073 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4074 | command = PyBytes_AsString(command_obj); |
| 4075 | Py_BEGIN_ALLOW_THREADS |
| 4076 | sts = system(command); |
| 4077 | Py_END_ALLOW_THREADS |
| 4078 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4079 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4080 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4081 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4082 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4083 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4084 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4085 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4086 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4087 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4088 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4089 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4090 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4091 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4092 | int i; |
| 4093 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 4094 | return NULL; |
| 4095 | i = (int)umask(i); |
| 4096 | if (i < 0) |
| 4097 | return posix_error(); |
| 4098 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4101 | #ifdef MS_WINDOWS |
| 4102 | |
| 4103 | /* override the default DeleteFileW behavior so that directory |
| 4104 | symlinks can be removed with this function, the same as with |
| 4105 | Unix symlinks */ |
| 4106 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4107 | { |
| 4108 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4109 | WIN32_FIND_DATAW find_data; |
| 4110 | HANDLE find_data_handle; |
| 4111 | int is_directory = 0; |
| 4112 | int is_link = 0; |
| 4113 | |
| 4114 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4115 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4116 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4117 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4118 | it is a symlink */ |
| 4119 | if(is_directory && |
| 4120 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4121 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4122 | |
| 4123 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 4124 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 4125 | FindClose(find_data_handle); |
| 4126 | } |
| 4127 | } |
| 4128 | } |
| 4129 | |
| 4130 | if (is_directory && is_link) |
| 4131 | return RemoveDirectoryW(lpFileName); |
| 4132 | |
| 4133 | return DeleteFileW(lpFileName); |
| 4134 | } |
| 4135 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4137 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4138 | "unlink(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4139 | Remove a file (same as remove()).\n\ |
| 4140 | \n\ |
| 4141 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4142 | and path should be relative; path will then be relative to that directory.\n\ |
| 4143 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4144 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4145 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4146 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4147 | "remove(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4148 | Remove a file (same as unlink()).\n\ |
| 4149 | \n\ |
| 4150 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4151 | and path should be relative; path will then be relative to that directory.\n\ |
| 4152 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4153 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4154 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4155 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4156 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4157 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4158 | path_t path; |
| 4159 | int dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4160 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4161 | int result; |
| 4162 | PyObject *return_value = NULL; |
| 4163 | |
| 4164 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4165 | path.function_name = "unlink"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4166 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4167 | path_converter, &path, |
| 4168 | #ifdef HAVE_UNLINKAT |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4169 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4170 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4171 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4172 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4173 | )) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4174 | return NULL; |
| 4175 | |
| 4176 | Py_BEGIN_ALLOW_THREADS |
| 4177 | #ifdef MS_WINDOWS |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4178 | if (path.wide) |
| 4179 | result = Py_DeleteFileW(path.wide); |
| 4180 | else |
| 4181 | result = DeleteFileA(path.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4182 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4183 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4184 | #ifdef HAVE_UNLINKAT |
| 4185 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4186 | result = unlinkat(dir_fd, path.narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4187 | else |
| 4188 | #endif /* HAVE_UNLINKAT */ |
| 4189 | result = unlink(path.narrow); |
| 4190 | #endif |
| 4191 | Py_END_ALLOW_THREADS |
| 4192 | |
| 4193 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4194 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4195 | goto exit; |
| 4196 | } |
| 4197 | |
| 4198 | return_value = Py_None; |
| 4199 | Py_INCREF(Py_None); |
| 4200 | |
| 4201 | exit: |
| 4202 | path_cleanup(&path); |
| 4203 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4206 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4207 | PyDoc_STRVAR(posix_uname__doc__, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4208 | "uname() -> uname_result\n\n\ |
| 4209 | Return an object identifying the current operating system.\n\ |
| 4210 | The object behaves like a named tuple with the following fields:\n\ |
| 4211 | (sysname, nodename, release, version, machine)"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4212 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4213 | static PyStructSequence_Field uname_result_fields[] = { |
| 4214 | {"sysname", "operating system name"}, |
| 4215 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4216 | {"release", "operating system release"}, |
| 4217 | {"version", "operating system version"}, |
| 4218 | {"machine", "hardware identifier"}, |
| 4219 | {NULL} |
| 4220 | }; |
| 4221 | |
| 4222 | PyDoc_STRVAR(uname_result__doc__, |
| 4223 | "uname_result: Result from os.uname().\n\n\ |
| 4224 | This object may be accessed either as a tuple of\n\ |
| 4225 | (sysname, nodename, release, version, machine),\n\ |
| 4226 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4227 | \n\ |
| 4228 | See os.uname for more information."); |
| 4229 | |
| 4230 | static PyStructSequence_Desc uname_result_desc = { |
| 4231 | "uname_result", /* name */ |
| 4232 | uname_result__doc__, /* doc */ |
| 4233 | uname_result_fields, |
| 4234 | 5 |
| 4235 | }; |
| 4236 | |
| 4237 | static PyTypeObject UnameResultType; |
| 4238 | |
| 4239 | |
| 4240 | #ifdef HAVE_UNAME |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4241 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4242 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4243 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4244 | struct utsname u; |
| 4245 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4246 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4247 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4248 | Py_BEGIN_ALLOW_THREADS |
| 4249 | res = uname(&u); |
| 4250 | Py_END_ALLOW_THREADS |
| 4251 | if (res < 0) |
| 4252 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4253 | |
| 4254 | value = PyStructSequence_New(&UnameResultType); |
| 4255 | if (value == NULL) |
| 4256 | return NULL; |
| 4257 | |
| 4258 | #define SET(i, field) \ |
| 4259 | { \ |
| 4260 | PyObject *o = PyUnicode_DecodeASCII(field, strlen(field), NULL); \ |
| 4261 | if (!o) { \ |
| 4262 | Py_DECREF(value); \ |
| 4263 | return NULL; \ |
| 4264 | } \ |
| 4265 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4266 | } \ |
| 4267 | |
| 4268 | SET(0, u.sysname); |
| 4269 | SET(1, u.nodename); |
| 4270 | SET(2, u.release); |
| 4271 | SET(3, u.version); |
| 4272 | SET(4, u.machine); |
| 4273 | |
| 4274 | #undef SET |
| 4275 | |
| 4276 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4277 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4278 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4279 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4280 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4281 | PyDoc_STRVAR(posix_utime__doc__, |
| 4282 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4283 | Set the access and modified time of path.\n\ |
| 4284 | \n\ |
| 4285 | path may always be specified as a string.\n\ |
| 4286 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4287 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4288 | \n\ |
| 4289 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4290 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4291 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4292 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4293 | since the epoch.\n\ |
| 4294 | If both times and ns are None, utime uses the current time.\n\ |
| 4295 | Specifying tuples for both times and ns is an error.\n\ |
| 4296 | \n\ |
| 4297 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4298 | and path should be relative; path will then be relative to that directory.\n\ |
| 4299 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4300 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4301 | link points to.\n\ |
| 4302 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4303 | as an open file descriptor.\n\ |
| 4304 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4305 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4306 | |
| 4307 | typedef struct { |
| 4308 | int now; |
| 4309 | time_t atime_s; |
| 4310 | long atime_ns; |
| 4311 | time_t mtime_s; |
| 4312 | long mtime_ns; |
| 4313 | } utime_t; |
| 4314 | |
| 4315 | /* |
| 4316 | * these macros assume that "utime" is a pointer to a utime_t |
| 4317 | * they also intentionally leak the declaration of a pointer named "time" |
| 4318 | */ |
| 4319 | #define UTIME_TO_TIMESPEC \ |
| 4320 | struct timespec ts[2]; \ |
| 4321 | struct timespec *time; \ |
| 4322 | if (utime->now) \ |
| 4323 | time = NULL; \ |
| 4324 | else { \ |
| 4325 | ts[0].tv_sec = utime->atime_s; \ |
| 4326 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4327 | ts[1].tv_sec = utime->mtime_s; \ |
| 4328 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4329 | time = ts; \ |
| 4330 | } \ |
| 4331 | |
| 4332 | #define UTIME_TO_TIMEVAL \ |
| 4333 | struct timeval tv[2]; \ |
| 4334 | struct timeval *time; \ |
| 4335 | if (utime->now) \ |
| 4336 | time = NULL; \ |
| 4337 | else { \ |
| 4338 | tv[0].tv_sec = utime->atime_s; \ |
| 4339 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4340 | tv[1].tv_sec = utime->mtime_s; \ |
| 4341 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4342 | time = tv; \ |
| 4343 | } \ |
| 4344 | |
| 4345 | #define UTIME_TO_UTIMBUF \ |
| 4346 | struct utimbuf u[2]; \ |
| 4347 | struct utimbuf *time; \ |
| 4348 | if (utime->now) \ |
| 4349 | time = NULL; \ |
| 4350 | else { \ |
| 4351 | u.actime = utime->atime_s; \ |
| 4352 | u.modtime = utime->mtime_s; \ |
| 4353 | time = u; \ |
| 4354 | } |
| 4355 | |
| 4356 | #define UTIME_TO_TIME_T \ |
| 4357 | time_t timet[2]; \ |
| 4358 | struct timet time; \ |
| 4359 | if (utime->now) \ |
| 4360 | time = NULL; \ |
| 4361 | else { \ |
| 4362 | timet[0] = utime->atime_s; \ |
| 4363 | timet[1] = utime->mtime_s; \ |
| 4364 | time = &timet; \ |
| 4365 | } \ |
| 4366 | |
| 4367 | |
| 4368 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4369 | |
| 4370 | #if UTIME_HAVE_DIR_FD |
| 4371 | |
| 4372 | static int |
| 4373 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4374 | { |
| 4375 | #ifdef HAVE_UTIMENSAT |
| 4376 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4377 | UTIME_TO_TIMESPEC; |
| 4378 | return utimensat(dir_fd, path, time, flags); |
| 4379 | #elif defined(HAVE_FUTIMESAT) |
| 4380 | UTIME_TO_TIMEVAL; |
| 4381 | /* |
| 4382 | * follow_symlinks will never be false here; |
| 4383 | * we only allow !follow_symlinks and dir_fd together |
| 4384 | * if we have utimensat() |
| 4385 | */ |
| 4386 | assert(follow_symlinks); |
| 4387 | return futimesat(dir_fd, path, time); |
| 4388 | #endif |
| 4389 | } |
| 4390 | |
| 4391 | #endif |
| 4392 | |
| 4393 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4394 | |
| 4395 | #if UTIME_HAVE_FD |
| 4396 | |
| 4397 | static int |
| 4398 | utime_fd(utime_t *utime, int fd) |
| 4399 | { |
| 4400 | #ifdef HAVE_FUTIMENS |
| 4401 | UTIME_TO_TIMESPEC; |
| 4402 | return futimens(fd, time); |
| 4403 | #else |
| 4404 | UTIME_TO_TIMEVAL; |
| 4405 | return futimes(fd, time); |
| 4406 | #endif |
| 4407 | } |
| 4408 | |
| 4409 | #endif |
| 4410 | |
| 4411 | |
| 4412 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4413 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4414 | |
| 4415 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4416 | |
| 4417 | static int |
| 4418 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4419 | { |
| 4420 | #ifdef HAVE_UTIMENSAT |
| 4421 | UTIME_TO_TIMESPEC; |
| 4422 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4423 | #else |
| 4424 | UTIME_TO_TIMEVAL; |
| 4425 | return lutimes(path, time); |
| 4426 | #endif |
| 4427 | } |
| 4428 | |
| 4429 | #endif |
| 4430 | |
| 4431 | #ifndef MS_WINDOWS |
| 4432 | |
| 4433 | static int |
| 4434 | utime_default(utime_t *utime, char *path) |
| 4435 | { |
| 4436 | #ifdef HAVE_UTIMENSAT |
| 4437 | UTIME_TO_TIMESPEC; |
| 4438 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4439 | #elif defined(HAVE_UTIMES) |
| 4440 | UTIME_TO_TIMEVAL; |
| 4441 | return utimes(path, time); |
| 4442 | #elif defined(HAVE_UTIME_H) |
| 4443 | UTIME_TO_UTIMBUF; |
| 4444 | return utime(path, time); |
| 4445 | #else |
| 4446 | UTIME_TO_TIME_T; |
| 4447 | return utime(path, time); |
| 4448 | #endif |
| 4449 | } |
| 4450 | |
| 4451 | #endif |
| 4452 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4453 | static int |
| 4454 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4455 | { |
| 4456 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4457 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4458 | divmod = PyNumber_Divmod(py_long, billion); |
| 4459 | if (!divmod) |
| 4460 | goto exit; |
| 4461 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4462 | if ((*s == -1) && PyErr_Occurred()) |
| 4463 | goto exit; |
| 4464 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4465 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4466 | goto exit; |
| 4467 | |
| 4468 | result = 1; |
| 4469 | exit: |
| 4470 | Py_XDECREF(divmod); |
| 4471 | return result; |
| 4472 | } |
| 4473 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4474 | static PyObject * |
| 4475 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4476 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4477 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4478 | PyObject *times = NULL; |
| 4479 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4480 | int dir_fd = DEFAULT_DIR_FD; |
| 4481 | int follow_symlinks = 1; |
| 4482 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4483 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4484 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4485 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4486 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4487 | #ifdef MS_WINDOWS |
| 4488 | HANDLE hFile; |
| 4489 | FILETIME atime, mtime; |
| 4490 | #else |
| 4491 | int result; |
| 4492 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4493 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4494 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4495 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4496 | memset(&path, 0, sizeof(path)); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4497 | path.function_name = "utime"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4498 | #if UTIME_HAVE_FD |
| 4499 | path.allow_fd = 1; |
| 4500 | #endif |
| 4501 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4502 | "O&|O$OO&p:utime", keywords, |
| 4503 | path_converter, &path, |
| 4504 | ×, &ns, |
| 4505 | #if UTIME_HAVE_DIR_FD |
| 4506 | dir_fd_converter, &dir_fd, |
| 4507 | #else |
| 4508 | dir_fd_unavailable, &dir_fd, |
| 4509 | #endif |
| 4510 | &follow_symlinks |
| 4511 | )) |
| 4512 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4513 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4514 | if (times && (times != Py_None) && ns) { |
| 4515 | PyErr_SetString(PyExc_ValueError, |
| 4516 | "utime: you may specify either 'times'" |
| 4517 | " or 'ns' but not both"); |
| 4518 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4522 | time_t a_sec, m_sec; |
| 4523 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4524 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4525 | PyErr_SetString(PyExc_TypeError, |
| 4526 | "utime: 'times' must be either" |
| 4527 | " a tuple of two ints or None"); |
| 4528 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4529 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4530 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4531 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4532 | &a_sec, &a_nsec) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4533 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4534 | &m_sec, &m_nsec) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4535 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4536 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4537 | utime.atime_s = a_sec; |
| 4538 | utime.atime_ns = a_nsec; |
| 4539 | utime.mtime_s = m_sec; |
| 4540 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4541 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4542 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4543 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4544 | PyErr_SetString(PyExc_TypeError, |
| 4545 | "utime: 'ns' must be a tuple of two ints"); |
| 4546 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4547 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4548 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4549 | if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4550 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4551 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4552 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4553 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4554 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4555 | } |
| 4556 | else { |
| 4557 | /* times and ns are both None/unspecified. use "now". */ |
| 4558 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4559 | } |
| 4560 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4561 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4562 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4563 | goto exit; |
| 4564 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4565 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4566 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4567 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4568 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4569 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4570 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4571 | #if !defined(HAVE_UTIMENSAT) |
| 4572 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4573 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4574 | "utime: cannot use dir_fd and follow_symlinks " |
| 4575 | "together on this platform"); |
| 4576 | goto exit; |
| 4577 | } |
| 4578 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4579 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4580 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4581 | Py_BEGIN_ALLOW_THREADS |
| 4582 | if (path.wide) |
| 4583 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4584 | NULL, OPEN_EXISTING, |
| 4585 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4586 | else |
| 4587 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4588 | NULL, OPEN_EXISTING, |
| 4589 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4590 | Py_END_ALLOW_THREADS |
| 4591 | if (hFile == INVALID_HANDLE_VALUE) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4592 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4593 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4594 | } |
| 4595 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4596 | if (utime.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4597 | SYSTEMTIME now; |
| 4598 | GetSystemTime(&now); |
| 4599 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 4600 | !SystemTimeToFileTime(&now, &atime)) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4601 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4602 | goto exit; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4603 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4604 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4605 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4606 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4607 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4608 | } |
| 4609 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4610 | /* Avoid putting the file name into the error here, |
| 4611 | as that may confuse the user into believing that |
| 4612 | something is wrong with the file, when it also |
| 4613 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4614 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4615 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4616 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4617 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4618 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4619 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4620 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4621 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4622 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4623 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4624 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4625 | |
| 4626 | #if UTIME_HAVE_DIR_FD |
| 4627 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4628 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4629 | else |
| 4630 | #endif |
| 4631 | |
| 4632 | #if UTIME_HAVE_FD |
| 4633 | if (path.fd != -1) |
| 4634 | result = utime_fd(&utime, path.fd); |
| 4635 | else |
| 4636 | #endif |
| 4637 | |
| 4638 | result = utime_default(&utime, path.narrow); |
| 4639 | |
| 4640 | Py_END_ALLOW_THREADS |
| 4641 | |
| 4642 | if (result < 0) { |
| 4643 | /* see previous comment about not putting filename in error here */ |
| 4644 | return_value = posix_error(); |
| 4645 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4646 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4647 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4648 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4649 | |
| 4650 | Py_INCREF(Py_None); |
| 4651 | return_value = Py_None; |
| 4652 | |
| 4653 | exit: |
| 4654 | path_cleanup(&path); |
| 4655 | #ifdef MS_WINDOWS |
| 4656 | if (hFile != INVALID_HANDLE_VALUE) |
| 4657 | CloseHandle(hFile); |
| 4658 | #endif |
| 4659 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4662 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4663 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4664 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4665 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4666 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4667 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4668 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4669 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4670 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4671 | int sts; |
| 4672 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 4673 | return NULL; |
| 4674 | _exit(sts); |
| 4675 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4678 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4679 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4680 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4681 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4682 | Py_ssize_t i; |
| 4683 | for (i = 0; i < count; i++) |
| 4684 | PyMem_Free(array[i]); |
| 4685 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4686 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4687 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4688 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4689 | int fsconvert_strdup(PyObject *o, char**out) |
| 4690 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4691 | PyObject *bytes; |
| 4692 | Py_ssize_t size; |
| 4693 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4694 | return 0; |
| 4695 | size = PyBytes_GET_SIZE(bytes); |
| 4696 | *out = PyMem_Malloc(size+1); |
| 4697 | if (!*out) |
| 4698 | return 0; |
| 4699 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4700 | Py_DECREF(bytes); |
| 4701 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4702 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4703 | #endif |
| 4704 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4705 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4706 | static char** |
| 4707 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4708 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4709 | char **envlist; |
| 4710 | Py_ssize_t i, pos, envc; |
| 4711 | PyObject *keys=NULL, *vals=NULL; |
| 4712 | PyObject *key, *val, *key2, *val2; |
| 4713 | char *p, *k, *v; |
| 4714 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4715 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4716 | i = PyMapping_Size(env); |
| 4717 | if (i < 0) |
| 4718 | return NULL; |
| 4719 | envlist = PyMem_NEW(char *, i + 1); |
| 4720 | if (envlist == NULL) { |
| 4721 | PyErr_NoMemory(); |
| 4722 | return NULL; |
| 4723 | } |
| 4724 | envc = 0; |
| 4725 | keys = PyMapping_Keys(env); |
| 4726 | vals = PyMapping_Values(env); |
| 4727 | if (!keys || !vals) |
| 4728 | goto error; |
| 4729 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4730 | PyErr_Format(PyExc_TypeError, |
| 4731 | "env.keys() or env.values() is not a list"); |
| 4732 | goto error; |
| 4733 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4734 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4735 | for (pos = 0; pos < i; pos++) { |
| 4736 | key = PyList_GetItem(keys, pos); |
| 4737 | val = PyList_GetItem(vals, pos); |
| 4738 | if (!key || !val) |
| 4739 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4740 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4741 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4742 | goto error; |
| 4743 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4744 | Py_DECREF(key2); |
| 4745 | goto error; |
| 4746 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4747 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4748 | k = PyBytes_AsString(key2); |
| 4749 | v = PyBytes_AsString(val2); |
| 4750 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4751 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4752 | p = PyMem_NEW(char, len); |
| 4753 | if (p == NULL) { |
| 4754 | PyErr_NoMemory(); |
| 4755 | Py_DECREF(key2); |
| 4756 | Py_DECREF(val2); |
| 4757 | goto error; |
| 4758 | } |
| 4759 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4760 | envlist[envc++] = p; |
| 4761 | Py_DECREF(key2); |
| 4762 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4763 | } |
| 4764 | Py_DECREF(vals); |
| 4765 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4766 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4767 | envlist[envc] = 0; |
| 4768 | *envc_ptr = envc; |
| 4769 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4770 | |
| 4771 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4772 | Py_XDECREF(keys); |
| 4773 | Py_XDECREF(vals); |
| 4774 | while (--envc >= 0) |
| 4775 | PyMem_DEL(envlist[envc]); |
| 4776 | PyMem_DEL(envlist); |
| 4777 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4778 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4779 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4780 | static char** |
| 4781 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4782 | { |
| 4783 | int i; |
| 4784 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4785 | if (argvlist == NULL) { |
| 4786 | PyErr_NoMemory(); |
| 4787 | return NULL; |
| 4788 | } |
| 4789 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4790 | PyObject* item = PySequence_ITEM(argv, i); |
| 4791 | if (item == NULL) |
| 4792 | goto fail; |
| 4793 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4794 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4795 | goto fail; |
| 4796 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4797 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4798 | } |
| 4799 | argvlist[*argc] = NULL; |
| 4800 | return argvlist; |
| 4801 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4802 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4803 | free_string_array(argvlist, *argc); |
| 4804 | return NULL; |
| 4805 | } |
| 4806 | #endif |
| 4807 | |
| 4808 | #ifdef HAVE_EXECV |
| 4809 | PyDoc_STRVAR(posix_execv__doc__, |
| 4810 | "execv(path, args)\n\n\ |
| 4811 | Execute an executable path with arguments, replacing current process.\n\ |
| 4812 | \n\ |
| 4813 | path: path of executable file\n\ |
| 4814 | args: tuple or list of strings"); |
| 4815 | |
| 4816 | static PyObject * |
| 4817 | posix_execv(PyObject *self, PyObject *args) |
| 4818 | { |
| 4819 | PyObject *opath; |
| 4820 | char *path; |
| 4821 | PyObject *argv; |
| 4822 | char **argvlist; |
| 4823 | Py_ssize_t argc; |
| 4824 | |
| 4825 | /* execv has two arguments: (path, argv), where |
| 4826 | argv is a list or tuple of strings. */ |
| 4827 | |
| 4828 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4829 | PyUnicode_FSConverter, |
| 4830 | &opath, &argv)) |
| 4831 | return NULL; |
| 4832 | path = PyBytes_AsString(opath); |
| 4833 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4834 | PyErr_SetString(PyExc_TypeError, |
| 4835 | "execv() arg 2 must be a tuple or list"); |
| 4836 | Py_DECREF(opath); |
| 4837 | return NULL; |
| 4838 | } |
| 4839 | argc = PySequence_Size(argv); |
| 4840 | if (argc < 1) { |
| 4841 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4842 | Py_DECREF(opath); |
| 4843 | return NULL; |
| 4844 | } |
| 4845 | |
| 4846 | argvlist = parse_arglist(argv, &argc); |
| 4847 | if (argvlist == NULL) { |
| 4848 | Py_DECREF(opath); |
| 4849 | return NULL; |
| 4850 | } |
| 4851 | |
| 4852 | execv(path, argvlist); |
| 4853 | |
| 4854 | /* If we get here it's definitely an error */ |
| 4855 | |
| 4856 | free_string_array(argvlist, argc); |
| 4857 | Py_DECREF(opath); |
| 4858 | return posix_error(); |
| 4859 | } |
| 4860 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4861 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4862 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4863 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4864 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4865 | path: path of executable file\n\ |
| 4866 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4867 | env: dictionary of strings mapping to strings\n\ |
| 4868 | \n\ |
| 4869 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 4870 | execve will execute the program the file descriptor is open to.\n\ |
| 4871 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4872 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4873 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4874 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4875 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4876 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4877 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4878 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4879 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4880 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4881 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4882 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4883 | /* execve has three arguments: (path, argv, env), where |
| 4884 | argv is a list or tuple of strings and env is a dictionary |
| 4885 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4886 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4887 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4888 | path.function_name = "execve"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4889 | #ifdef HAVE_FEXECVE |
| 4890 | path.allow_fd = 1; |
| 4891 | #endif |
| 4892 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 4893 | path_converter, &path, |
| 4894 | &argv, &env |
| 4895 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4896 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4897 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4898 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4899 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4900 | "execve: argv must be a tuple or list"); |
| 4901 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4902 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4903 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4904 | if (!PyMapping_Check(env)) { |
| 4905 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4906 | "execve: environment must be a mapping object"); |
| 4907 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4908 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4909 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4910 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4911 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4912 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4913 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4914 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4915 | envlist = parse_envlist(env, &envc); |
| 4916 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4917 | goto fail; |
| 4918 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4919 | #ifdef HAVE_FEXECVE |
| 4920 | if (path.fd > -1) |
| 4921 | fexecve(path.fd, argvlist, envlist); |
| 4922 | else |
| 4923 | #endif |
| 4924 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4925 | |
| 4926 | /* If we get here it's definitely an error */ |
| 4927 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4928 | path_error(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4929 | |
| 4930 | while (--envc >= 0) |
| 4931 | PyMem_DEL(envlist[envc]); |
| 4932 | PyMem_DEL(envlist); |
| 4933 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4934 | if (argvlist) |
| 4935 | free_string_array(argvlist, argc); |
| 4936 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4937 | return NULL; |
| 4938 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4939 | #endif /* HAVE_EXECV */ |
| 4940 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4941 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4942 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4943 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4944 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4945 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4946 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4947 | mode: mode of process creation\n\ |
| 4948 | path: path of executable file\n\ |
| 4949 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4950 | |
| 4951 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4952 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4953 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4954 | PyObject *opath; |
| 4955 | char *path; |
| 4956 | PyObject *argv; |
| 4957 | char **argvlist; |
| 4958 | int mode, i; |
| 4959 | Py_ssize_t argc; |
| 4960 | Py_intptr_t spawnval; |
| 4961 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4962 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4963 | /* spawnv has three arguments: (mode, path, argv), where |
| 4964 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4965 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4966 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4967 | PyUnicode_FSConverter, |
| 4968 | &opath, &argv)) |
| 4969 | return NULL; |
| 4970 | path = PyBytes_AsString(opath); |
| 4971 | if (PyList_Check(argv)) { |
| 4972 | argc = PyList_Size(argv); |
| 4973 | getitem = PyList_GetItem; |
| 4974 | } |
| 4975 | else if (PyTuple_Check(argv)) { |
| 4976 | argc = PyTuple_Size(argv); |
| 4977 | getitem = PyTuple_GetItem; |
| 4978 | } |
| 4979 | else { |
| 4980 | PyErr_SetString(PyExc_TypeError, |
| 4981 | "spawnv() arg 2 must be a tuple or list"); |
| 4982 | Py_DECREF(opath); |
| 4983 | return NULL; |
| 4984 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4986 | argvlist = PyMem_NEW(char *, argc+1); |
| 4987 | if (argvlist == NULL) { |
| 4988 | Py_DECREF(opath); |
| 4989 | return PyErr_NoMemory(); |
| 4990 | } |
| 4991 | for (i = 0; i < argc; i++) { |
| 4992 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4993 | &argvlist[i])) { |
| 4994 | free_string_array(argvlist, i); |
| 4995 | PyErr_SetString( |
| 4996 | PyExc_TypeError, |
| 4997 | "spawnv() arg 2 must contain only strings"); |
| 4998 | Py_DECREF(opath); |
| 4999 | return NULL; |
| 5000 | } |
| 5001 | } |
| 5002 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5003 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5004 | if (mode == _OLD_P_OVERLAY) |
| 5005 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5006 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5007 | Py_BEGIN_ALLOW_THREADS |
| 5008 | spawnval = _spawnv(mode, path, argvlist); |
| 5009 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5010 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5011 | free_string_array(argvlist, argc); |
| 5012 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5013 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5014 | if (spawnval == -1) |
| 5015 | return posix_error(); |
| 5016 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 5017 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5018 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5019 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5020 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5021 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5025 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5026 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5027 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5028 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5029 | mode: mode of process creation\n\ |
| 5030 | path: path of executable file\n\ |
| 5031 | args: tuple or list of arguments\n\ |
| 5032 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5033 | |
| 5034 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5035 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5036 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5037 | PyObject *opath; |
| 5038 | char *path; |
| 5039 | PyObject *argv, *env; |
| 5040 | char **argvlist; |
| 5041 | char **envlist; |
| 5042 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5043 | int mode; |
| 5044 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5045 | Py_intptr_t spawnval; |
| 5046 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5047 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5048 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5049 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5050 | argv is a list or tuple of strings and env is a dictionary |
| 5051 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5052 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5053 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 5054 | PyUnicode_FSConverter, |
| 5055 | &opath, &argv, &env)) |
| 5056 | return NULL; |
| 5057 | path = PyBytes_AsString(opath); |
| 5058 | if (PyList_Check(argv)) { |
| 5059 | argc = PyList_Size(argv); |
| 5060 | getitem = PyList_GetItem; |
| 5061 | } |
| 5062 | else if (PyTuple_Check(argv)) { |
| 5063 | argc = PyTuple_Size(argv); |
| 5064 | getitem = PyTuple_GetItem; |
| 5065 | } |
| 5066 | else { |
| 5067 | PyErr_SetString(PyExc_TypeError, |
| 5068 | "spawnve() arg 2 must be a tuple or list"); |
| 5069 | goto fail_0; |
| 5070 | } |
| 5071 | if (!PyMapping_Check(env)) { |
| 5072 | PyErr_SetString(PyExc_TypeError, |
| 5073 | "spawnve() arg 3 must be a mapping object"); |
| 5074 | goto fail_0; |
| 5075 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5076 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5077 | argvlist = PyMem_NEW(char *, argc+1); |
| 5078 | if (argvlist == NULL) { |
| 5079 | PyErr_NoMemory(); |
| 5080 | goto fail_0; |
| 5081 | } |
| 5082 | for (i = 0; i < argc; i++) { |
| 5083 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5084 | &argvlist[i])) |
| 5085 | { |
| 5086 | lastarg = i; |
| 5087 | goto fail_1; |
| 5088 | } |
| 5089 | } |
| 5090 | lastarg = argc; |
| 5091 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5092 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5093 | envlist = parse_envlist(env, &envc); |
| 5094 | if (envlist == NULL) |
| 5095 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5096 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5097 | if (mode == _OLD_P_OVERLAY) |
| 5098 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5099 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5100 | Py_BEGIN_ALLOW_THREADS |
| 5101 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 5102 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5103 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5104 | if (spawnval == -1) |
| 5105 | (void) posix_error(); |
| 5106 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 5107 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5108 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5109 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5110 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5111 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5112 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5113 | while (--envc >= 0) |
| 5114 | PyMem_DEL(envlist[envc]); |
| 5115 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5116 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5117 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5118 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5119 | Py_DECREF(opath); |
| 5120 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5121 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5122 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5123 | #endif /* HAVE_SPAWNV */ |
| 5124 | |
| 5125 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5126 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5127 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5128 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5129 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 5130 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5131 | 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] | 5132 | |
| 5133 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5134 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5135 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5136 | pid_t pid; |
| 5137 | int result = 0; |
| 5138 | _PyImport_AcquireLock(); |
| 5139 | pid = fork1(); |
| 5140 | if (pid == 0) { |
| 5141 | /* child: this clobbers and resets the import lock. */ |
| 5142 | PyOS_AfterFork(); |
| 5143 | } else { |
| 5144 | /* parent: release the import lock. */ |
| 5145 | result = _PyImport_ReleaseLock(); |
| 5146 | } |
| 5147 | if (pid == -1) |
| 5148 | return posix_error(); |
| 5149 | if (result < 0) { |
| 5150 | /* Don't clobber the OSError if the fork failed. */ |
| 5151 | PyErr_SetString(PyExc_RuntimeError, |
| 5152 | "not holding the import lock"); |
| 5153 | return NULL; |
| 5154 | } |
| 5155 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5156 | } |
| 5157 | #endif |
| 5158 | |
| 5159 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5160 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5161 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5162 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5163 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5164 | 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] | 5165 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5166 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5167 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5168 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5169 | pid_t pid; |
| 5170 | int result = 0; |
| 5171 | _PyImport_AcquireLock(); |
| 5172 | pid = fork(); |
| 5173 | if (pid == 0) { |
| 5174 | /* child: this clobbers and resets the import lock. */ |
| 5175 | PyOS_AfterFork(); |
| 5176 | } else { |
| 5177 | /* parent: release the import lock. */ |
| 5178 | result = _PyImport_ReleaseLock(); |
| 5179 | } |
| 5180 | if (pid == -1) |
| 5181 | return posix_error(); |
| 5182 | if (result < 0) { |
| 5183 | /* Don't clobber the OSError if the fork failed. */ |
| 5184 | PyErr_SetString(PyExc_RuntimeError, |
| 5185 | "not holding the import lock"); |
| 5186 | return NULL; |
| 5187 | } |
| 5188 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5189 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5190 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5191 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5192 | #ifdef HAVE_SCHED_H |
| 5193 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5194 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5195 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5196 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5197 | "sched_get_priority_max(policy)\n\n\ |
| 5198 | Get the maximum scheduling priority for *policy*."); |
| 5199 | |
| 5200 | static PyObject * |
| 5201 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5202 | { |
| 5203 | int policy, max; |
| 5204 | |
| 5205 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5206 | return NULL; |
| 5207 | max = sched_get_priority_max(policy); |
| 5208 | if (max < 0) |
| 5209 | return posix_error(); |
| 5210 | return PyLong_FromLong(max); |
| 5211 | } |
| 5212 | |
| 5213 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5214 | "sched_get_priority_min(policy)\n\n\ |
| 5215 | Get the minimum scheduling priority for *policy*."); |
| 5216 | |
| 5217 | static PyObject * |
| 5218 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5219 | { |
| 5220 | int policy, min; |
| 5221 | |
| 5222 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5223 | return NULL; |
| 5224 | min = sched_get_priority_min(policy); |
| 5225 | if (min < 0) |
| 5226 | return posix_error(); |
| 5227 | return PyLong_FromLong(min); |
| 5228 | } |
| 5229 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5230 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5231 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5232 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5233 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5234 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5235 | "sched_getscheduler(pid)\n\n\ |
| 5236 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5237 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5238 | |
| 5239 | static PyObject * |
| 5240 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5241 | { |
| 5242 | pid_t pid; |
| 5243 | int policy; |
| 5244 | |
| 5245 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5246 | return NULL; |
| 5247 | policy = sched_getscheduler(pid); |
| 5248 | if (policy < 0) |
| 5249 | return posix_error(); |
| 5250 | return PyLong_FromLong(policy); |
| 5251 | } |
| 5252 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5253 | #endif |
| 5254 | |
| 5255 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5256 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5257 | static PyObject * |
| 5258 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5259 | { |
| 5260 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5261 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5262 | |
| 5263 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5264 | return NULL; |
| 5265 | res = PyStructSequence_New(type); |
| 5266 | if (!res) |
| 5267 | return NULL; |
| 5268 | Py_INCREF(priority); |
| 5269 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5270 | return res; |
| 5271 | } |
| 5272 | |
| 5273 | PyDoc_STRVAR(sched_param__doc__, |
| 5274 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5275 | Current has only one field: sched_priority"); |
| 5276 | |
| 5277 | static PyStructSequence_Field sched_param_fields[] = { |
| 5278 | {"sched_priority", "the scheduling priority"}, |
| 5279 | {0} |
| 5280 | }; |
| 5281 | |
| 5282 | static PyStructSequence_Desc sched_param_desc = { |
| 5283 | "sched_param", /* name */ |
| 5284 | sched_param__doc__, /* doc */ |
| 5285 | sched_param_fields, |
| 5286 | 1 |
| 5287 | }; |
| 5288 | |
| 5289 | static int |
| 5290 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5291 | { |
| 5292 | long priority; |
| 5293 | |
| 5294 | if (Py_TYPE(param) != &SchedParamType) { |
| 5295 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5296 | return 0; |
| 5297 | } |
| 5298 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5299 | if (priority == -1 && PyErr_Occurred()) |
| 5300 | return 0; |
| 5301 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5302 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5303 | return 0; |
| 5304 | } |
| 5305 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5306 | return 1; |
| 5307 | } |
| 5308 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5309 | #endif |
| 5310 | |
| 5311 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5312 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5313 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5314 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5315 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5316 | If *pid* is 0, the calling process is changed.\n\ |
| 5317 | *param* is an instance of sched_param."); |
| 5318 | |
| 5319 | static PyObject * |
| 5320 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5321 | { |
| 5322 | pid_t pid; |
| 5323 | int policy; |
| 5324 | struct sched_param param; |
| 5325 | |
| 5326 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5327 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5328 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5329 | |
| 5330 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5331 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5332 | ** scheduling policy under Solaris/Illumos, and others. |
| 5333 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5334 | */ |
| 5335 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5336 | return posix_error(); |
| 5337 | Py_RETURN_NONE; |
| 5338 | } |
| 5339 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5340 | #endif |
| 5341 | |
| 5342 | #ifdef HAVE_SCHED_SETPARAM |
| 5343 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5344 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5345 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5346 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5347 | sched_param class. A PID of 0 means the calling process."); |
| 5348 | |
| 5349 | static PyObject * |
| 5350 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5351 | { |
| 5352 | pid_t pid; |
| 5353 | struct sched_param param; |
| 5354 | PyObject *res, *priority; |
| 5355 | |
| 5356 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5357 | return NULL; |
| 5358 | if (sched_getparam(pid, ¶m)) |
| 5359 | return posix_error(); |
| 5360 | res = PyStructSequence_New(&SchedParamType); |
| 5361 | if (!res) |
| 5362 | return NULL; |
| 5363 | priority = PyLong_FromLong(param.sched_priority); |
| 5364 | if (!priority) { |
| 5365 | Py_DECREF(res); |
| 5366 | return NULL; |
| 5367 | } |
| 5368 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5369 | return res; |
| 5370 | } |
| 5371 | |
| 5372 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5373 | "sched_setparam(pid, param)\n\n\ |
| 5374 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5375 | A PID of 0 means the calling process."); |
| 5376 | |
| 5377 | static PyObject * |
| 5378 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5379 | { |
| 5380 | pid_t pid; |
| 5381 | struct sched_param param; |
| 5382 | |
| 5383 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5384 | &pid, &convert_sched_param, ¶m)) |
| 5385 | return NULL; |
| 5386 | if (sched_setparam(pid, ¶m)) |
| 5387 | return posix_error(); |
| 5388 | Py_RETURN_NONE; |
| 5389 | } |
| 5390 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5391 | #endif |
| 5392 | |
| 5393 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5394 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5395 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5396 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5397 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5398 | |
| 5399 | static PyObject * |
| 5400 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5401 | { |
| 5402 | pid_t pid; |
| 5403 | struct timespec interval; |
| 5404 | |
| 5405 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5406 | return NULL; |
| 5407 | if (sched_rr_get_interval(pid, &interval)) |
| 5408 | return posix_error(); |
| 5409 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5410 | } |
| 5411 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5412 | #endif |
| 5413 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5414 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5415 | "sched_yield()\n\n\ |
| 5416 | Voluntarily relinquish the CPU."); |
| 5417 | |
| 5418 | static PyObject * |
| 5419 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5420 | { |
| 5421 | if (sched_yield()) |
| 5422 | return posix_error(); |
| 5423 | Py_RETURN_NONE; |
| 5424 | } |
| 5425 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5426 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5427 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5428 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5429 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5430 | |
| 5431 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5432 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5433 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5434 | |
| 5435 | static PyObject * |
| 5436 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5437 | { |
| 5438 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5439 | int ncpus; |
| 5440 | size_t setsize; |
| 5441 | cpu_set_t *mask = NULL; |
| 5442 | PyObject *iterable, *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5443 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5444 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity", |
| 5445 | &pid, &iterable)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5446 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5447 | |
| 5448 | iterator = PyObject_GetIter(iterable); |
| 5449 | if (iterator == NULL) |
| 5450 | return NULL; |
| 5451 | |
| 5452 | ncpus = NCPUS_START; |
| 5453 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5454 | mask = CPU_ALLOC(ncpus); |
| 5455 | if (mask == NULL) { |
| 5456 | PyErr_NoMemory(); |
| 5457 | goto error; |
| 5458 | } |
| 5459 | CPU_ZERO_S(setsize, mask); |
| 5460 | |
| 5461 | while ((item = PyIter_Next(iterator))) { |
| 5462 | long cpu; |
| 5463 | if (!PyLong_Check(item)) { |
| 5464 | PyErr_Format(PyExc_TypeError, |
| 5465 | "expected an iterator of ints, " |
| 5466 | "but iterator yielded %R", |
| 5467 | Py_TYPE(item)); |
| 5468 | Py_DECREF(item); |
| 5469 | goto error; |
| 5470 | } |
| 5471 | cpu = PyLong_AsLong(item); |
| 5472 | Py_DECREF(item); |
| 5473 | if (cpu < 0) { |
| 5474 | if (!PyErr_Occurred()) |
| 5475 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5476 | goto error; |
| 5477 | } |
| 5478 | if (cpu > INT_MAX - 1) { |
| 5479 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5480 | goto error; |
| 5481 | } |
| 5482 | if (cpu >= ncpus) { |
| 5483 | /* Grow CPU mask to fit the CPU number */ |
| 5484 | int newncpus = ncpus; |
| 5485 | cpu_set_t *newmask; |
| 5486 | size_t newsetsize; |
| 5487 | while (newncpus <= cpu) { |
| 5488 | if (newncpus > INT_MAX / 2) |
| 5489 | newncpus = cpu + 1; |
| 5490 | else |
| 5491 | newncpus = newncpus * 2; |
| 5492 | } |
| 5493 | newmask = CPU_ALLOC(newncpus); |
| 5494 | if (newmask == NULL) { |
| 5495 | PyErr_NoMemory(); |
| 5496 | goto error; |
| 5497 | } |
| 5498 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5499 | CPU_ZERO_S(newsetsize, newmask); |
| 5500 | memcpy(newmask, mask, setsize); |
| 5501 | CPU_FREE(mask); |
| 5502 | setsize = newsetsize; |
| 5503 | mask = newmask; |
| 5504 | ncpus = newncpus; |
| 5505 | } |
| 5506 | CPU_SET_S(cpu, setsize, mask); |
| 5507 | } |
| 5508 | Py_CLEAR(iterator); |
| 5509 | |
| 5510 | if (sched_setaffinity(pid, setsize, mask)) { |
| 5511 | posix_error(); |
| 5512 | goto error; |
| 5513 | } |
| 5514 | CPU_FREE(mask); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5515 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5516 | |
| 5517 | error: |
| 5518 | if (mask) |
| 5519 | CPU_FREE(mask); |
| 5520 | Py_XDECREF(iterator); |
| 5521 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5525 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5526 | Return the affinity of the process with PID *pid*.\n\ |
| 5527 | The returned cpu_set will be of size *ncpus*."); |
| 5528 | |
| 5529 | static PyObject * |
| 5530 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5531 | { |
| 5532 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5533 | int cpu, ncpus, count; |
| 5534 | size_t setsize; |
| 5535 | cpu_set_t *mask = NULL; |
| 5536 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5537 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5538 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity", |
| 5539 | &pid)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5540 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5541 | |
| 5542 | ncpus = NCPUS_START; |
| 5543 | while (1) { |
| 5544 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5545 | mask = CPU_ALLOC(ncpus); |
| 5546 | if (mask == NULL) |
| 5547 | return PyErr_NoMemory(); |
| 5548 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5549 | break; |
| 5550 | CPU_FREE(mask); |
| 5551 | if (errno != EINVAL) |
| 5552 | return posix_error(); |
| 5553 | if (ncpus > INT_MAX / 2) { |
| 5554 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5555 | "a large enough CPU set"); |
| 5556 | return NULL; |
| 5557 | } |
| 5558 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5559 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5560 | |
| 5561 | res = PySet_New(NULL); |
| 5562 | if (res == NULL) |
| 5563 | goto error; |
| 5564 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5565 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5566 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5567 | --count; |
| 5568 | if (cpu_num == NULL) |
| 5569 | goto error; |
| 5570 | if (PySet_Add(res, cpu_num)) { |
| 5571 | Py_DECREF(cpu_num); |
| 5572 | goto error; |
| 5573 | } |
| 5574 | Py_DECREF(cpu_num); |
| 5575 | } |
| 5576 | } |
| 5577 | CPU_FREE(mask); |
| 5578 | return res; |
| 5579 | |
| 5580 | error: |
| 5581 | if (mask) |
| 5582 | CPU_FREE(mask); |
| 5583 | Py_XDECREF(res); |
| 5584 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5585 | } |
| 5586 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5587 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5588 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5589 | #endif /* HAVE_SCHED_H */ |
| 5590 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5591 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5592 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5593 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5594 | #define DEV_PTY_FILE "/dev/ptc" |
| 5595 | #define HAVE_DEV_PTMX |
| 5596 | #else |
| 5597 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5598 | #endif |
| 5599 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5600 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5601 | #ifdef HAVE_PTY_H |
| 5602 | #include <pty.h> |
| 5603 | #else |
| 5604 | #ifdef HAVE_LIBUTIL_H |
| 5605 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5606 | #else |
| 5607 | #ifdef HAVE_UTIL_H |
| 5608 | #include <util.h> |
| 5609 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5610 | #endif /* HAVE_LIBUTIL_H */ |
| 5611 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5612 | #ifdef HAVE_STROPTS_H |
| 5613 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5614 | #endif |
| 5615 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5616 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5617 | #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] | 5618 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5619 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5620 | 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] | 5621 | |
| 5622 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5623 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5624 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5625 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5626 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5627 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5628 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5629 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5630 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5631 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5632 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5633 | #endif |
| 5634 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5635 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5636 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5637 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5638 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5639 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5640 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5641 | if (slave_name == NULL) |
| 5642 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5643 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5644 | slave_fd = open(slave_name, O_RDWR); |
| 5645 | if (slave_fd < 0) |
| 5646 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5647 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5648 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5649 | if (master_fd < 0) |
| 5650 | return posix_error(); |
| 5651 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5652 | /* change permission of slave */ |
| 5653 | if (grantpt(master_fd) < 0) { |
| 5654 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5655 | return posix_error(); |
| 5656 | } |
| 5657 | /* unlock slave */ |
| 5658 | if (unlockpt(master_fd) < 0) { |
| 5659 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5660 | return posix_error(); |
| 5661 | } |
| 5662 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5663 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5664 | if (slave_name == NULL) |
| 5665 | return posix_error(); |
| 5666 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5667 | if (slave_fd < 0) |
| 5668 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5669 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5670 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5671 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5672 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5673 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5674 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5675 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5676 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5677 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5678 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5679 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5680 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5681 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5682 | |
| 5683 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5684 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5685 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5686 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5687 | 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] | 5688 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5689 | |
| 5690 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5691 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5692 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5693 | int master_fd = -1, result = 0; |
| 5694 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5695 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5696 | _PyImport_AcquireLock(); |
| 5697 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5698 | if (pid == 0) { |
| 5699 | /* child: this clobbers and resets the import lock. */ |
| 5700 | PyOS_AfterFork(); |
| 5701 | } else { |
| 5702 | /* parent: release the import lock. */ |
| 5703 | result = _PyImport_ReleaseLock(); |
| 5704 | } |
| 5705 | if (pid == -1) |
| 5706 | return posix_error(); |
| 5707 | if (result < 0) { |
| 5708 | /* Don't clobber the OSError if the fork failed. */ |
| 5709 | PyErr_SetString(PyExc_RuntimeError, |
| 5710 | "not holding the import lock"); |
| 5711 | return NULL; |
| 5712 | } |
| 5713 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5714 | } |
| 5715 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5716 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5717 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5718 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5719 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5720 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5721 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5722 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5723 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5724 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5725 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5726 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5727 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5728 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5729 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5730 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5731 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5732 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5733 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5734 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5735 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5736 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5737 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5738 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5739 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5740 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5741 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5742 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5743 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5744 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5745 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5746 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5747 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5748 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5749 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5750 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5751 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5752 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5753 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5754 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5755 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5756 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5757 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5758 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5759 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5760 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5761 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5762 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5763 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5764 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5767 | #ifdef HAVE_GETGROUPLIST |
| 5768 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5769 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5770 | Returns a list of groups to which a user belongs.\n\n\ |
| 5771 | user: username to lookup\n\ |
| 5772 | group: base group id of the user"); |
| 5773 | |
| 5774 | static PyObject * |
| 5775 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5776 | { |
| 5777 | #ifdef NGROUPS_MAX |
| 5778 | #define MAX_GROUPS NGROUPS_MAX |
| 5779 | #else |
| 5780 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5781 | #define MAX_GROUPS 64 |
| 5782 | #endif |
| 5783 | |
| 5784 | const char *user; |
| 5785 | int i, ngroups; |
| 5786 | PyObject *list; |
| 5787 | #ifdef __APPLE__ |
| 5788 | int *groups, basegid; |
| 5789 | #else |
| 5790 | gid_t *groups, basegid; |
| 5791 | #endif |
| 5792 | ngroups = MAX_GROUPS; |
| 5793 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5794 | #ifdef __APPLE__ |
| 5795 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5796 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5797 | #else |
| 5798 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 5799 | _Py_Gid_Converter, &basegid)) |
| 5800 | return NULL; |
| 5801 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5802 | |
| 5803 | #ifdef __APPLE__ |
| 5804 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5805 | #else |
| 5806 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5807 | #endif |
| 5808 | if (groups == NULL) |
| 5809 | return PyErr_NoMemory(); |
| 5810 | |
| 5811 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5812 | PyMem_Del(groups); |
| 5813 | return posix_error(); |
| 5814 | } |
| 5815 | |
| 5816 | list = PyList_New(ngroups); |
| 5817 | if (list == NULL) { |
| 5818 | PyMem_Del(groups); |
| 5819 | return NULL; |
| 5820 | } |
| 5821 | |
| 5822 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5823 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5824 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5825 | #else |
| 5826 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 5827 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5828 | if (o == NULL) { |
| 5829 | Py_DECREF(list); |
| 5830 | PyMem_Del(groups); |
| 5831 | return NULL; |
| 5832 | } |
| 5833 | PyList_SET_ITEM(list, i, o); |
| 5834 | } |
| 5835 | |
| 5836 | PyMem_Del(groups); |
| 5837 | |
| 5838 | return list; |
| 5839 | } |
| 5840 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5841 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5842 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5843 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5844 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5845 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5846 | |
| 5847 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5848 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5849 | { |
| 5850 | PyObject *result = NULL; |
| 5851 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5852 | #ifdef NGROUPS_MAX |
| 5853 | #define MAX_GROUPS NGROUPS_MAX |
| 5854 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5855 | /* 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] | 5856 | #define MAX_GROUPS 64 |
| 5857 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5858 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5859 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5860 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5861 | * This is a helper variable to store the intermediate result when |
| 5862 | * that happens. |
| 5863 | * |
| 5864 | * To keep the code readable the OSX behaviour is unconditional, |
| 5865 | * according to the POSIX spec this should be safe on all unix-y |
| 5866 | * systems. |
| 5867 | */ |
| 5868 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5869 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5870 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5871 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5872 | if (n < 0) { |
| 5873 | if (errno == EINVAL) { |
| 5874 | n = getgroups(0, NULL); |
| 5875 | if (n == -1) { |
| 5876 | return posix_error(); |
| 5877 | } |
| 5878 | if (n == 0) { |
| 5879 | /* Avoid malloc(0) */ |
| 5880 | alt_grouplist = grouplist; |
| 5881 | } else { |
| 5882 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5883 | if (alt_grouplist == NULL) { |
| 5884 | errno = EINVAL; |
| 5885 | return posix_error(); |
| 5886 | } |
| 5887 | n = getgroups(n, alt_grouplist); |
| 5888 | if (n == -1) { |
| 5889 | PyMem_Free(alt_grouplist); |
| 5890 | return posix_error(); |
| 5891 | } |
| 5892 | } |
| 5893 | } else { |
| 5894 | return posix_error(); |
| 5895 | } |
| 5896 | } |
| 5897 | result = PyList_New(n); |
| 5898 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5899 | int i; |
| 5900 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5901 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5902 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5903 | Py_DECREF(result); |
| 5904 | result = NULL; |
| 5905 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5906 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5907 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5908 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | if (alt_grouplist != grouplist) { |
| 5912 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5913 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5914 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5915 | return result; |
| 5916 | } |
| 5917 | #endif |
| 5918 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5919 | #ifdef HAVE_INITGROUPS |
| 5920 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5921 | "initgroups(username, gid) -> None\n\n\ |
| 5922 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5923 | the groups of which the specified username is a member, plus the specified\n\ |
| 5924 | group id."); |
| 5925 | |
| 5926 | static PyObject * |
| 5927 | posix_initgroups(PyObject *self, PyObject *args) |
| 5928 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5929 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5930 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5931 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5932 | #ifdef __APPLE__ |
| 5933 | int gid; |
| 5934 | #else |
| 5935 | gid_t gid; |
| 5936 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5937 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5938 | #ifdef __APPLE__ |
| 5939 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 5940 | PyUnicode_FSConverter, &oname, |
| 5941 | &gid)) |
| 5942 | #else |
| 5943 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 5944 | PyUnicode_FSConverter, &oname, |
| 5945 | _Py_Gid_Converter, &gid)) |
| 5946 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5947 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5948 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5949 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5950 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5951 | Py_DECREF(oname); |
| 5952 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5953 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5954 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5955 | Py_INCREF(Py_None); |
| 5956 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5957 | } |
| 5958 | #endif |
| 5959 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5960 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5961 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5962 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5963 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5964 | |
| 5965 | static PyObject * |
| 5966 | posix_getpgid(PyObject *self, PyObject *args) |
| 5967 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5968 | pid_t pid, pgid; |
| 5969 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5970 | return NULL; |
| 5971 | pgid = getpgid(pid); |
| 5972 | if (pgid < 0) |
| 5973 | return posix_error(); |
| 5974 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5975 | } |
| 5976 | #endif /* HAVE_GETPGID */ |
| 5977 | |
| 5978 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5979 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5980 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5981 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5982 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5983 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5984 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5985 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5986 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5987 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5988 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5989 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5990 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5991 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5992 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5993 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5994 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5995 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5996 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5997 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5998 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5999 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6000 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6001 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6002 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6003 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6004 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6005 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6006 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6007 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6008 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6009 | return posix_error(); |
| 6010 | Py_INCREF(Py_None); |
| 6011 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6014 | #endif /* HAVE_SETPGRP */ |
| 6015 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6016 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6017 | |
| 6018 | #ifdef MS_WINDOWS |
| 6019 | #include <tlhelp32.h> |
| 6020 | |
| 6021 | static PyObject* |
| 6022 | win32_getppid() |
| 6023 | { |
| 6024 | HANDLE snapshot; |
| 6025 | pid_t mypid; |
| 6026 | PyObject* result = NULL; |
| 6027 | BOOL have_record; |
| 6028 | PROCESSENTRY32 pe; |
| 6029 | |
| 6030 | mypid = getpid(); /* This function never fails */ |
| 6031 | |
| 6032 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6033 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6034 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6035 | |
| 6036 | pe.dwSize = sizeof(pe); |
| 6037 | have_record = Process32First(snapshot, &pe); |
| 6038 | while (have_record) { |
| 6039 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6040 | /* We could cache the ulong value in a static variable. */ |
| 6041 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6042 | break; |
| 6043 | } |
| 6044 | |
| 6045 | have_record = Process32Next(snapshot, &pe); |
| 6046 | } |
| 6047 | |
| 6048 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6049 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6050 | * error anyway, so let's raise it. */ |
| 6051 | if (!result) |
| 6052 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6053 | |
| 6054 | CloseHandle(snapshot); |
| 6055 | |
| 6056 | return result; |
| 6057 | } |
| 6058 | #endif /*MS_WINDOWS*/ |
| 6059 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6060 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6061 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6062 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6063 | Windows machines will still return its id; others systems will return the id\n\ |
| 6064 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6065 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6066 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6067 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6068 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6069 | #ifdef MS_WINDOWS |
| 6070 | return win32_getppid(); |
| 6071 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6072 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6073 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6074 | } |
| 6075 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6076 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6077 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6078 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6079 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6080 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6081 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6082 | |
| 6083 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6084 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6085 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6086 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6087 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6088 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6089 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6090 | |
| 6091 | if (GetUserNameW(user_name, &num_chars)) { |
| 6092 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6093 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6094 | } |
| 6095 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6096 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6097 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6098 | char *name; |
| 6099 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6100 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6101 | errno = 0; |
| 6102 | name = getlogin(); |
| 6103 | if (name == NULL) { |
| 6104 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6105 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6106 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6107 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6108 | } |
| 6109 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6110 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6111 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6112 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6113 | return result; |
| 6114 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6115 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6116 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6117 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6118 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6119 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6120 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6121 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6122 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6123 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6124 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6125 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6126 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6127 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6128 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6129 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6130 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6131 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6132 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6133 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6134 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6135 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6136 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6137 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6138 | pid_t pid; |
| 6139 | int sig; |
| 6140 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6141 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6142 | if (kill(pid, sig) == -1) |
| 6143 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6144 | Py_INCREF(Py_None); |
| 6145 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6146 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6147 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6148 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6149 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6150 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6151 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6152 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6153 | |
| 6154 | static PyObject * |
| 6155 | posix_killpg(PyObject *self, PyObject *args) |
| 6156 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6157 | int sig; |
| 6158 | pid_t pgid; |
| 6159 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6160 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6161 | take the same type. Moreover, pid_t is always at least as wide as |
| 6162 | int (else compilation of this module fails), which is safe. */ |
| 6163 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6164 | return NULL; |
| 6165 | if (killpg(pgid, sig) == -1) |
| 6166 | return posix_error(); |
| 6167 | Py_INCREF(Py_None); |
| 6168 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6169 | } |
| 6170 | #endif |
| 6171 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6172 | #ifdef MS_WINDOWS |
| 6173 | PyDoc_STRVAR(win32_kill__doc__, |
| 6174 | "kill(pid, sig)\n\n\ |
| 6175 | Kill a process with a signal."); |
| 6176 | |
| 6177 | static PyObject * |
| 6178 | win32_kill(PyObject *self, PyObject *args) |
| 6179 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6180 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6181 | DWORD pid, sig, err; |
| 6182 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6184 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 6185 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6186 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6187 | /* Console processes which share a common console can be sent CTRL+C or |
| 6188 | CTRL+BREAK events, provided they handle said events. */ |
| 6189 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 6190 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 6191 | err = GetLastError(); |
| 6192 | PyErr_SetFromWindowsErr(err); |
| 6193 | } |
| 6194 | else |
| 6195 | Py_RETURN_NONE; |
| 6196 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6197 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6198 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6199 | attempt to open and terminate the process. */ |
| 6200 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 6201 | if (handle == NULL) { |
| 6202 | err = GetLastError(); |
| 6203 | return PyErr_SetFromWindowsErr(err); |
| 6204 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6205 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6206 | if (TerminateProcess(handle, sig) == 0) { |
| 6207 | err = GetLastError(); |
| 6208 | result = PyErr_SetFromWindowsErr(err); |
| 6209 | } else { |
| 6210 | Py_INCREF(Py_None); |
| 6211 | result = Py_None; |
| 6212 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6213 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6214 | CloseHandle(handle); |
| 6215 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6216 | } |
| 6217 | #endif /* MS_WINDOWS */ |
| 6218 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6219 | #ifdef HAVE_PLOCK |
| 6220 | |
| 6221 | #ifdef HAVE_SYS_LOCK_H |
| 6222 | #include <sys/lock.h> |
| 6223 | #endif |
| 6224 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6225 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6226 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6227 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6228 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6229 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6230 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6231 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6232 | int op; |
| 6233 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6234 | return NULL; |
| 6235 | if (plock(op) == -1) |
| 6236 | return posix_error(); |
| 6237 | Py_INCREF(Py_None); |
| 6238 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6239 | } |
| 6240 | #endif |
| 6241 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6242 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6243 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6244 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6245 | Set the current process's user id."); |
| 6246 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6247 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6248 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6249 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6250 | uid_t uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6251 | if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6252 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6253 | if (setuid(uid) < 0) |
| 6254 | return posix_error(); |
| 6255 | Py_INCREF(Py_None); |
| 6256 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6257 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6258 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6259 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6260 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6261 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6262 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6263 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6264 | Set the current process's effective user id."); |
| 6265 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6266 | static PyObject * |
| 6267 | posix_seteuid (PyObject *self, PyObject *args) |
| 6268 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6269 | uid_t euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6270 | if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6271 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6272 | if (seteuid(euid) < 0) { |
| 6273 | return posix_error(); |
| 6274 | } else { |
| 6275 | Py_INCREF(Py_None); |
| 6276 | return Py_None; |
| 6277 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6278 | } |
| 6279 | #endif /* HAVE_SETEUID */ |
| 6280 | |
| 6281 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6282 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6283 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6284 | Set the current process's effective group id."); |
| 6285 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6286 | static PyObject * |
| 6287 | posix_setegid (PyObject *self, PyObject *args) |
| 6288 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6289 | gid_t egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6290 | if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6291 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6292 | if (setegid(egid) < 0) { |
| 6293 | return posix_error(); |
| 6294 | } else { |
| 6295 | Py_INCREF(Py_None); |
| 6296 | return Py_None; |
| 6297 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6298 | } |
| 6299 | #endif /* HAVE_SETEGID */ |
| 6300 | |
| 6301 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6302 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6303 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6304 | Set the current process's real and effective user ids."); |
| 6305 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6306 | static PyObject * |
| 6307 | posix_setreuid (PyObject *self, PyObject *args) |
| 6308 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6309 | uid_t ruid, euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6310 | if (!PyArg_ParseTuple(args, "O&O&:setreuid", |
| 6311 | _Py_Uid_Converter, &ruid, |
| 6312 | _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6313 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6314 | if (setreuid(ruid, euid) < 0) { |
| 6315 | return posix_error(); |
| 6316 | } else { |
| 6317 | Py_INCREF(Py_None); |
| 6318 | return Py_None; |
| 6319 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6320 | } |
| 6321 | #endif /* HAVE_SETREUID */ |
| 6322 | |
| 6323 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6324 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6325 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6326 | Set the current process's real and effective group ids."); |
| 6327 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6328 | static PyObject * |
| 6329 | posix_setregid (PyObject *self, PyObject *args) |
| 6330 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6331 | gid_t rgid, egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6332 | if (!PyArg_ParseTuple(args, "O&O&:setregid", |
| 6333 | _Py_Gid_Converter, &rgid, |
| 6334 | _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6335 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6336 | if (setregid(rgid, egid) < 0) { |
| 6337 | return posix_error(); |
| 6338 | } else { |
| 6339 | Py_INCREF(Py_None); |
| 6340 | return Py_None; |
| 6341 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6342 | } |
| 6343 | #endif /* HAVE_SETREGID */ |
| 6344 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6345 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6346 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6347 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6348 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6349 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6350 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6351 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6352 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6353 | gid_t gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6354 | if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6355 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6356 | if (setgid(gid) < 0) |
| 6357 | return posix_error(); |
| 6358 | Py_INCREF(Py_None); |
| 6359 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6360 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6361 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6362 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6363 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6364 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6365 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6366 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6367 | |
| 6368 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6369 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6370 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6371 | int i, len; |
| 6372 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6373 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6374 | if (!PySequence_Check(groups)) { |
| 6375 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6376 | return NULL; |
| 6377 | } |
| 6378 | len = PySequence_Size(groups); |
| 6379 | if (len > MAX_GROUPS) { |
| 6380 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6381 | return NULL; |
| 6382 | } |
| 6383 | for(i = 0; i < len; i++) { |
| 6384 | PyObject *elem; |
| 6385 | elem = PySequence_GetItem(groups, i); |
| 6386 | if (!elem) |
| 6387 | return NULL; |
| 6388 | if (!PyLong_Check(elem)) { |
| 6389 | PyErr_SetString(PyExc_TypeError, |
| 6390 | "groups must be integers"); |
| 6391 | Py_DECREF(elem); |
| 6392 | return NULL; |
| 6393 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6394 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6395 | Py_DECREF(elem); |
| 6396 | return NULL; |
| 6397 | } |
| 6398 | } |
| 6399 | Py_DECREF(elem); |
| 6400 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6402 | if (setgroups(len, grouplist) < 0) |
| 6403 | return posix_error(); |
| 6404 | Py_INCREF(Py_None); |
| 6405 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6406 | } |
| 6407 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6408 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6409 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6410 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6411 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6412 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6413 | PyObject *result; |
| 6414 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6415 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6416 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6417 | if (pid == -1) |
| 6418 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6419 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6420 | if (struct_rusage == NULL) { |
| 6421 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6422 | if (m == NULL) |
| 6423 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6424 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6425 | Py_DECREF(m); |
| 6426 | if (struct_rusage == NULL) |
| 6427 | return NULL; |
| 6428 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6429 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6430 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6431 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6432 | if (!result) |
| 6433 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6434 | |
| 6435 | #ifndef doubletime |
| 6436 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6437 | #endif |
| 6438 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6439 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6440 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6441 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6442 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6443 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6444 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6445 | SET_INT(result, 2, ru->ru_maxrss); |
| 6446 | SET_INT(result, 3, ru->ru_ixrss); |
| 6447 | SET_INT(result, 4, ru->ru_idrss); |
| 6448 | SET_INT(result, 5, ru->ru_isrss); |
| 6449 | SET_INT(result, 6, ru->ru_minflt); |
| 6450 | SET_INT(result, 7, ru->ru_majflt); |
| 6451 | SET_INT(result, 8, ru->ru_nswap); |
| 6452 | SET_INT(result, 9, ru->ru_inblock); |
| 6453 | SET_INT(result, 10, ru->ru_oublock); |
| 6454 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6455 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6456 | SET_INT(result, 13, ru->ru_nsignals); |
| 6457 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6458 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6459 | #undef SET_INT |
| 6460 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6461 | if (PyErr_Occurred()) { |
| 6462 | Py_DECREF(result); |
| 6463 | return NULL; |
| 6464 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6465 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6466 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6467 | } |
| 6468 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6469 | |
| 6470 | #ifdef HAVE_WAIT3 |
| 6471 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6472 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6473 | Wait for completion of a child process."); |
| 6474 | |
| 6475 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6476 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6477 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6478 | pid_t pid; |
| 6479 | int options; |
| 6480 | struct rusage ru; |
| 6481 | WAIT_TYPE status; |
| 6482 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6483 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6484 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6485 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6486 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6487 | Py_BEGIN_ALLOW_THREADS |
| 6488 | pid = wait3(&status, options, &ru); |
| 6489 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6490 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6491 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6492 | } |
| 6493 | #endif /* HAVE_WAIT3 */ |
| 6494 | |
| 6495 | #ifdef HAVE_WAIT4 |
| 6496 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6497 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6498 | Wait for completion of a given child process."); |
| 6499 | |
| 6500 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6501 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6502 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6503 | pid_t pid; |
| 6504 | int options; |
| 6505 | struct rusage ru; |
| 6506 | WAIT_TYPE status; |
| 6507 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6508 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6509 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6510 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6511 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6512 | Py_BEGIN_ALLOW_THREADS |
| 6513 | pid = wait4(pid, &status, options, &ru); |
| 6514 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6515 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6516 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6517 | } |
| 6518 | #endif /* HAVE_WAIT4 */ |
| 6519 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6520 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6521 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6522 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6523 | Wait for the completion of one or more child processes.\n\n\ |
| 6524 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6525 | id specifies the pid to wait on.\n\ |
| 6526 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6527 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6528 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6529 | no children in a waitable state."); |
| 6530 | |
| 6531 | static PyObject * |
| 6532 | posix_waitid(PyObject *self, PyObject *args) |
| 6533 | { |
| 6534 | PyObject *result; |
| 6535 | idtype_t idtype; |
| 6536 | id_t id; |
| 6537 | int options, res; |
| 6538 | siginfo_t si; |
| 6539 | si.si_pid = 0; |
| 6540 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6541 | return NULL; |
| 6542 | Py_BEGIN_ALLOW_THREADS |
| 6543 | res = waitid(idtype, id, &si, options); |
| 6544 | Py_END_ALLOW_THREADS |
| 6545 | if (res == -1) |
| 6546 | return posix_error(); |
| 6547 | |
| 6548 | if (si.si_pid == 0) |
| 6549 | Py_RETURN_NONE; |
| 6550 | |
| 6551 | result = PyStructSequence_New(&WaitidResultType); |
| 6552 | if (!result) |
| 6553 | return NULL; |
| 6554 | |
| 6555 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6556 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6557 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6558 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6559 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6560 | if (PyErr_Occurred()) { |
| 6561 | Py_DECREF(result); |
| 6562 | return NULL; |
| 6563 | } |
| 6564 | |
| 6565 | return result; |
| 6566 | } |
| 6567 | #endif |
| 6568 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6569 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6570 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6571 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6572 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6573 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6574 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6575 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6576 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6577 | pid_t pid; |
| 6578 | int options; |
| 6579 | WAIT_TYPE status; |
| 6580 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6581 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6582 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6583 | return NULL; |
| 6584 | Py_BEGIN_ALLOW_THREADS |
| 6585 | pid = waitpid(pid, &status, options); |
| 6586 | Py_END_ALLOW_THREADS |
| 6587 | if (pid == -1) |
| 6588 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6589 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6590 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6593 | #elif defined(HAVE_CWAIT) |
| 6594 | |
| 6595 | /* 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] | 6596 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6597 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6598 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6599 | |
| 6600 | static PyObject * |
| 6601 | posix_waitpid(PyObject *self, PyObject *args) |
| 6602 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6603 | Py_intptr_t pid; |
| 6604 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6605 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6606 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6607 | return NULL; |
| 6608 | Py_BEGIN_ALLOW_THREADS |
| 6609 | pid = _cwait(&status, pid, options); |
| 6610 | Py_END_ALLOW_THREADS |
| 6611 | if (pid == -1) |
| 6612 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6613 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6614 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6615 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6616 | } |
| 6617 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6618 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6619 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6620 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6621 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6622 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6623 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6624 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6625 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6626 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6627 | pid_t pid; |
| 6628 | WAIT_TYPE status; |
| 6629 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6630 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6631 | Py_BEGIN_ALLOW_THREADS |
| 6632 | pid = wait(&status); |
| 6633 | Py_END_ALLOW_THREADS |
| 6634 | if (pid == -1) |
| 6635 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6636 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6637 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6638 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6639 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6640 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6641 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6642 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 6643 | PyDoc_STRVAR(readlink__doc__, |
| 6644 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 6645 | Return a string representing the path to which the symbolic link points.\n\ |
| 6646 | \n\ |
| 6647 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 6648 | and path should be relative; path will then be relative to that directory.\n\ |
| 6649 | dir_fd may not be implemented on your platform.\n\ |
| 6650 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6651 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6652 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6653 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6654 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6655 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6656 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6657 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6658 | path_t path; |
| 6659 | int dir_fd = DEFAULT_DIR_FD; |
| 6660 | char buffer[MAXPATHLEN]; |
| 6661 | ssize_t length; |
| 6662 | PyObject *return_value = NULL; |
| 6663 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6664 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6665 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6666 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6667 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 6668 | path_converter, &path, |
| 6669 | #ifdef HAVE_READLINKAT |
| 6670 | dir_fd_converter, &dir_fd |
| 6671 | #else |
| 6672 | dir_fd_unavailable, &dir_fd |
| 6673 | #endif |
| 6674 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6675 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6676 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6677 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6678 | #ifdef HAVE_READLINKAT |
| 6679 | if (dir_fd != DEFAULT_DIR_FD) |
| 6680 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6681 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6682 | #endif |
| 6683 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 6684 | Py_END_ALLOW_THREADS |
| 6685 | |
| 6686 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6687 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6688 | goto exit; |
| 6689 | } |
| 6690 | |
| 6691 | if (PyUnicode_Check(path.object)) |
| 6692 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 6693 | else |
| 6694 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 6695 | exit: |
| 6696 | path_cleanup(&path); |
| 6697 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6698 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6699 | |
| 6700 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6701 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6702 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6703 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6704 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6705 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6706 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 6707 | Create a symbolic link pointing to src named dst.\n\n\ |
| 6708 | target_is_directory is required on Windows if the target is to be\n\ |
| 6709 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 6710 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 6711 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 6712 | \n\ |
| 6713 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 6714 | and path should be relative; path will then be relative to that directory.\n\ |
| 6715 | dir_fd may not be implemented on your platform.\n\ |
| 6716 | If it is unavailable, using it will raise a NotImplementedError."); |
| 6717 | |
| 6718 | #if defined(MS_WINDOWS) |
| 6719 | |
| 6720 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6721 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 6722 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
| 6723 | static int |
| 6724 | check_CreateSymbolicLink() |
| 6725 | { |
| 6726 | HINSTANCE hKernel32; |
| 6727 | /* only recheck */ |
| 6728 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 6729 | return 1; |
| 6730 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 6731 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6732 | "CreateSymbolicLinkW"); |
| 6733 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 6734 | "CreateSymbolicLinkA"); |
| 6735 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 6736 | } |
| 6737 | |
| 6738 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6739 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6740 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6741 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6742 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6743 | path_t src; |
| 6744 | path_t dst; |
| 6745 | int dir_fd = DEFAULT_DIR_FD; |
| 6746 | int target_is_directory = 0; |
| 6747 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 6748 | "dir_fd", NULL}; |
| 6749 | PyObject *return_value; |
| 6750 | #ifdef MS_WINDOWS |
| 6751 | DWORD result; |
| 6752 | #else |
| 6753 | int result; |
| 6754 | #endif |
| 6755 | |
| 6756 | memset(&src, 0, sizeof(src)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6757 | src.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6758 | src.argument_name = "src"; |
| 6759 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6760 | dst.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6761 | dst.argument_name = "dst"; |
| 6762 | |
| 6763 | #ifdef MS_WINDOWS |
| 6764 | if (!check_CreateSymbolicLink()) { |
| 6765 | PyErr_SetString(PyExc_NotImplementedError, |
| 6766 | "CreateSymbolicLink functions not found"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 6767 | return NULL; |
| 6768 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6769 | if (!win32_can_symlink) { |
| 6770 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 6771 | return NULL; |
| 6772 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6773 | #endif |
| 6774 | |
| 6775 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 6776 | keywords, |
| 6777 | path_converter, &src, |
| 6778 | path_converter, &dst, |
| 6779 | &target_is_directory, |
| 6780 | #ifdef HAVE_SYMLINKAT |
| 6781 | dir_fd_converter, &dir_fd |
| 6782 | #else |
| 6783 | dir_fd_unavailable, &dir_fd |
| 6784 | #endif |
| 6785 | )) |
| 6786 | return NULL; |
| 6787 | |
| 6788 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 6789 | PyErr_SetString(PyExc_ValueError, |
| 6790 | "symlink: src and dst must be the same type"); |
| 6791 | return_value = NULL; |
| 6792 | goto exit; |
| 6793 | } |
| 6794 | |
| 6795 | #ifdef MS_WINDOWS |
| 6796 | Py_BEGIN_ALLOW_THREADS |
| 6797 | if (dst.wide) |
| 6798 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 6799 | target_is_directory); |
| 6800 | else |
| 6801 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 6802 | target_is_directory); |
| 6803 | Py_END_ALLOW_THREADS |
| 6804 | |
| 6805 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 6806 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6807 | goto exit; |
| 6808 | } |
| 6809 | |
| 6810 | #else |
| 6811 | |
| 6812 | Py_BEGIN_ALLOW_THREADS |
| 6813 | #if HAVE_SYMLINKAT |
| 6814 | if (dir_fd != DEFAULT_DIR_FD) |
| 6815 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 6816 | else |
| 6817 | #endif |
| 6818 | result = symlink(src.narrow, dst.narrow); |
| 6819 | Py_END_ALLOW_THREADS |
| 6820 | |
| 6821 | if (result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 6822 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6823 | goto exit; |
| 6824 | } |
| 6825 | #endif |
| 6826 | |
| 6827 | return_value = Py_None; |
| 6828 | Py_INCREF(Py_None); |
| 6829 | goto exit; /* silence "unused label" warning */ |
| 6830 | exit: |
| 6831 | path_cleanup(&src); |
| 6832 | path_cleanup(&dst); |
| 6833 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6834 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6835 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6836 | #endif /* HAVE_SYMLINK */ |
| 6837 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6838 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6839 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6840 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6841 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6842 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6843 | { |
| 6844 | wchar_t *path; |
| 6845 | DWORD n_bytes_returned; |
| 6846 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6847 | PyObject *po, *result; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 6848 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6849 | HANDLE reparse_point_handle; |
| 6850 | |
| 6851 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6852 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6853 | wchar_t *print_name; |
| 6854 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6855 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 6856 | |
| 6857 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 6858 | &po, |
| 6859 | dir_fd_unavailable, &dir_fd |
| 6860 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6861 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6862 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6863 | path = PyUnicode_AsUnicode(po); |
| 6864 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6865 | return NULL; |
| 6866 | |
| 6867 | /* First get a handle to the reparse point */ |
| 6868 | Py_BEGIN_ALLOW_THREADS |
| 6869 | reparse_point_handle = CreateFileW( |
| 6870 | path, |
| 6871 | 0, |
| 6872 | 0, |
| 6873 | 0, |
| 6874 | OPEN_EXISTING, |
| 6875 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6876 | 0); |
| 6877 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6878 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6879 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6880 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6881 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6882 | Py_BEGIN_ALLOW_THREADS |
| 6883 | /* New call DeviceIoControl to read the reparse point */ |
| 6884 | io_result = DeviceIoControl( |
| 6885 | reparse_point_handle, |
| 6886 | FSCTL_GET_REPARSE_POINT, |
| 6887 | 0, 0, /* in buffer */ |
| 6888 | target_buffer, sizeof(target_buffer), |
| 6889 | &n_bytes_returned, |
| 6890 | 0 /* we're not using OVERLAPPED_IO */ |
| 6891 | ); |
| 6892 | CloseHandle(reparse_point_handle); |
| 6893 | Py_END_ALLOW_THREADS |
| 6894 | |
| 6895 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6896 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6897 | |
| 6898 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6899 | { |
| 6900 | PyErr_SetString(PyExc_ValueError, |
| 6901 | "not a symbolic link"); |
| 6902 | return NULL; |
| 6903 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6904 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6905 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6906 | |
| 6907 | result = PyUnicode_FromWideChar(print_name, |
| 6908 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6909 | return result; |
| 6910 | } |
| 6911 | |
| 6912 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6913 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6914 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6915 | static PyStructSequence_Field times_result_fields[] = { |
| 6916 | {"user", "user time"}, |
| 6917 | {"system", "system time"}, |
| 6918 | {"children_user", "user time of children"}, |
| 6919 | {"children_system", "system time of children"}, |
| 6920 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 6921 | {NULL} |
| 6922 | }; |
| 6923 | |
| 6924 | PyDoc_STRVAR(times_result__doc__, |
| 6925 | "times_result: Result from os.times().\n\n\ |
| 6926 | This object may be accessed either as a tuple of\n\ |
| 6927 | (user, system, children_user, children_system, elapsed),\n\ |
| 6928 | or via the attributes user, system, children_user, children_system,\n\ |
| 6929 | and elapsed.\n\ |
| 6930 | \n\ |
| 6931 | See os.times for more information."); |
| 6932 | |
| 6933 | static PyStructSequence_Desc times_result_desc = { |
| 6934 | "times_result", /* name */ |
| 6935 | times_result__doc__, /* doc */ |
| 6936 | times_result_fields, |
| 6937 | 5 |
| 6938 | }; |
| 6939 | |
| 6940 | static PyTypeObject TimesResultType; |
| 6941 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 6942 | #ifdef MS_WINDOWS |
| 6943 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 6944 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6945 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 6946 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6947 | |
| 6948 | static PyObject * |
| 6949 | build_times_result(double user, double system, |
| 6950 | double children_user, double children_system, |
| 6951 | double elapsed) |
| 6952 | { |
| 6953 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 6954 | if (value == NULL) |
| 6955 | return NULL; |
| 6956 | |
| 6957 | #define SET(i, field) \ |
| 6958 | { \ |
| 6959 | PyObject *o = PyFloat_FromDouble(field); \ |
| 6960 | if (!o) { \ |
| 6961 | Py_DECREF(value); \ |
| 6962 | return NULL; \ |
| 6963 | } \ |
| 6964 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 6965 | } \ |
| 6966 | |
| 6967 | SET(0, user); |
| 6968 | SET(1, system); |
| 6969 | SET(2, children_user); |
| 6970 | SET(3, children_system); |
| 6971 | SET(4, elapsed); |
| 6972 | |
| 6973 | #undef SET |
| 6974 | |
| 6975 | return value; |
| 6976 | } |
| 6977 | |
| 6978 | PyDoc_STRVAR(posix_times__doc__, |
| 6979 | "times() -> times_result\n\n\ |
| 6980 | Return an object containing floating point numbers indicating process\n\ |
| 6981 | times. The object behaves like a named tuple with these fields:\n\ |
| 6982 | (utime, stime, cutime, cstime, elapsed_time)"); |
| 6983 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 6984 | #if defined(MS_WINDOWS) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6985 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6986 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6987 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6988 | FILETIME create, exit, kernel, user; |
| 6989 | HANDLE hProc; |
| 6990 | hProc = GetCurrentProcess(); |
| 6991 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6992 | /* The fields of a FILETIME structure are the hi and lo part |
| 6993 | of a 64-bit value expressed in 100 nanosecond units. |
| 6994 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6995 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6996 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6997 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6998 | (double)(user.dwHighDateTime*429.4967296 + |
| 6999 | user.dwLowDateTime*1e-7), |
| 7000 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7001 | kernel.dwLowDateTime*1e-7), |
| 7002 | (double)0, |
| 7003 | (double)0, |
| 7004 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7005 | } |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 7006 | #else /* Not Windows */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7007 | #define NEED_TICKS_PER_SECOND |
| 7008 | static long ticks_per_second = -1; |
| 7009 | static PyObject * |
| 7010 | posix_times(PyObject *self, PyObject *noargs) |
| 7011 | { |
| 7012 | struct tms t; |
| 7013 | clock_t c; |
| 7014 | errno = 0; |
| 7015 | c = times(&t); |
| 7016 | if (c == (clock_t) -1) |
| 7017 | return posix_error(); |
| 7018 | return build_times_result( |
| 7019 | (double)t.tms_utime / ticks_per_second, |
| 7020 | (double)t.tms_stime / ticks_per_second, |
| 7021 | (double)t.tms_cutime / ticks_per_second, |
| 7022 | (double)t.tms_cstime / ticks_per_second, |
| 7023 | (double)c / ticks_per_second); |
| 7024 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7025 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7026 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7027 | #endif /* HAVE_TIMES */ |
| 7028 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7029 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7030 | #ifdef HAVE_GETSID |
| 7031 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7032 | "getsid(pid) -> sid\n\n\ |
| 7033 | Call the system call getsid()."); |
| 7034 | |
| 7035 | static PyObject * |
| 7036 | posix_getsid(PyObject *self, PyObject *args) |
| 7037 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7038 | pid_t pid; |
| 7039 | int sid; |
| 7040 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7041 | return NULL; |
| 7042 | sid = getsid(pid); |
| 7043 | if (sid < 0) |
| 7044 | return posix_error(); |
| 7045 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7046 | } |
| 7047 | #endif /* HAVE_GETSID */ |
| 7048 | |
| 7049 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7050 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7051 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7052 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7053 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7054 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7055 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7056 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7057 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7058 | if (setsid() < 0) |
| 7059 | return posix_error(); |
| 7060 | Py_INCREF(Py_None); |
| 7061 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7062 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7063 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7064 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7065 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7066 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7067 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7068 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7069 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7070 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7071 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7072 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7073 | pid_t pid; |
| 7074 | int pgrp; |
| 7075 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7076 | return NULL; |
| 7077 | if (setpgid(pid, pgrp) < 0) |
| 7078 | return posix_error(); |
| 7079 | Py_INCREF(Py_None); |
| 7080 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7081 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7082 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7083 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7084 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7085 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7086 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7087 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7088 | 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] | 7089 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7090 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7091 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7092 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7093 | int fd; |
| 7094 | pid_t pgid; |
| 7095 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7096 | return NULL; |
| 7097 | pgid = tcgetpgrp(fd); |
| 7098 | if (pgid < 0) |
| 7099 | return posix_error(); |
| 7100 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7101 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7102 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7103 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7104 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7105 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7106 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7107 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7108 | 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] | 7109 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7110 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7111 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7112 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7113 | int fd; |
| 7114 | pid_t pgid; |
| 7115 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7116 | return NULL; |
| 7117 | if (tcsetpgrp(fd, pgid) < 0) |
| 7118 | return posix_error(); |
| 7119 | Py_INCREF(Py_None); |
| 7120 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7121 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7122 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7123 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7124 | /* Functions acting on file descriptors */ |
| 7125 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7126 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7127 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7128 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7129 | \n\ |
| 7130 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7131 | and path should be relative; path will then be relative to that directory.\n\ |
| 7132 | dir_fd may not be implemented on your platform.\n\ |
| 7133 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7134 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7135 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7136 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7137 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7138 | path_t path; |
| 7139 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7140 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7141 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7142 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7143 | PyObject *return_value = NULL; |
| 7144 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7145 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7146 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7147 | path.function_name = "open"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7148 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7149 | path_converter, &path, |
| 7150 | &flags, &mode, |
| 7151 | #ifdef HAVE_OPENAT |
| 7152 | dir_fd_converter, &dir_fd |
| 7153 | #else |
| 7154 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7155 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7156 | )) |
| 7157 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7158 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7159 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7160 | #ifdef MS_WINDOWS |
| 7161 | if (path.wide) |
| 7162 | fd = _wopen(path.wide, flags, mode); |
| 7163 | else |
| 7164 | #endif |
| 7165 | #ifdef HAVE_OPENAT |
| 7166 | if (dir_fd != DEFAULT_DIR_FD) |
| 7167 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7168 | else |
| 7169 | #endif |
| 7170 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7171 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7172 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7173 | if (fd == -1) { |
Victor Stinner | 4e7d2d4 | 2012-11-05 01:20:58 +0100 | [diff] [blame] | 7174 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path.object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7175 | goto exit; |
| 7176 | } |
| 7177 | |
| 7178 | return_value = PyLong_FromLong((long)fd); |
| 7179 | |
| 7180 | exit: |
| 7181 | path_cleanup(&path); |
| 7182 | return return_value; |
| 7183 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7184 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7185 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7186 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7187 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7188 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7189 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7190 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7191 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7192 | int fd, res; |
| 7193 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7194 | return NULL; |
| 7195 | if (!_PyVerify_fd(fd)) |
| 7196 | return posix_error(); |
| 7197 | Py_BEGIN_ALLOW_THREADS |
| 7198 | res = close(fd); |
| 7199 | Py_END_ALLOW_THREADS |
| 7200 | if (res < 0) |
| 7201 | return posix_error(); |
| 7202 | Py_INCREF(Py_None); |
| 7203 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7204 | } |
| 7205 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7206 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7207 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7208 | "closerange(fd_low, fd_high)\n\n\ |
| 7209 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7210 | |
| 7211 | static PyObject * |
| 7212 | posix_closerange(PyObject *self, PyObject *args) |
| 7213 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7214 | int fd_from, fd_to, i; |
| 7215 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7216 | return NULL; |
| 7217 | Py_BEGIN_ALLOW_THREADS |
| 7218 | for (i = fd_from; i < fd_to; i++) |
| 7219 | if (_PyVerify_fd(i)) |
| 7220 | close(i); |
| 7221 | Py_END_ALLOW_THREADS |
| 7222 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7223 | } |
| 7224 | |
| 7225 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7226 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7227 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7228 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7229 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7230 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7231 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7232 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7233 | int fd; |
| 7234 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7235 | return NULL; |
| 7236 | if (!_PyVerify_fd(fd)) |
| 7237 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7238 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7239 | if (fd < 0) |
| 7240 | return posix_error(); |
| 7241 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7242 | } |
| 7243 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7245 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7246 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7247 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7248 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7249 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7250 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7251 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7252 | int fd, fd2, res; |
| 7253 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 7254 | return NULL; |
| 7255 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7256 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7257 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7258 | if (res < 0) |
| 7259 | return posix_error(); |
| 7260 | Py_INCREF(Py_None); |
| 7261 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7262 | } |
| 7263 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7264 | #ifdef HAVE_LOCKF |
| 7265 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7266 | "lockf(fd, cmd, len)\n\n\ |
| 7267 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7268 | fd is an open file descriptor.\n\ |
| 7269 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7270 | F_TEST.\n\ |
| 7271 | len specifies the section of the file to lock."); |
| 7272 | |
| 7273 | static PyObject * |
| 7274 | posix_lockf(PyObject *self, PyObject *args) |
| 7275 | { |
| 7276 | int fd, cmd, res; |
| 7277 | off_t len; |
| 7278 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7279 | &fd, &cmd, _parse_off_t, &len)) |
| 7280 | return NULL; |
| 7281 | |
| 7282 | Py_BEGIN_ALLOW_THREADS |
| 7283 | res = lockf(fd, cmd, len); |
| 7284 | Py_END_ALLOW_THREADS |
| 7285 | |
| 7286 | if (res < 0) |
| 7287 | return posix_error(); |
| 7288 | |
| 7289 | Py_RETURN_NONE; |
| 7290 | } |
| 7291 | #endif |
| 7292 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7294 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7295 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7296 | Set the current position of a file descriptor.\n\ |
| 7297 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7298 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7299 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7300 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7301 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7302 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7303 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7304 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7305 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7306 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7307 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7308 | PyObject *posobj; |
| 7309 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7310 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7311 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7312 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7313 | switch (how) { |
| 7314 | case 0: how = SEEK_SET; break; |
| 7315 | case 1: how = SEEK_CUR; break; |
| 7316 | case 2: how = SEEK_END; break; |
| 7317 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7318 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7319 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7320 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7321 | pos = PyLong_AsLong(posobj); |
| 7322 | #else |
| 7323 | pos = PyLong_AsLongLong(posobj); |
| 7324 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7325 | if (PyErr_Occurred()) |
| 7326 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7327 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7328 | if (!_PyVerify_fd(fd)) |
| 7329 | return posix_error(); |
| 7330 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7331 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7332 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7333 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7334 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7335 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7336 | Py_END_ALLOW_THREADS |
| 7337 | if (res < 0) |
| 7338 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7339 | |
| 7340 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7341 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7342 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7343 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7344 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7345 | } |
| 7346 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7347 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7348 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7349 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7350 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7351 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7352 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7353 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7354 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7355 | int fd, size; |
| 7356 | Py_ssize_t n; |
| 7357 | PyObject *buffer; |
| 7358 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7359 | return NULL; |
| 7360 | if (size < 0) { |
| 7361 | errno = EINVAL; |
| 7362 | return posix_error(); |
| 7363 | } |
| 7364 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7365 | if (buffer == NULL) |
| 7366 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7367 | if (!_PyVerify_fd(fd)) { |
| 7368 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7369 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7370 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7371 | Py_BEGIN_ALLOW_THREADS |
| 7372 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7373 | Py_END_ALLOW_THREADS |
| 7374 | if (n < 0) { |
| 7375 | Py_DECREF(buffer); |
| 7376 | return posix_error(); |
| 7377 | } |
| 7378 | if (n != size) |
| 7379 | _PyBytes_Resize(&buffer, n); |
| 7380 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7383 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7384 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7385 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7386 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7387 | { |
| 7388 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7389 | Py_ssize_t blen, total = 0; |
| 7390 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7391 | *iov = PyMem_New(struct iovec, cnt); |
| 7392 | if (*iov == NULL) { |
| 7393 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7394 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7395 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7396 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7397 | *buf = PyMem_New(Py_buffer, cnt); |
| 7398 | if (*buf == NULL) { |
| 7399 | PyMem_Del(*iov); |
| 7400 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7401 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7402 | } |
| 7403 | |
| 7404 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7405 | PyObject *item = PySequence_GetItem(seq, i); |
| 7406 | if (item == NULL) |
| 7407 | goto fail; |
| 7408 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7409 | Py_DECREF(item); |
| 7410 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7411 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7412 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7413 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7414 | blen = (*buf)[i].len; |
| 7415 | (*iov)[i].iov_len = blen; |
| 7416 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7417 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7418 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7419 | |
| 7420 | fail: |
| 7421 | PyMem_Del(*iov); |
| 7422 | for (j = 0; j < i; j++) { |
| 7423 | PyBuffer_Release(&(*buf)[j]); |
| 7424 | } |
| 7425 | PyMem_Del(*buf); |
| 7426 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7427 | } |
| 7428 | |
| 7429 | static void |
| 7430 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7431 | { |
| 7432 | int i; |
| 7433 | PyMem_Del(iov); |
| 7434 | for (i = 0; i < cnt; i++) { |
| 7435 | PyBuffer_Release(&buf[i]); |
| 7436 | } |
| 7437 | PyMem_Del(buf); |
| 7438 | } |
| 7439 | #endif |
| 7440 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7441 | #ifdef HAVE_READV |
| 7442 | PyDoc_STRVAR(posix_readv__doc__, |
| 7443 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7444 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7445 | is an arbitrary sequence of writable buffers.\n\ |
| 7446 | Returns the total number of bytes read."); |
| 7447 | |
| 7448 | static PyObject * |
| 7449 | posix_readv(PyObject *self, PyObject *args) |
| 7450 | { |
| 7451 | int fd, cnt; |
| 7452 | Py_ssize_t n; |
| 7453 | PyObject *seq; |
| 7454 | struct iovec *iov; |
| 7455 | Py_buffer *buf; |
| 7456 | |
| 7457 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7458 | return NULL; |
| 7459 | if (!PySequence_Check(seq)) { |
| 7460 | PyErr_SetString(PyExc_TypeError, |
| 7461 | "readv() arg 2 must be a sequence"); |
| 7462 | return NULL; |
| 7463 | } |
| 7464 | cnt = PySequence_Size(seq); |
| 7465 | |
| 7466 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7467 | return NULL; |
| 7468 | |
| 7469 | Py_BEGIN_ALLOW_THREADS |
| 7470 | n = readv(fd, iov, cnt); |
| 7471 | Py_END_ALLOW_THREADS |
| 7472 | |
| 7473 | iov_cleanup(iov, buf, cnt); |
| 7474 | return PyLong_FromSsize_t(n); |
| 7475 | } |
| 7476 | #endif |
| 7477 | |
| 7478 | #ifdef HAVE_PREAD |
| 7479 | PyDoc_STRVAR(posix_pread__doc__, |
| 7480 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7481 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7482 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7483 | |
| 7484 | static PyObject * |
| 7485 | posix_pread(PyObject *self, PyObject *args) |
| 7486 | { |
| 7487 | int fd, size; |
| 7488 | off_t offset; |
| 7489 | Py_ssize_t n; |
| 7490 | PyObject *buffer; |
| 7491 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7492 | return NULL; |
| 7493 | |
| 7494 | if (size < 0) { |
| 7495 | errno = EINVAL; |
| 7496 | return posix_error(); |
| 7497 | } |
| 7498 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7499 | if (buffer == NULL) |
| 7500 | return NULL; |
| 7501 | if (!_PyVerify_fd(fd)) { |
| 7502 | Py_DECREF(buffer); |
| 7503 | return posix_error(); |
| 7504 | } |
| 7505 | Py_BEGIN_ALLOW_THREADS |
| 7506 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7507 | Py_END_ALLOW_THREADS |
| 7508 | if (n < 0) { |
| 7509 | Py_DECREF(buffer); |
| 7510 | return posix_error(); |
| 7511 | } |
| 7512 | if (n != size) |
| 7513 | _PyBytes_Resize(&buffer, n); |
| 7514 | return buffer; |
| 7515 | } |
| 7516 | #endif |
| 7517 | |
| 7518 | PyDoc_STRVAR(posix_write__doc__, |
| 7519 | "write(fd, string) -> byteswritten\n\n\ |
| 7520 | Write a string to a file descriptor."); |
| 7521 | |
| 7522 | static PyObject * |
| 7523 | posix_write(PyObject *self, PyObject *args) |
| 7524 | { |
| 7525 | Py_buffer pbuf; |
| 7526 | int fd; |
| 7527 | Py_ssize_t size, len; |
| 7528 | |
| 7529 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7530 | return NULL; |
| 7531 | if (!_PyVerify_fd(fd)) { |
| 7532 | PyBuffer_Release(&pbuf); |
| 7533 | return posix_error(); |
| 7534 | } |
| 7535 | len = pbuf.len; |
| 7536 | Py_BEGIN_ALLOW_THREADS |
| 7537 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7538 | if (len > INT_MAX) |
| 7539 | len = INT_MAX; |
| 7540 | size = write(fd, pbuf.buf, (int)len); |
| 7541 | #else |
| 7542 | size = write(fd, pbuf.buf, len); |
| 7543 | #endif |
| 7544 | Py_END_ALLOW_THREADS |
| 7545 | PyBuffer_Release(&pbuf); |
| 7546 | if (size < 0) |
| 7547 | return posix_error(); |
| 7548 | return PyLong_FromSsize_t(size); |
| 7549 | } |
| 7550 | |
| 7551 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7552 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7553 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7554 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7555 | -> byteswritten\n\ |
| 7556 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7557 | |
| 7558 | static PyObject * |
| 7559 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7560 | { |
| 7561 | int in, out; |
| 7562 | Py_ssize_t ret; |
| 7563 | off_t offset; |
| 7564 | |
| 7565 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7566 | #ifndef __APPLE__ |
| 7567 | Py_ssize_t len; |
| 7568 | #endif |
| 7569 | PyObject *headers = NULL, *trailers = NULL; |
| 7570 | Py_buffer *hbuf, *tbuf; |
| 7571 | off_t sbytes; |
| 7572 | struct sf_hdtr sf; |
| 7573 | int flags = 0; |
| 7574 | sf.headers = NULL; |
| 7575 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7576 | static char *keywords[] = {"out", "in", |
| 7577 | "offset", "count", |
| 7578 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7579 | |
| 7580 | #ifdef __APPLE__ |
| 7581 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7582 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7583 | #else |
| 7584 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7585 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7586 | #endif |
| 7587 | &headers, &trailers, &flags)) |
| 7588 | return NULL; |
| 7589 | if (headers != NULL) { |
| 7590 | if (!PySequence_Check(headers)) { |
| 7591 | PyErr_SetString(PyExc_TypeError, |
| 7592 | "sendfile() headers must be a sequence or None"); |
| 7593 | return NULL; |
| 7594 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7595 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7596 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7597 | if (sf.hdr_cnt > 0 && |
| 7598 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7599 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7600 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7601 | #ifdef __APPLE__ |
| 7602 | sbytes += i; |
| 7603 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7604 | } |
| 7605 | } |
| 7606 | if (trailers != NULL) { |
| 7607 | if (!PySequence_Check(trailers)) { |
| 7608 | PyErr_SetString(PyExc_TypeError, |
| 7609 | "sendfile() trailers must be a sequence or None"); |
| 7610 | return NULL; |
| 7611 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7612 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7613 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7614 | if (sf.trl_cnt > 0 && |
| 7615 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7616 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7617 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7618 | #ifdef __APPLE__ |
| 7619 | sbytes += i; |
| 7620 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7621 | } |
| 7622 | } |
| 7623 | |
| 7624 | Py_BEGIN_ALLOW_THREADS |
| 7625 | #ifdef __APPLE__ |
| 7626 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7627 | #else |
| 7628 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7629 | #endif |
| 7630 | Py_END_ALLOW_THREADS |
| 7631 | |
| 7632 | if (sf.headers != NULL) |
| 7633 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7634 | if (sf.trailers != NULL) |
| 7635 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7636 | |
| 7637 | if (ret < 0) { |
| 7638 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7639 | if (sbytes != 0) { |
| 7640 | // some data has been sent |
| 7641 | goto done; |
| 7642 | } |
| 7643 | else { |
| 7644 | // no data has been sent; upper application is supposed |
| 7645 | // to retry on EAGAIN or EBUSY |
| 7646 | return posix_error(); |
| 7647 | } |
| 7648 | } |
| 7649 | return posix_error(); |
| 7650 | } |
| 7651 | goto done; |
| 7652 | |
| 7653 | done: |
| 7654 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7655 | return Py_BuildValue("l", sbytes); |
| 7656 | #else |
| 7657 | return Py_BuildValue("L", sbytes); |
| 7658 | #endif |
| 7659 | |
| 7660 | #else |
| 7661 | Py_ssize_t count; |
| 7662 | PyObject *offobj; |
| 7663 | static char *keywords[] = {"out", "in", |
| 7664 | "offset", "count", NULL}; |
| 7665 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7666 | keywords, &out, &in, &offobj, &count)) |
| 7667 | return NULL; |
| 7668 | #ifdef linux |
| 7669 | if (offobj == Py_None) { |
| 7670 | Py_BEGIN_ALLOW_THREADS |
| 7671 | ret = sendfile(out, in, NULL, count); |
| 7672 | Py_END_ALLOW_THREADS |
| 7673 | if (ret < 0) |
| 7674 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7675 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7676 | } |
| 7677 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7678 | if (!_parse_off_t(offobj, &offset)) |
| 7679 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7680 | Py_BEGIN_ALLOW_THREADS |
| 7681 | ret = sendfile(out, in, &offset, count); |
| 7682 | Py_END_ALLOW_THREADS |
| 7683 | if (ret < 0) |
| 7684 | return posix_error(); |
| 7685 | return Py_BuildValue("n", ret); |
| 7686 | #endif |
| 7687 | } |
| 7688 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7689 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7690 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7691 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7692 | Like stat(), but for an open file descriptor.\n\ |
| 7693 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7694 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7695 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7696 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7697 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7698 | int fd; |
| 7699 | STRUCT_STAT st; |
| 7700 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7701 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7702 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7703 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7704 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7705 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7706 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7707 | Py_BEGIN_ALLOW_THREADS |
| 7708 | res = FSTAT(fd, &st); |
| 7709 | Py_END_ALLOW_THREADS |
| 7710 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7711 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 7712 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7713 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7714 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7715 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7716 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7717 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7718 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7719 | } |
| 7720 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7721 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7722 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7723 | 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] | 7724 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7725 | |
| 7726 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7727 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7728 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7729 | int fd; |
| 7730 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7731 | return NULL; |
| 7732 | if (!_PyVerify_fd(fd)) |
| 7733 | return PyBool_FromLong(0); |
| 7734 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7735 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7736 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7737 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7738 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7739 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7740 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7741 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7742 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7743 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7744 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7745 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7746 | int fds[2]; |
| 7747 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7748 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7749 | if (res != 0) |
| 7750 | return posix_error(); |
| 7751 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7752 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7753 | HANDLE read, write; |
| 7754 | int read_fd, write_fd; |
| 7755 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7756 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7757 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 7758 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7759 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7760 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7761 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7762 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7763 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7764 | #endif /* HAVE_PIPE */ |
| 7765 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7766 | #ifdef HAVE_PIPE2 |
| 7767 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7768 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7769 | Create a pipe with flags set atomically.\n\ |
| 7770 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7771 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7772 | "); |
| 7773 | |
| 7774 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7775 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7776 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7777 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7778 | int fds[2]; |
| 7779 | int res; |
| 7780 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 7781 | flags = _PyLong_AsInt(arg); |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7782 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7783 | return NULL; |
| 7784 | |
| 7785 | res = pipe2(fds, flags); |
| 7786 | if (res != 0) |
| 7787 | return posix_error(); |
| 7788 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7789 | } |
| 7790 | #endif /* HAVE_PIPE2 */ |
| 7791 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7792 | #ifdef HAVE_WRITEV |
| 7793 | PyDoc_STRVAR(posix_writev__doc__, |
| 7794 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7795 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7796 | arbitrary sequence of buffers.\n\ |
| 7797 | Returns the total bytes written."); |
| 7798 | |
| 7799 | static PyObject * |
| 7800 | posix_writev(PyObject *self, PyObject *args) |
| 7801 | { |
| 7802 | int fd, cnt; |
| 7803 | Py_ssize_t res; |
| 7804 | PyObject *seq; |
| 7805 | struct iovec *iov; |
| 7806 | Py_buffer *buf; |
| 7807 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7808 | return NULL; |
| 7809 | if (!PySequence_Check(seq)) { |
| 7810 | PyErr_SetString(PyExc_TypeError, |
| 7811 | "writev() arg 2 must be a sequence"); |
| 7812 | return NULL; |
| 7813 | } |
| 7814 | cnt = PySequence_Size(seq); |
| 7815 | |
| 7816 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7817 | return NULL; |
| 7818 | } |
| 7819 | |
| 7820 | Py_BEGIN_ALLOW_THREADS |
| 7821 | res = writev(fd, iov, cnt); |
| 7822 | Py_END_ALLOW_THREADS |
| 7823 | |
| 7824 | iov_cleanup(iov, buf, cnt); |
| 7825 | return PyLong_FromSsize_t(res); |
| 7826 | } |
| 7827 | #endif |
| 7828 | |
| 7829 | #ifdef HAVE_PWRITE |
| 7830 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7831 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7832 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7833 | offset unchanged."); |
| 7834 | |
| 7835 | static PyObject * |
| 7836 | posix_pwrite(PyObject *self, PyObject *args) |
| 7837 | { |
| 7838 | Py_buffer pbuf; |
| 7839 | int fd; |
| 7840 | off_t offset; |
| 7841 | Py_ssize_t size; |
| 7842 | |
| 7843 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7844 | return NULL; |
| 7845 | |
| 7846 | if (!_PyVerify_fd(fd)) { |
| 7847 | PyBuffer_Release(&pbuf); |
| 7848 | return posix_error(); |
| 7849 | } |
| 7850 | Py_BEGIN_ALLOW_THREADS |
| 7851 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7852 | Py_END_ALLOW_THREADS |
| 7853 | PyBuffer_Release(&pbuf); |
| 7854 | if (size < 0) |
| 7855 | return posix_error(); |
| 7856 | return PyLong_FromSsize_t(size); |
| 7857 | } |
| 7858 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7859 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7860 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7861 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7862 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 7863 | Create a FIFO (a POSIX named pipe).\n\ |
| 7864 | \n\ |
| 7865 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7866 | and path should be relative; path will then be relative to that directory.\n\ |
| 7867 | dir_fd may not be implemented on your platform.\n\ |
| 7868 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7869 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7870 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7871 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7872 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7873 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7874 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7875 | int dir_fd = DEFAULT_DIR_FD; |
| 7876 | int result; |
| 7877 | PyObject *return_value = NULL; |
| 7878 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 7879 | |
| 7880 | memset(&path, 0, sizeof(path)); |
| 7881 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 7882 | path_converter, &path, |
| 7883 | &mode, |
| 7884 | #ifdef HAVE_MKFIFOAT |
| 7885 | dir_fd_converter, &dir_fd |
| 7886 | #else |
| 7887 | dir_fd_unavailable, &dir_fd |
| 7888 | #endif |
| 7889 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7890 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7891 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7892 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7893 | #ifdef HAVE_MKFIFOAT |
| 7894 | if (dir_fd != DEFAULT_DIR_FD) |
| 7895 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 7896 | else |
| 7897 | #endif |
| 7898 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7899 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7900 | |
| 7901 | if (result < 0) { |
| 7902 | return_value = posix_error(); |
| 7903 | goto exit; |
| 7904 | } |
| 7905 | |
| 7906 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7907 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7908 | |
| 7909 | exit: |
| 7910 | path_cleanup(&path); |
| 7911 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7912 | } |
| 7913 | #endif |
| 7914 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7915 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7916 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7917 | "mknod(filename, mode=0o600, device=0, *, dir_fd=None)\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7918 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7919 | named filename. mode specifies both the permissions to use and the\n\ |
| 7920 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7921 | 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] | 7922 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7923 | os.makedev()), otherwise it is ignored.\n\ |
| 7924 | \n\ |
| 7925 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7926 | and path should be relative; path will then be relative to that directory.\n\ |
| 7927 | dir_fd may not be implemented on your platform.\n\ |
| 7928 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7929 | |
| 7930 | |
| 7931 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7932 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7933 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7934 | path_t path; |
| 7935 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7936 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7937 | int dir_fd = DEFAULT_DIR_FD; |
| 7938 | int result; |
| 7939 | PyObject *return_value = NULL; |
| 7940 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 7941 | |
| 7942 | memset(&path, 0, sizeof(path)); |
| 7943 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 7944 | path_converter, &path, |
| 7945 | &mode, &device, |
| 7946 | #ifdef HAVE_MKNODAT |
| 7947 | dir_fd_converter, &dir_fd |
| 7948 | #else |
| 7949 | dir_fd_unavailable, &dir_fd |
| 7950 | #endif |
| 7951 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7952 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7953 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7954 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7955 | #ifdef HAVE_MKNODAT |
| 7956 | if (dir_fd != DEFAULT_DIR_FD) |
| 7957 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 7958 | else |
| 7959 | #endif |
| 7960 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7961 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7962 | |
| 7963 | if (result < 0) { |
| 7964 | return_value = posix_error(); |
| 7965 | goto exit; |
| 7966 | } |
| 7967 | |
| 7968 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7969 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 7970 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7971 | exit: |
| 7972 | path_cleanup(&path); |
| 7973 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7974 | } |
| 7975 | #endif |
| 7976 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7977 | #ifdef HAVE_DEVICE_MACROS |
| 7978 | PyDoc_STRVAR(posix_major__doc__, |
| 7979 | "major(device) -> major number\n\ |
| 7980 | Extracts a device major number from a raw device number."); |
| 7981 | |
| 7982 | static PyObject * |
| 7983 | posix_major(PyObject *self, PyObject *args) |
| 7984 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7985 | int device; |
| 7986 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7987 | return NULL; |
| 7988 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7989 | } |
| 7990 | |
| 7991 | PyDoc_STRVAR(posix_minor__doc__, |
| 7992 | "minor(device) -> minor number\n\ |
| 7993 | Extracts a device minor number from a raw device number."); |
| 7994 | |
| 7995 | static PyObject * |
| 7996 | posix_minor(PyObject *self, PyObject *args) |
| 7997 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7998 | int device; |
| 7999 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 8000 | return NULL; |
| 8001 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8002 | } |
| 8003 | |
| 8004 | PyDoc_STRVAR(posix_makedev__doc__, |
| 8005 | "makedev(major, minor) -> device number\n\ |
| 8006 | Composes a raw device number from the major and minor device numbers."); |
| 8007 | |
| 8008 | static PyObject * |
| 8009 | posix_makedev(PyObject *self, PyObject *args) |
| 8010 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8011 | int major, minor; |
| 8012 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 8013 | return NULL; |
| 8014 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8015 | } |
| 8016 | #endif /* device macros */ |
| 8017 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8018 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8019 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8020 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8021 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8022 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8023 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8024 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8025 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8026 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8027 | int fd; |
| 8028 | off_t length; |
| 8029 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8030 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8031 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8032 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8034 | Py_BEGIN_ALLOW_THREADS |
| 8035 | res = ftruncate(fd, length); |
| 8036 | Py_END_ALLOW_THREADS |
| 8037 | if (res < 0) |
| 8038 | return posix_error(); |
| 8039 | Py_INCREF(Py_None); |
| 8040 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8041 | } |
| 8042 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8043 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8044 | #ifdef HAVE_TRUNCATE |
| 8045 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8046 | "truncate(path, length)\n\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8047 | Truncate the file given by path to length bytes.\n\ |
| 8048 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8049 | If this functionality is unavailable, using it raises an exception."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8050 | |
| 8051 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8052 | posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8053 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8054 | path_t path; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8055 | off_t length; |
| 8056 | int res; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8057 | PyObject *result = NULL; |
| 8058 | static char *keywords[] = {"path", "length", NULL}; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8059 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8060 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8061 | path.function_name = "truncate"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8062 | #ifdef HAVE_FTRUNCATE |
| 8063 | path.allow_fd = 1; |
| 8064 | #endif |
| 8065 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords, |
| 8066 | path_converter, &path, |
| 8067 | _parse_off_t, &length)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8068 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8069 | |
| 8070 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8071 | #ifdef HAVE_FTRUNCATE |
| 8072 | if (path.fd != -1) |
| 8073 | res = ftruncate(path.fd, length); |
| 8074 | else |
| 8075 | #endif |
| 8076 | res = truncate(path.narrow, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8077 | Py_END_ALLOW_THREADS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8078 | if (res < 0) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8079 | result = path_error(&path); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8080 | else { |
| 8081 | Py_INCREF(Py_None); |
| 8082 | result = Py_None; |
| 8083 | } |
| 8084 | path_cleanup(&path); |
| 8085 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8086 | } |
| 8087 | #endif |
| 8088 | |
| 8089 | #ifdef HAVE_POSIX_FALLOCATE |
| 8090 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8091 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8092 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8093 | starting from offset and continuing for len bytes."); |
| 8094 | |
| 8095 | static PyObject * |
| 8096 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8097 | { |
| 8098 | off_t len, offset; |
| 8099 | int res, fd; |
| 8100 | |
| 8101 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8102 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8103 | return NULL; |
| 8104 | |
| 8105 | Py_BEGIN_ALLOW_THREADS |
| 8106 | res = posix_fallocate(fd, offset, len); |
| 8107 | Py_END_ALLOW_THREADS |
| 8108 | if (res != 0) { |
| 8109 | errno = res; |
| 8110 | return posix_error(); |
| 8111 | } |
| 8112 | Py_RETURN_NONE; |
| 8113 | } |
| 8114 | #endif |
| 8115 | |
| 8116 | #ifdef HAVE_POSIX_FADVISE |
| 8117 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8118 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8119 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8120 | the kernel to make optimizations.\n\ |
| 8121 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8122 | offset and continuing for len bytes.\n\ |
| 8123 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8124 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8125 | POSIX_FADV_DONTNEED."); |
| 8126 | |
| 8127 | static PyObject * |
| 8128 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8129 | { |
| 8130 | off_t len, offset; |
| 8131 | int res, fd, advice; |
| 8132 | |
| 8133 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8134 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8135 | return NULL; |
| 8136 | |
| 8137 | Py_BEGIN_ALLOW_THREADS |
| 8138 | res = posix_fadvise(fd, offset, len, advice); |
| 8139 | Py_END_ALLOW_THREADS |
| 8140 | if (res != 0) { |
| 8141 | errno = res; |
| 8142 | return posix_error(); |
| 8143 | } |
| 8144 | Py_RETURN_NONE; |
| 8145 | } |
| 8146 | #endif |
| 8147 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8148 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8149 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8150 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8151 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8152 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8153 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8154 | * get re-set with another call for the same key. */ |
| 8155 | static PyObject *posix_putenv_garbage; |
| 8156 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8157 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8158 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8159 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8160 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8161 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8162 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8163 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8164 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8165 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8166 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8167 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8168 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8169 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8170 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8171 | if (newstr == NULL) { |
| 8172 | PyErr_NoMemory(); |
| 8173 | goto error; |
| 8174 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8175 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8176 | PyErr_Format(PyExc_ValueError, |
| 8177 | "the environment variable is longer than %u characters", |
| 8178 | _MAX_ENV); |
| 8179 | goto error; |
| 8180 | } |
| 8181 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8182 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8183 | if (newenv == NULL) |
| 8184 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8185 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8186 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8187 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8188 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8189 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8190 | PyObject *os1, *os2; |
| 8191 | char *s1, *s2; |
| 8192 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8193 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8194 | if (!PyArg_ParseTuple(args, |
| 8195 | "O&O&:putenv", |
| 8196 | PyUnicode_FSConverter, &os1, |
| 8197 | PyUnicode_FSConverter, &os2)) |
| 8198 | return NULL; |
| 8199 | s1 = PyBytes_AsString(os1); |
| 8200 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8201 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8202 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8203 | if (newstr == NULL) { |
| 8204 | PyErr_NoMemory(); |
| 8205 | goto error; |
| 8206 | } |
| 8207 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8208 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8209 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8210 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8211 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8212 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8213 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8214 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8215 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8216 | * this will cause previous value to be collected. This has to |
| 8217 | * happen after the real putenv() call because the old value |
| 8218 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8219 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8220 | /* really not much we can do; just leak */ |
| 8221 | PyErr_Clear(); |
| 8222 | } |
| 8223 | else { |
| 8224 | Py_DECREF(newstr); |
| 8225 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8226 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8227 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8228 | Py_DECREF(os1); |
| 8229 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8230 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8231 | Py_RETURN_NONE; |
| 8232 | |
| 8233 | error: |
| 8234 | #ifndef MS_WINDOWS |
| 8235 | Py_DECREF(os1); |
| 8236 | Py_DECREF(os2); |
| 8237 | #endif |
| 8238 | Py_XDECREF(newstr); |
| 8239 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8240 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8241 | #endif /* putenv */ |
| 8242 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8243 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8244 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8245 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8246 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8247 | |
| 8248 | static PyObject * |
| 8249 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8250 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8251 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8252 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8253 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8254 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8255 | |
| 8256 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8257 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8258 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8259 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8260 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8261 | #ifdef HAVE_BROKEN_UNSETENV |
| 8262 | unsetenv(PyBytes_AS_STRING(name)); |
| 8263 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8264 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8265 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8266 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8267 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8268 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8269 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8270 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8271 | /* Remove the key from posix_putenv_garbage; |
| 8272 | * this will cause it to be collected. This has to |
| 8273 | * happen after the real unsetenv() call because the |
| 8274 | * old value was still accessible until then. |
| 8275 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8276 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8277 | /* really not much we can do; just leak */ |
| 8278 | PyErr_Clear(); |
| 8279 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8280 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8281 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8282 | } |
| 8283 | #endif /* unsetenv */ |
| 8284 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8285 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8286 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8287 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8288 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8289 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8290 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8291 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8292 | int code; |
| 8293 | char *message; |
| 8294 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8295 | return NULL; |
| 8296 | message = strerror(code); |
| 8297 | if (message == NULL) { |
| 8298 | PyErr_SetString(PyExc_ValueError, |
| 8299 | "strerror() argument out of range"); |
| 8300 | return NULL; |
| 8301 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8302 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8303 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8304 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8305 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8306 | #ifdef HAVE_SYS_WAIT_H |
| 8307 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8308 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8309 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8310 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8311 | 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] | 8312 | |
| 8313 | static PyObject * |
| 8314 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 8315 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8316 | WAIT_TYPE status; |
| 8317 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8318 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8319 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 8320 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8321 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8322 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8323 | } |
| 8324 | #endif /* WCOREDUMP */ |
| 8325 | |
| 8326 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8327 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8328 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8329 | 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] | 8330 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8331 | |
| 8332 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8333 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8334 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8335 | WAIT_TYPE status; |
| 8336 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8337 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8338 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 8339 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8340 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8341 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8342 | } |
| 8343 | #endif /* WIFCONTINUED */ |
| 8344 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8345 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8346 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8347 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8348 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8349 | |
| 8350 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8351 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8352 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8353 | WAIT_TYPE status; |
| 8354 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8355 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8356 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 8357 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8358 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8359 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8360 | } |
| 8361 | #endif /* WIFSTOPPED */ |
| 8362 | |
| 8363 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8364 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8365 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8366 | 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] | 8367 | |
| 8368 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8369 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8370 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8371 | WAIT_TYPE status; |
| 8372 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8373 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8374 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 8375 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8376 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8377 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8378 | } |
| 8379 | #endif /* WIFSIGNALED */ |
| 8380 | |
| 8381 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8382 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8383 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8384 | 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] | 8385 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8386 | |
| 8387 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8388 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8389 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8390 | WAIT_TYPE status; |
| 8391 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8393 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 8394 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8395 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8396 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8397 | } |
| 8398 | #endif /* WIFEXITED */ |
| 8399 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8400 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8401 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8402 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8403 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8404 | |
| 8405 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8406 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8407 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8408 | WAIT_TYPE status; |
| 8409 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8410 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8411 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8412 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8413 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8414 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8415 | } |
| 8416 | #endif /* WEXITSTATUS */ |
| 8417 | |
| 8418 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8419 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8420 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8421 | 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] | 8422 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8423 | |
| 8424 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8425 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8426 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8427 | WAIT_TYPE status; |
| 8428 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8429 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8430 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8431 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8432 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8433 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8434 | } |
| 8435 | #endif /* WTERMSIG */ |
| 8436 | |
| 8437 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8438 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8439 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8440 | Return the signal that stopped the process that provided\n\ |
| 8441 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8442 | |
| 8443 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8444 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8445 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8446 | WAIT_TYPE status; |
| 8447 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8448 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8449 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8450 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8451 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8452 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8453 | } |
| 8454 | #endif /* WSTOPSIG */ |
| 8455 | |
| 8456 | #endif /* HAVE_SYS_WAIT_H */ |
| 8457 | |
| 8458 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8459 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8460 | #ifdef _SCO_DS |
| 8461 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8462 | needed definitions in sys/statvfs.h */ |
| 8463 | #define _SVID3 |
| 8464 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8465 | #include <sys/statvfs.h> |
| 8466 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8467 | static PyObject* |
| 8468 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8469 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8470 | if (v == NULL) |
| 8471 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8472 | |
| 8473 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8474 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8475 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8476 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8477 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8478 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8479 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8480 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8481 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8482 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8483 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8484 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8485 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8486 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8487 | PyStructSequence_SET_ITEM(v, 2, |
| 8488 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8489 | PyStructSequence_SET_ITEM(v, 3, |
| 8490 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8491 | PyStructSequence_SET_ITEM(v, 4, |
| 8492 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8493 | PyStructSequence_SET_ITEM(v, 5, |
| 8494 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8495 | PyStructSequence_SET_ITEM(v, 6, |
| 8496 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8497 | PyStructSequence_SET_ITEM(v, 7, |
| 8498 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8499 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8500 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8501 | #endif |
| 8502 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8503 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8504 | } |
| 8505 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8506 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8507 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8508 | Perform an fstatvfs system call on the given fd.\n\ |
| 8509 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8510 | |
| 8511 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8512 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8513 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8514 | int fd, res; |
| 8515 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8516 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8517 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8518 | return NULL; |
| 8519 | Py_BEGIN_ALLOW_THREADS |
| 8520 | res = fstatvfs(fd, &st); |
| 8521 | Py_END_ALLOW_THREADS |
| 8522 | if (res != 0) |
| 8523 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8524 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8525 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8526 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8527 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8528 | |
| 8529 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8530 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8531 | #include <sys/statvfs.h> |
| 8532 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8533 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8534 | "statvfs(path)\n\n\ |
| 8535 | Perform a statvfs system call on the given path.\n\ |
| 8536 | \n\ |
| 8537 | path may always be specified as a string.\n\ |
| 8538 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8539 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8540 | |
| 8541 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8542 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8543 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8544 | static char *keywords[] = {"path", NULL}; |
| 8545 | path_t path; |
| 8546 | int result; |
| 8547 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8548 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8549 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8550 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8551 | path.function_name = "statvfs"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8552 | #ifdef HAVE_FSTATVFS |
| 8553 | path.allow_fd = 1; |
| 8554 | #endif |
| 8555 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 8556 | path_converter, &path |
| 8557 | )) |
| 8558 | return NULL; |
| 8559 | |
| 8560 | Py_BEGIN_ALLOW_THREADS |
| 8561 | #ifdef HAVE_FSTATVFS |
| 8562 | if (path.fd != -1) { |
| 8563 | #ifdef __APPLE__ |
| 8564 | /* handle weak-linking on Mac OS X 10.3 */ |
| 8565 | if (fstatvfs == NULL) { |
| 8566 | fd_specified("statvfs", path.fd); |
| 8567 | goto exit; |
| 8568 | } |
| 8569 | #endif |
| 8570 | result = fstatvfs(path.fd, &st); |
| 8571 | } |
| 8572 | else |
| 8573 | #endif |
| 8574 | result = statvfs(path.narrow, &st); |
| 8575 | Py_END_ALLOW_THREADS |
| 8576 | |
| 8577 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8578 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8579 | goto exit; |
| 8580 | } |
| 8581 | |
| 8582 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 8583 | |
| 8584 | exit: |
| 8585 | path_cleanup(&path); |
| 8586 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8587 | } |
| 8588 | #endif /* HAVE_STATVFS */ |
| 8589 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8590 | #ifdef MS_WINDOWS |
| 8591 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8592 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8593 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8594 | |
| 8595 | static PyObject * |
| 8596 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8597 | { |
| 8598 | BOOL retval; |
| 8599 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8600 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8601 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8602 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8603 | return NULL; |
| 8604 | |
| 8605 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8606 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8607 | Py_END_ALLOW_THREADS |
| 8608 | if (retval == 0) |
| 8609 | return PyErr_SetFromWindowsErr(0); |
| 8610 | |
| 8611 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8612 | } |
| 8613 | #endif |
| 8614 | |
| 8615 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8616 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8617 | * It maps strings representing configuration variable names to |
| 8618 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8619 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8620 | * rarely-used constants. There are three separate tables that use |
| 8621 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8622 | * |
| 8623 | * This code is always included, even if none of the interfaces that |
| 8624 | * need it are included. The #if hackery needed to avoid it would be |
| 8625 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8626 | */ |
| 8627 | struct constdef { |
| 8628 | char *name; |
| 8629 | long value; |
| 8630 | }; |
| 8631 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8632 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8633 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8634 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8635 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8636 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8637 | *valuep = PyLong_AS_LONG(arg); |
| 8638 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8639 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8640 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8641 | /* look up the value in the table using a binary search */ |
| 8642 | size_t lo = 0; |
| 8643 | size_t mid; |
| 8644 | size_t hi = tablesize; |
| 8645 | int cmp; |
| 8646 | const char *confname; |
| 8647 | if (!PyUnicode_Check(arg)) { |
| 8648 | PyErr_SetString(PyExc_TypeError, |
| 8649 | "configuration names must be strings or integers"); |
| 8650 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8651 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8652 | confname = _PyUnicode_AsString(arg); |
| 8653 | if (confname == NULL) |
| 8654 | return 0; |
| 8655 | while (lo < hi) { |
| 8656 | mid = (lo + hi) / 2; |
| 8657 | cmp = strcmp(confname, table[mid].name); |
| 8658 | if (cmp < 0) |
| 8659 | hi = mid; |
| 8660 | else if (cmp > 0) |
| 8661 | lo = mid + 1; |
| 8662 | else { |
| 8663 | *valuep = table[mid].value; |
| 8664 | return 1; |
| 8665 | } |
| 8666 | } |
| 8667 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8668 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8669 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8670 | } |
| 8671 | |
| 8672 | |
| 8673 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8674 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8675 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8676 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8677 | #endif |
| 8678 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8679 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8680 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8681 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8682 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8683 | #endif |
| 8684 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8685 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8686 | #endif |
| 8687 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8688 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8689 | #endif |
| 8690 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8691 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8692 | #endif |
| 8693 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8694 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8695 | #endif |
| 8696 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8697 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8698 | #endif |
| 8699 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8700 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8701 | #endif |
| 8702 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8703 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8704 | #endif |
| 8705 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8706 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8707 | #endif |
| 8708 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8709 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8710 | #endif |
| 8711 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8713 | #endif |
| 8714 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8716 | #endif |
| 8717 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8718 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8719 | #endif |
| 8720 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8721 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8722 | #endif |
| 8723 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8724 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8725 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8726 | #ifdef _PC_ACL_ENABLED |
| 8727 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8728 | #endif |
| 8729 | #ifdef _PC_MIN_HOLE_SIZE |
| 8730 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8731 | #endif |
| 8732 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8733 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8734 | #endif |
| 8735 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8736 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8737 | #endif |
| 8738 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8739 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8740 | #endif |
| 8741 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8742 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8743 | #endif |
| 8744 | #ifdef _PC_REC_XFER_ALIGN |
| 8745 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8746 | #endif |
| 8747 | #ifdef _PC_SYMLINK_MAX |
| 8748 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8749 | #endif |
| 8750 | #ifdef _PC_XATTR_ENABLED |
| 8751 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8752 | #endif |
| 8753 | #ifdef _PC_XATTR_EXISTS |
| 8754 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8755 | #endif |
| 8756 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8757 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8758 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8759 | }; |
| 8760 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8761 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8762 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8763 | { |
| 8764 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8765 | sizeof(posix_constants_pathconf) |
| 8766 | / sizeof(struct constdef)); |
| 8767 | } |
| 8768 | #endif |
| 8769 | |
| 8770 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8771 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8772 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8773 | 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] | 8774 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8775 | |
| 8776 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8777 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8778 | { |
| 8779 | PyObject *result = NULL; |
| 8780 | int name, fd; |
| 8781 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8782 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8783 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8784 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8785 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8786 | errno = 0; |
| 8787 | limit = fpathconf(fd, name); |
| 8788 | if (limit == -1 && errno != 0) |
| 8789 | posix_error(); |
| 8790 | else |
| 8791 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8792 | } |
| 8793 | return result; |
| 8794 | } |
| 8795 | #endif |
| 8796 | |
| 8797 | |
| 8798 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8799 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8800 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8801 | Return the configuration limit name for the file or directory path.\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8802 | If there is no limit, return -1.\n\ |
| 8803 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8804 | If this functionality is unavailable, using it raises an exception."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8805 | |
| 8806 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8807 | posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8808 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8809 | path_t path; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8810 | PyObject *result = NULL; |
| 8811 | int name; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8812 | static char *keywords[] = {"path", "name", NULL}; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8813 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8814 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8815 | path.function_name = "pathconf"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8816 | #ifdef HAVE_FPATHCONF |
| 8817 | path.allow_fd = 1; |
| 8818 | #endif |
| 8819 | if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords, |
| 8820 | path_converter, &path, |
| 8821 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8822 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8823 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8824 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8825 | #ifdef HAVE_FPATHCONF |
| 8826 | if (path.fd != -1) |
| 8827 | limit = fpathconf(path.fd, name); |
| 8828 | else |
| 8829 | #endif |
| 8830 | limit = pathconf(path.narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8831 | if (limit == -1 && errno != 0) { |
| 8832 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8833 | /* could be a path or name problem */ |
| 8834 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8835 | else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8836 | result = path_error(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8837 | } |
| 8838 | else |
| 8839 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8840 | } |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8841 | path_cleanup(&path); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8842 | return result; |
| 8843 | } |
| 8844 | #endif |
| 8845 | |
| 8846 | #ifdef HAVE_CONFSTR |
| 8847 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8848 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8849 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8850 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8851 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8852 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8853 | #endif |
| 8854 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8855 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8856 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8857 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8858 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8859 | #endif |
| 8860 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8861 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8862 | #endif |
| 8863 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8864 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8865 | #endif |
| 8866 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8867 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8868 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8869 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8870 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8871 | #endif |
| 8872 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8873 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8874 | #endif |
| 8875 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8876 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8877 | #endif |
| 8878 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8879 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8880 | #endif |
| 8881 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8882 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8883 | #endif |
| 8884 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8885 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8886 | #endif |
| 8887 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8888 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8889 | #endif |
| 8890 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8891 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8892 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8893 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8894 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8895 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8896 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8897 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8898 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8899 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8900 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8901 | #endif |
| 8902 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8903 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8904 | #endif |
| 8905 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8906 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8907 | #endif |
| 8908 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8909 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8910 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8911 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8912 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8913 | #endif |
| 8914 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8915 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8916 | #endif |
| 8917 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8918 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8919 | #endif |
| 8920 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8921 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8922 | #endif |
| 8923 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8924 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8925 | #endif |
| 8926 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8927 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8928 | #endif |
| 8929 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8930 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8931 | #endif |
| 8932 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8933 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8934 | #endif |
| 8935 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8936 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8937 | #endif |
| 8938 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8939 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8940 | #endif |
| 8941 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8942 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8943 | #endif |
| 8944 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8945 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8946 | #endif |
| 8947 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8948 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8949 | #endif |
| 8950 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8951 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8952 | #endif |
| 8953 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8954 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8955 | #endif |
| 8956 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8957 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8958 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8959 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8960 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8961 | #endif |
| 8962 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8963 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8964 | #endif |
| 8965 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8966 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8967 | #endif |
| 8968 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8969 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8970 | #endif |
| 8971 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8972 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8973 | #endif |
| 8974 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8975 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8976 | #endif |
| 8977 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8978 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8979 | #endif |
| 8980 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8981 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8982 | #endif |
| 8983 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8984 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8985 | #endif |
| 8986 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8987 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8988 | #endif |
| 8989 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8990 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8991 | #endif |
| 8992 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8993 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8994 | #endif |
| 8995 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8996 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8997 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8998 | }; |
| 8999 | |
| 9000 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9001 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9002 | { |
| 9003 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9004 | sizeof(posix_constants_confstr) |
| 9005 | / sizeof(struct constdef)); |
| 9006 | } |
| 9007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9008 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9009 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9010 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9011 | |
| 9012 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9013 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9014 | { |
| 9015 | PyObject *result = NULL; |
| 9016 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9017 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9018 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9019 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9020 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 9021 | return NULL; |
| 9022 | |
| 9023 | errno = 0; |
| 9024 | len = confstr(name, buffer, sizeof(buffer)); |
| 9025 | if (len == 0) { |
| 9026 | if (errno) { |
| 9027 | posix_error(); |
| 9028 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9029 | } |
| 9030 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9031 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9032 | } |
| 9033 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9034 | |
| 9035 | if ((unsigned int)len >= sizeof(buffer)) { |
| 9036 | char *buf = PyMem_Malloc(len); |
| 9037 | if (buf == NULL) |
| 9038 | return PyErr_NoMemory(); |
| 9039 | confstr(name, buf, len); |
| 9040 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9041 | PyMem_Free(buf); |
| 9042 | } |
| 9043 | else |
| 9044 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9045 | return result; |
| 9046 | } |
| 9047 | #endif |
| 9048 | |
| 9049 | |
| 9050 | #ifdef HAVE_SYSCONF |
| 9051 | static struct constdef posix_constants_sysconf[] = { |
| 9052 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9053 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9054 | #endif |
| 9055 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9056 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9057 | #endif |
| 9058 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9059 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9060 | #endif |
| 9061 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9062 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9063 | #endif |
| 9064 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9065 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9066 | #endif |
| 9067 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9068 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9069 | #endif |
| 9070 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9071 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9072 | #endif |
| 9073 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9074 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9075 | #endif |
| 9076 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9078 | #endif |
| 9079 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9080 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9081 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9082 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9084 | #endif |
| 9085 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9086 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9087 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9088 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9089 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9090 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9091 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9092 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9093 | #endif |
| 9094 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9095 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9096 | #endif |
| 9097 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9098 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9099 | #endif |
| 9100 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9101 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9102 | #endif |
| 9103 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9104 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9105 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9106 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9107 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9108 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9109 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9110 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9111 | #endif |
| 9112 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9113 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9114 | #endif |
| 9115 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9116 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9117 | #endif |
| 9118 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9119 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9120 | #endif |
| 9121 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9123 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9124 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9125 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9126 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9127 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9128 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9129 | #endif |
| 9130 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9131 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9132 | #endif |
| 9133 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9134 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9135 | #endif |
| 9136 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9137 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9138 | #endif |
| 9139 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9140 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9141 | #endif |
| 9142 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9143 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9144 | #endif |
| 9145 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9146 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9147 | #endif |
| 9148 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9149 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9150 | #endif |
| 9151 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9152 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9153 | #endif |
| 9154 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9155 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9156 | #endif |
| 9157 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9158 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9159 | #endif |
| 9160 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9161 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9162 | #endif |
| 9163 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9164 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9165 | #endif |
| 9166 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9167 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9168 | #endif |
| 9169 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9170 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9171 | #endif |
| 9172 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9173 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9174 | #endif |
| 9175 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9176 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9177 | #endif |
| 9178 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9179 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9180 | #endif |
| 9181 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9182 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9183 | #endif |
| 9184 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9185 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9186 | #endif |
| 9187 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9188 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9189 | #endif |
| 9190 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9191 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9192 | #endif |
| 9193 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9194 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9195 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9196 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9197 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9198 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9199 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9200 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9201 | #endif |
| 9202 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9203 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9204 | #endif |
| 9205 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9206 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9207 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9208 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9209 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9210 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9211 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9212 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9213 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9214 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9215 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9216 | #endif |
| 9217 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9218 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9219 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9220 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9221 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9222 | #endif |
| 9223 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9224 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9225 | #endif |
| 9226 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9227 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9228 | #endif |
| 9229 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9230 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9231 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9232 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9233 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9234 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9235 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9236 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9237 | #endif |
| 9238 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9239 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9240 | #endif |
| 9241 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9242 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9243 | #endif |
| 9244 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9245 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9246 | #endif |
| 9247 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9248 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9249 | #endif |
| 9250 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9251 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9252 | #endif |
| 9253 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9254 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9255 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9256 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9258 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9259 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9260 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9261 | #endif |
| 9262 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9263 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9264 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9265 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9266 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9267 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9268 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9269 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9270 | #endif |
| 9271 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9272 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9273 | #endif |
| 9274 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9275 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9276 | #endif |
| 9277 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9279 | #endif |
| 9280 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9281 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9282 | #endif |
| 9283 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9284 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9285 | #endif |
| 9286 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9287 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9288 | #endif |
| 9289 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9290 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9291 | #endif |
| 9292 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9293 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9294 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9295 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9296 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9297 | #endif |
| 9298 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9299 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9300 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9301 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9302 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9303 | #endif |
| 9304 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9305 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9306 | #endif |
| 9307 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9308 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9309 | #endif |
| 9310 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9311 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9312 | #endif |
| 9313 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9314 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9315 | #endif |
| 9316 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9317 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9318 | #endif |
| 9319 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9320 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9321 | #endif |
| 9322 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9323 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9324 | #endif |
| 9325 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9326 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9327 | #endif |
| 9328 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9329 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9330 | #endif |
| 9331 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9332 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9333 | #endif |
| 9334 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9335 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9336 | #endif |
| 9337 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9338 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9339 | #endif |
| 9340 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9341 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9342 | #endif |
| 9343 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9344 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9345 | #endif |
| 9346 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9347 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9348 | #endif |
| 9349 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9350 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9351 | #endif |
| 9352 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9353 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9354 | #endif |
| 9355 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9356 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9357 | #endif |
| 9358 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9359 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9360 | #endif |
| 9361 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9362 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9363 | #endif |
| 9364 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9365 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9366 | #endif |
| 9367 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9368 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9369 | #endif |
| 9370 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9371 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9372 | #endif |
| 9373 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9375 | #endif |
| 9376 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9377 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9378 | #endif |
| 9379 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9380 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9381 | #endif |
| 9382 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9383 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9384 | #endif |
| 9385 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9386 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9387 | #endif |
| 9388 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9389 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9390 | #endif |
| 9391 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9392 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9393 | #endif |
| 9394 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9395 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9396 | #endif |
| 9397 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9398 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9399 | #endif |
| 9400 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9401 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9402 | #endif |
| 9403 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9404 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9405 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9406 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9407 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9408 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9409 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9410 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9411 | #endif |
| 9412 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9413 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9414 | #endif |
| 9415 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9416 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9417 | #endif |
| 9418 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9419 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9420 | #endif |
| 9421 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9422 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9423 | #endif |
| 9424 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9425 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9426 | #endif |
| 9427 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9428 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9429 | #endif |
| 9430 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9431 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9432 | #endif |
| 9433 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9434 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9435 | #endif |
| 9436 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9437 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9438 | #endif |
| 9439 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9440 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9441 | #endif |
| 9442 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9443 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9444 | #endif |
| 9445 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9446 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9447 | #endif |
| 9448 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9449 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9450 | #endif |
| 9451 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9452 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9453 | #endif |
| 9454 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9455 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9456 | #endif |
| 9457 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9458 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9459 | #endif |
| 9460 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9461 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9462 | #endif |
| 9463 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9464 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9465 | #endif |
| 9466 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9467 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9468 | #endif |
| 9469 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9470 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9471 | #endif |
| 9472 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9473 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9474 | #endif |
| 9475 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9476 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9477 | #endif |
| 9478 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9479 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9480 | #endif |
| 9481 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9482 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9483 | #endif |
| 9484 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9485 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9486 | #endif |
| 9487 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9488 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9489 | #endif |
| 9490 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9491 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9492 | #endif |
| 9493 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9494 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9495 | #endif |
| 9496 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9497 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9498 | #endif |
| 9499 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9500 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9501 | #endif |
| 9502 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9503 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9504 | #endif |
| 9505 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9506 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9507 | #endif |
| 9508 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9509 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9510 | #endif |
| 9511 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9512 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9513 | #endif |
| 9514 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9515 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9516 | #endif |
| 9517 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9518 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9519 | #endif |
| 9520 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9521 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9522 | #endif |
| 9523 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9524 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9525 | #endif |
| 9526 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9527 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9528 | #endif |
| 9529 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9530 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9531 | #endif |
| 9532 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9533 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9534 | #endif |
| 9535 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9536 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9537 | #endif |
| 9538 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9539 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9540 | #endif |
| 9541 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9542 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9543 | #endif |
| 9544 | }; |
| 9545 | |
| 9546 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9547 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9548 | { |
| 9549 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9550 | sizeof(posix_constants_sysconf) |
| 9551 | / sizeof(struct constdef)); |
| 9552 | } |
| 9553 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9554 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9555 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9556 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9557 | |
| 9558 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9559 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9560 | { |
| 9561 | PyObject *result = NULL; |
| 9562 | int name; |
| 9563 | |
| 9564 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
Victor Stinner | 6fdd7b8 | 2013-05-16 22:26:29 +0200 | [diff] [blame] | 9565 | long value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9566 | |
| 9567 | errno = 0; |
| 9568 | value = sysconf(name); |
| 9569 | if (value == -1 && errno != 0) |
| 9570 | posix_error(); |
| 9571 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9572 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9573 | } |
| 9574 | return result; |
| 9575 | } |
| 9576 | #endif |
| 9577 | |
| 9578 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9579 | /* This code is used to ensure that the tables of configuration value names |
| 9580 | * are in sorted order as required by conv_confname(), and also to build the |
| 9581 | * the exported dictionaries that are used to publish information about the |
| 9582 | * names available on the host platform. |
| 9583 | * |
| 9584 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9585 | * when used, even for platforms we're not able to test on. It also makes |
| 9586 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9587 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9588 | |
| 9589 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9590 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9591 | { |
| 9592 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9593 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9594 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9595 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9596 | |
| 9597 | return strcmp(c1->name, c2->name); |
| 9598 | } |
| 9599 | |
| 9600 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9601 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9602 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9603 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9604 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9605 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9606 | |
| 9607 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9608 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9609 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9610 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9611 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9612 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9613 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9614 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9615 | Py_XDECREF(o); |
| 9616 | Py_DECREF(d); |
| 9617 | return -1; |
| 9618 | } |
| 9619 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9620 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9621 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9622 | } |
| 9623 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9624 | /* Return -1 on failure, 0 on success. */ |
| 9625 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9626 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9627 | { |
| 9628 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9629 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9630 | sizeof(posix_constants_pathconf) |
| 9631 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9632 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9633 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9634 | #endif |
| 9635 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9636 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9637 | sizeof(posix_constants_confstr) |
| 9638 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9639 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9640 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9641 | #endif |
| 9642 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9643 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9644 | sizeof(posix_constants_sysconf) |
| 9645 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9646 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9647 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9648 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9649 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9650 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9651 | |
| 9652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9653 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9654 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9655 | 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] | 9656 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9657 | |
| 9658 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9659 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9660 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9661 | abort(); |
| 9662 | /*NOTREACHED*/ |
| 9663 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9664 | return NULL; |
| 9665 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9666 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9667 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9668 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9669 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9670 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9671 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9672 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9673 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9674 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9675 | application (if any) its extension is associated.\n\ |
| 9676 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9677 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9678 | \n\ |
| 9679 | startfile returns as soon as the associated application is launched.\n\ |
| 9680 | There is no option to wait for the application to close, and no way\n\ |
| 9681 | to retrieve the application's exit status.\n\ |
| 9682 | \n\ |
| 9683 | The filepath is relative to the current directory. If you want to use\n\ |
| 9684 | 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] | 9685 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9686 | |
| 9687 | static PyObject * |
| 9688 | win32_startfile(PyObject *self, PyObject *args) |
| 9689 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9690 | PyObject *ofilepath; |
| 9691 | char *filepath; |
| 9692 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9693 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9694 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9695 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9696 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9697 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9698 | &unipath, &operation)) { |
| 9699 | PyErr_Clear(); |
| 9700 | goto normal; |
| 9701 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9702 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9703 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9704 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9705 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9706 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9707 | PyErr_Clear(); |
| 9708 | operation = NULL; |
| 9709 | goto normal; |
| 9710 | } |
| 9711 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9712 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9713 | wpath = PyUnicode_AsUnicode(unipath); |
| 9714 | if (wpath == NULL) |
| 9715 | goto normal; |
| 9716 | if (uoperation) { |
| 9717 | woperation = PyUnicode_AsUnicode(uoperation); |
| 9718 | if (woperation == NULL) |
| 9719 | goto normal; |
| 9720 | } |
| 9721 | else |
| 9722 | woperation = NULL; |
| 9723 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9724 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9725 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 9726 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9727 | Py_END_ALLOW_THREADS |
| 9728 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9729 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9730 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9731 | win32_error_object("startfile", unipath); |
| 9732 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9733 | } |
| 9734 | Py_INCREF(Py_None); |
| 9735 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9736 | |
| 9737 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9738 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9739 | PyUnicode_FSConverter, &ofilepath, |
| 9740 | &operation)) |
| 9741 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 9742 | if (win32_warn_bytes_api()) { |
| 9743 | Py_DECREF(ofilepath); |
| 9744 | return NULL; |
| 9745 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9746 | filepath = PyBytes_AsString(ofilepath); |
| 9747 | Py_BEGIN_ALLOW_THREADS |
| 9748 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9749 | NULL, NULL, SW_SHOWNORMAL); |
| 9750 | Py_END_ALLOW_THREADS |
| 9751 | if (rc <= (HINSTANCE)32) { |
| 9752 | PyObject *errval = win32_error("startfile", filepath); |
| 9753 | Py_DECREF(ofilepath); |
| 9754 | return errval; |
| 9755 | } |
| 9756 | Py_DECREF(ofilepath); |
| 9757 | Py_INCREF(Py_None); |
| 9758 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9759 | } |
| 9760 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9761 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9762 | #ifdef HAVE_GETLOADAVG |
| 9763 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9764 | "getloadavg() -> (float, float, float)\n\n\ |
| 9765 | Return the number of processes in the system run queue averaged over\n\ |
| 9766 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9767 | was unobtainable"); |
| 9768 | |
| 9769 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9770 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9771 | { |
| 9772 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9773 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9774 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9775 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9776 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9777 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9778 | } |
| 9779 | #endif |
| 9780 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9781 | PyDoc_STRVAR(device_encoding__doc__, |
| 9782 | "device_encoding(fd) -> str\n\n\ |
| 9783 | Return a string describing the encoding of the device\n\ |
| 9784 | if the output is a terminal; else return None."); |
| 9785 | |
| 9786 | static PyObject * |
| 9787 | device_encoding(PyObject *self, PyObject *args) |
| 9788 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9789 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9790 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9791 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9792 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9793 | |
| 9794 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9795 | } |
| 9796 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9797 | #ifdef HAVE_SETRESUID |
| 9798 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9799 | "setresuid(ruid, euid, suid)\n\n\ |
| 9800 | Set the current process's real, effective, and saved user ids."); |
| 9801 | |
| 9802 | static PyObject* |
| 9803 | posix_setresuid (PyObject *self, PyObject *args) |
| 9804 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9805 | /* We assume uid_t is no larger than a long. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9806 | uid_t ruid, euid, suid; |
| 9807 | if (!PyArg_ParseTuple(args, "O&O&O&:setresuid", |
| 9808 | _Py_Uid_Converter, &ruid, |
| 9809 | _Py_Uid_Converter, &euid, |
| 9810 | _Py_Uid_Converter, &suid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9811 | return NULL; |
| 9812 | if (setresuid(ruid, euid, suid) < 0) |
| 9813 | return posix_error(); |
| 9814 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9815 | } |
| 9816 | #endif |
| 9817 | |
| 9818 | #ifdef HAVE_SETRESGID |
| 9819 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9820 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9821 | Set the current process's real, effective, and saved group ids."); |
| 9822 | |
| 9823 | static PyObject* |
| 9824 | posix_setresgid (PyObject *self, PyObject *args) |
| 9825 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9826 | gid_t rgid, egid, sgid; |
| 9827 | if (!PyArg_ParseTuple(args, "O&O&O&:setresgid", |
| 9828 | _Py_Gid_Converter, &rgid, |
| 9829 | _Py_Gid_Converter, &egid, |
| 9830 | _Py_Gid_Converter, &sgid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9831 | return NULL; |
| 9832 | if (setresgid(rgid, egid, sgid) < 0) |
| 9833 | return posix_error(); |
| 9834 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9835 | } |
| 9836 | #endif |
| 9837 | |
| 9838 | #ifdef HAVE_GETRESUID |
| 9839 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9840 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9841 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9842 | |
| 9843 | static PyObject* |
| 9844 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9845 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9846 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9847 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9848 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9849 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 9850 | _PyLong_FromUid(euid), |
| 9851 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9852 | } |
| 9853 | #endif |
| 9854 | |
| 9855 | #ifdef HAVE_GETRESGID |
| 9856 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9857 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9858 | 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] | 9859 | |
| 9860 | static PyObject* |
| 9861 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9862 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9863 | uid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9864 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9865 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9866 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 9867 | _PyLong_FromGid(egid), |
| 9868 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9869 | } |
| 9870 | #endif |
| 9871 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 9872 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9873 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9874 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9875 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 9876 | Return the value of extended attribute attribute on path.\n\ |
| 9877 | \n\ |
| 9878 | path may be either a string or an open file descriptor.\n\ |
| 9879 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 9880 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 9881 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9882 | |
| 9883 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9884 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9885 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9886 | path_t path; |
| 9887 | path_t attribute; |
| 9888 | int follow_symlinks = 1; |
| 9889 | PyObject *buffer = NULL; |
| 9890 | int i; |
| 9891 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9892 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9893 | memset(&path, 0, sizeof(path)); |
| 9894 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9895 | path.function_name = "getxattr"; |
| 9896 | attribute.function_name = "getxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9897 | path.allow_fd = 1; |
| 9898 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 9899 | path_converter, &path, |
| 9900 | path_converter, &attribute, |
| 9901 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9902 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9903 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9904 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 9905 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9906 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9907 | for (i = 0; ; i++) { |
| 9908 | void *ptr; |
| 9909 | ssize_t result; |
| 9910 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 9911 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 9912 | if (!buffer_size) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9913 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9914 | goto exit; |
| 9915 | } |
| 9916 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 9917 | if (!buffer) |
| 9918 | goto exit; |
| 9919 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9920 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9921 | Py_BEGIN_ALLOW_THREADS; |
| 9922 | if (path.fd >= 0) |
| 9923 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 9924 | else if (follow_symlinks) |
| 9925 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 9926 | else |
| 9927 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 9928 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9929 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9930 | if (result < 0) { |
| 9931 | Py_DECREF(buffer); |
| 9932 | buffer = NULL; |
| 9933 | if (errno == ERANGE) |
| 9934 | continue; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9935 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9936 | goto exit; |
| 9937 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9938 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9939 | if (result != buffer_size) { |
| 9940 | /* Can only shrink. */ |
| 9941 | _PyBytes_Resize(&buffer, result); |
| 9942 | } |
| 9943 | break; |
| 9944 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9945 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9946 | exit: |
| 9947 | path_cleanup(&path); |
| 9948 | path_cleanup(&attribute); |
| 9949 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9950 | } |
| 9951 | |
| 9952 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9953 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 9954 | Set extended attribute attribute on path to value.\n\ |
| 9955 | path may be either a string or an open file descriptor.\n\ |
| 9956 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 9957 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 9958 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9959 | |
| 9960 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9961 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9962 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9963 | path_t path; |
| 9964 | path_t attribute; |
| 9965 | Py_buffer value; |
| 9966 | int flags = 0; |
| 9967 | int follow_symlinks = 1; |
| 9968 | int result; |
| 9969 | PyObject *return_value = NULL; |
| 9970 | static char *keywords[] = {"path", "attribute", "value", |
| 9971 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9972 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9973 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9974 | path.function_name = "setxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9975 | path.allow_fd = 1; |
| 9976 | memset(&attribute, 0, sizeof(attribute)); |
| 9977 | memset(&value, 0, sizeof(value)); |
| 9978 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 9979 | keywords, |
| 9980 | path_converter, &path, |
| 9981 | path_converter, &attribute, |
| 9982 | &value, &flags, |
| 9983 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9984 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9985 | |
| 9986 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 9987 | goto exit; |
| 9988 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9989 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9990 | if (path.fd > -1) |
| 9991 | result = fsetxattr(path.fd, attribute.narrow, |
| 9992 | value.buf, value.len, flags); |
| 9993 | else if (follow_symlinks) |
| 9994 | result = setxattr(path.narrow, attribute.narrow, |
| 9995 | value.buf, value.len, flags); |
| 9996 | else |
| 9997 | result = lsetxattr(path.narrow, attribute.narrow, |
| 9998 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9999 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10000 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10001 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10002 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10003 | goto exit; |
| 10004 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10005 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10006 | return_value = Py_None; |
| 10007 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10008 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10009 | exit: |
| 10010 | path_cleanup(&path); |
| 10011 | path_cleanup(&attribute); |
| 10012 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10013 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10014 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10015 | } |
| 10016 | |
| 10017 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10018 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 10019 | Remove extended attribute attribute on path.\n\ |
| 10020 | path may be either a string or an open file descriptor.\n\ |
| 10021 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10022 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 10023 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10024 | |
| 10025 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10026 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10027 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10028 | path_t path; |
| 10029 | path_t attribute; |
| 10030 | int follow_symlinks = 1; |
| 10031 | int result; |
| 10032 | PyObject *return_value = NULL; |
| 10033 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10034 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10035 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10036 | path.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10037 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10038 | attribute.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10039 | path.allow_fd = 1; |
| 10040 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 10041 | keywords, |
| 10042 | path_converter, &path, |
| 10043 | path_converter, &attribute, |
| 10044 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10045 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10046 | |
| 10047 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 10048 | goto exit; |
| 10049 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10050 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10051 | if (path.fd > -1) |
| 10052 | result = fremovexattr(path.fd, attribute.narrow); |
| 10053 | else if (follow_symlinks) |
| 10054 | result = removexattr(path.narrow, attribute.narrow); |
| 10055 | else |
| 10056 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10057 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10058 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10059 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10060 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10061 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10062 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10063 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10064 | return_value = Py_None; |
| 10065 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10066 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10067 | exit: |
| 10068 | path_cleanup(&path); |
| 10069 | path_cleanup(&attribute); |
| 10070 | |
| 10071 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10072 | } |
| 10073 | |
| 10074 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10075 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 10076 | Return a list of extended attributes on path.\n\ |
| 10077 | \n\ |
| 10078 | path may be either None, a string, or an open file descriptor.\n\ |
| 10079 | if path is None, listxattr will examine the current directory.\n\ |
| 10080 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10081 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 10082 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10083 | |
| 10084 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10085 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10086 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10087 | path_t path; |
| 10088 | int follow_symlinks = 1; |
| 10089 | Py_ssize_t i; |
| 10090 | PyObject *result = NULL; |
| 10091 | char *buffer = NULL; |
| 10092 | char *name; |
| 10093 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10094 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10095 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10096 | path.function_name = "listxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10097 | path.allow_fd = 1; |
| 10098 | path.fd = -1; |
| 10099 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 10100 | path_converter, &path, |
| 10101 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10102 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10103 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10104 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 10105 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10106 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10107 | name = path.narrow ? path.narrow : "."; |
| 10108 | for (i = 0; ; i++) { |
| 10109 | char *start, *trace, *end; |
| 10110 | ssize_t length; |
| 10111 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10112 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10113 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10114 | /* ERANGE */ |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10115 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10116 | break; |
| 10117 | } |
| 10118 | buffer = PyMem_MALLOC(buffer_size); |
| 10119 | if (!buffer) { |
| 10120 | PyErr_NoMemory(); |
| 10121 | break; |
| 10122 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10123 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10124 | Py_BEGIN_ALLOW_THREADS; |
| 10125 | if (path.fd > -1) |
| 10126 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10127 | else if (follow_symlinks) |
| 10128 | length = listxattr(name, buffer, buffer_size); |
| 10129 | else |
| 10130 | length = llistxattr(name, buffer, buffer_size); |
| 10131 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10132 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10133 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10134 | if (errno == ERANGE) { |
| 10135 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 10136 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10137 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 10138 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10139 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10140 | break; |
| 10141 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10142 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10143 | result = PyList_New(0); |
| 10144 | if (!result) { |
| 10145 | goto exit; |
| 10146 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10147 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10148 | end = buffer + length; |
| 10149 | for (trace = start = buffer; trace != end; trace++) { |
| 10150 | if (!*trace) { |
| 10151 | int error; |
| 10152 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10153 | trace - start); |
| 10154 | if (!attribute) { |
| 10155 | Py_DECREF(result); |
| 10156 | result = NULL; |
| 10157 | goto exit; |
| 10158 | } |
| 10159 | error = PyList_Append(result, attribute); |
| 10160 | Py_DECREF(attribute); |
| 10161 | if (error) { |
| 10162 | Py_DECREF(result); |
| 10163 | result = NULL; |
| 10164 | goto exit; |
| 10165 | } |
| 10166 | start = trace + 1; |
| 10167 | } |
| 10168 | } |
| 10169 | break; |
| 10170 | } |
| 10171 | exit: |
| 10172 | path_cleanup(&path); |
| 10173 | if (buffer) |
| 10174 | PyMem_FREE(buffer); |
| 10175 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10176 | } |
| 10177 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10178 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10179 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10180 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10181 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10182 | "urandom(n) -> str\n\n\ |
| 10183 | Return n random bytes suitable for cryptographic use."); |
| 10184 | |
| 10185 | static PyObject * |
| 10186 | posix_urandom(PyObject *self, PyObject *args) |
| 10187 | { |
| 10188 | Py_ssize_t size; |
| 10189 | PyObject *result; |
| 10190 | int ret; |
| 10191 | |
| 10192 | /* Read arguments */ |
| 10193 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10194 | return NULL; |
| 10195 | if (size < 0) |
| 10196 | return PyErr_Format(PyExc_ValueError, |
| 10197 | "negative argument not allowed"); |
| 10198 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10199 | if (result == NULL) |
| 10200 | return NULL; |
| 10201 | |
| 10202 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10203 | PyBytes_GET_SIZE(result)); |
| 10204 | if (ret == -1) { |
| 10205 | Py_DECREF(result); |
| 10206 | return NULL; |
| 10207 | } |
| 10208 | return result; |
| 10209 | } |
| 10210 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10211 | /* Terminal size querying */ |
| 10212 | |
| 10213 | static PyTypeObject TerminalSizeType; |
| 10214 | |
| 10215 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10216 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10217 | |
| 10218 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10219 | {"columns", "width of the terminal window in characters"}, |
| 10220 | {"lines", "height of the terminal window in characters"}, |
| 10221 | {NULL, NULL} |
| 10222 | }; |
| 10223 | |
| 10224 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10225 | "os.terminal_size", |
| 10226 | TerminalSize_docstring, |
| 10227 | TerminalSize_fields, |
| 10228 | 2, |
| 10229 | }; |
| 10230 | |
| 10231 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10232 | PyDoc_STRVAR(termsize__doc__, |
| 10233 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10234 | "\n" \ |
| 10235 | "The optional argument fd (default standard output) specifies\n" \ |
| 10236 | "which file descriptor should be queried.\n" \ |
| 10237 | "\n" \ |
| 10238 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10239 | "is thrown.\n" \ |
| 10240 | "\n" \ |
| 10241 | "This function will only be defined if an implementation is\n" \ |
| 10242 | "available for this system.\n" \ |
| 10243 | "\n" \ |
| 10244 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10245 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10246 | |
| 10247 | static PyObject* |
| 10248 | get_terminal_size(PyObject *self, PyObject *args) |
| 10249 | { |
| 10250 | int columns, lines; |
| 10251 | PyObject *termsize; |
| 10252 | |
| 10253 | int fd = fileno(stdout); |
| 10254 | /* Under some conditions stdout may not be connected and |
| 10255 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10256 | * GUI apps don't have valid standard streams by default. |
| 10257 | * |
| 10258 | * If this happens, and the optional fd argument is not present, |
| 10259 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10260 | */ |
| 10261 | |
| 10262 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10263 | return NULL; |
| 10264 | |
| 10265 | #ifdef TERMSIZE_USE_IOCTL |
| 10266 | { |
| 10267 | struct winsize w; |
| 10268 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10269 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10270 | columns = w.ws_col; |
| 10271 | lines = w.ws_row; |
| 10272 | } |
| 10273 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10274 | |
| 10275 | #ifdef TERMSIZE_USE_CONIO |
| 10276 | { |
| 10277 | DWORD nhandle; |
| 10278 | HANDLE handle; |
| 10279 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10280 | switch (fd) { |
| 10281 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10282 | break; |
| 10283 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10284 | break; |
| 10285 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10286 | break; |
| 10287 | default: |
| 10288 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10289 | } |
| 10290 | handle = GetStdHandle(nhandle); |
| 10291 | if (handle == NULL) |
| 10292 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10293 | if (handle == INVALID_HANDLE_VALUE) |
| 10294 | return PyErr_SetFromWindowsErr(0); |
| 10295 | |
| 10296 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10297 | return PyErr_SetFromWindowsErr(0); |
| 10298 | |
| 10299 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10300 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10301 | } |
| 10302 | #endif /* TERMSIZE_USE_CONIO */ |
| 10303 | |
| 10304 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10305 | if (termsize == NULL) |
| 10306 | return NULL; |
| 10307 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10308 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10309 | if (PyErr_Occurred()) { |
| 10310 | Py_DECREF(termsize); |
| 10311 | return NULL; |
| 10312 | } |
| 10313 | return termsize; |
| 10314 | } |
| 10315 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10316 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10317 | PyDoc_STRVAR(posix_cpu_count__doc__, |
| 10318 | "cpu_count() -> integer\n\n\ |
| 10319 | Return the number of CPUs in the system, or None if this value cannot be\n\ |
| 10320 | established."); |
| 10321 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10322 | static PyObject * |
| 10323 | posix_cpu_count(PyObject *self) |
| 10324 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 10325 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10326 | #ifdef MS_WINDOWS |
| 10327 | SYSTEM_INFO sysinfo; |
| 10328 | GetSystemInfo(&sysinfo); |
| 10329 | ncpu = sysinfo.dwNumberOfProcessors; |
| 10330 | #elif defined(__hpux) |
| 10331 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 10332 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 10333 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10334 | #elif defined(__DragonFly__) || \ |
| 10335 | defined(__OpenBSD__) || \ |
| 10336 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 10337 | defined(__NetBSD__) || \ |
| 10338 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 10339 | int mib[2]; |
| 10340 | size_t len = sizeof(ncpu); |
| 10341 | mib[0] = CTL_HW; |
| 10342 | mib[1] = HW_NCPU; |
| 10343 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 10344 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10345 | #endif |
| 10346 | if (ncpu >= 1) |
| 10347 | return PyLong_FromLong(ncpu); |
| 10348 | else |
| 10349 | Py_RETURN_NONE; |
| 10350 | } |
| 10351 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10352 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10353 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10354 | {"access", (PyCFunction)posix_access, |
| 10355 | METH_VARARGS | METH_KEYWORDS, |
| 10356 | posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10357 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10358 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10359 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10360 | {"chdir", (PyCFunction)posix_chdir, |
| 10361 | METH_VARARGS | METH_KEYWORDS, |
| 10362 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10363 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10364 | {"chflags", (PyCFunction)posix_chflags, |
| 10365 | METH_VARARGS | METH_KEYWORDS, |
| 10366 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10367 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10368 | {"chmod", (PyCFunction)posix_chmod, |
| 10369 | METH_VARARGS | METH_KEYWORDS, |
| 10370 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10371 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10372 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10373 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10374 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10375 | {"chown", (PyCFunction)posix_chown, |
| 10376 | METH_VARARGS | METH_KEYWORDS, |
| 10377 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10378 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10379 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10380 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10381 | #endif /* HAVE_LCHMOD */ |
| 10382 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10383 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10384 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10385 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10386 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10387 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10388 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10389 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10390 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10391 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10392 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10393 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10394 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10395 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10396 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10397 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10398 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10399 | METH_NOARGS, posix_getcwd__doc__}, |
| 10400 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10401 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10402 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10403 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 10404 | {"link", (PyCFunction)posix_link, |
| 10405 | METH_VARARGS | METH_KEYWORDS, |
| 10406 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10407 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10408 | {"listdir", (PyCFunction)posix_listdir, |
| 10409 | METH_VARARGS | METH_KEYWORDS, |
| 10410 | posix_listdir__doc__}, |
| 10411 | {"lstat", (PyCFunction)posix_lstat, |
| 10412 | METH_VARARGS | METH_KEYWORDS, |
| 10413 | posix_lstat__doc__}, |
| 10414 | {"mkdir", (PyCFunction)posix_mkdir, |
| 10415 | METH_VARARGS | METH_KEYWORDS, |
| 10416 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10417 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10418 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10419 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10420 | #ifdef HAVE_GETPRIORITY |
| 10421 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10422 | #endif /* HAVE_GETPRIORITY */ |
| 10423 | #ifdef HAVE_SETPRIORITY |
| 10424 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10425 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10426 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10427 | {"readlink", (PyCFunction)posix_readlink, |
| 10428 | METH_VARARGS | METH_KEYWORDS, |
| 10429 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10430 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10431 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10432 | {"readlink", (PyCFunction)win_readlink, |
| 10433 | METH_VARARGS | METH_KEYWORDS, |
| 10434 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10435 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10436 | {"rename", (PyCFunction)posix_rename, |
| 10437 | METH_VARARGS | METH_KEYWORDS, |
| 10438 | posix_rename__doc__}, |
| 10439 | {"replace", (PyCFunction)posix_replace, |
| 10440 | METH_VARARGS | METH_KEYWORDS, |
| 10441 | posix_replace__doc__}, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 10442 | {"rmdir", (PyCFunction)posix_rmdir, |
| 10443 | METH_VARARGS | METH_KEYWORDS, |
| 10444 | posix_rmdir__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10445 | {"stat", (PyCFunction)posix_stat, |
| 10446 | METH_VARARGS | METH_KEYWORDS, |
| 10447 | posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10448 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10449 | #if defined(HAVE_SYMLINK) |
| 10450 | {"symlink", (PyCFunction)posix_symlink, |
| 10451 | METH_VARARGS | METH_KEYWORDS, |
| 10452 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10453 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10454 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10455 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10456 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10457 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10458 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10459 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10460 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10461 | {"unlink", (PyCFunction)posix_unlink, |
| 10462 | METH_VARARGS | METH_KEYWORDS, |
| 10463 | posix_unlink__doc__}, |
| 10464 | {"remove", (PyCFunction)posix_unlink, |
| 10465 | METH_VARARGS | METH_KEYWORDS, |
| 10466 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10467 | {"utime", (PyCFunction)posix_utime, |
| 10468 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10469 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10470 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10471 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10472 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10473 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10474 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10475 | {"execve", (PyCFunction)posix_execve, |
| 10476 | METH_VARARGS | METH_KEYWORDS, |
| 10477 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10478 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10479 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10480 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10481 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10482 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10483 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10484 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10485 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10486 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10487 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10488 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10489 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10490 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10491 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10492 | {"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] | 10493 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10494 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10495 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10496 | #endif |
| 10497 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10498 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10499 | #endif |
| 10500 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10501 | {"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] | 10502 | #endif |
| 10503 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10504 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10505 | #endif |
| 10506 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10507 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10508 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10509 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10510 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10511 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10512 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10513 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10514 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10515 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10516 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10517 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10518 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10519 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10520 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10521 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10522 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 10523 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10524 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10525 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 10526 | #endif /* HAVE_GETEUID */ |
| 10527 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10528 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10529 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10530 | #ifdef HAVE_GETGROUPLIST |
| 10531 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10532 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10533 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10534 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10535 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10536 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10537 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10538 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10539 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10540 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10541 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10542 | #endif /* HAVE_GETPPID */ |
| 10543 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10544 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10545 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10546 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10547 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10548 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10549 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10550 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10551 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10552 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10553 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10554 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10555 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10556 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10557 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10558 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10559 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10560 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10561 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10562 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10563 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10564 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10565 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10566 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10567 | #endif /* HAVE_SETEUID */ |
| 10568 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10569 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10570 | #endif /* HAVE_SETEGID */ |
| 10571 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10572 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10573 | #endif /* HAVE_SETREUID */ |
| 10574 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10575 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10576 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10577 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10578 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10579 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10580 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10581 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10582 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10583 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10584 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10585 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10586 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10587 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10588 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10589 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10590 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10591 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10592 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10593 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10594 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10595 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10596 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10597 | #endif /* HAVE_WAIT3 */ |
| 10598 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10599 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10600 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10601 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10602 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10603 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10604 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10605 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10606 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10607 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10608 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10609 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10610 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10611 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10612 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10613 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10614 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10615 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10616 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10617 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10618 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10619 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10620 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10621 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10622 | {"open", (PyCFunction)posix_open,\ |
| 10623 | METH_VARARGS | METH_KEYWORDS, |
| 10624 | posix_open__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10625 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10626 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10627 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10628 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10629 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10630 | #ifdef HAVE_LOCKF |
| 10631 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10632 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10633 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10634 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10635 | #ifdef HAVE_READV |
| 10636 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10637 | #endif |
| 10638 | #ifdef HAVE_PREAD |
| 10639 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10640 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10641 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10642 | #ifdef HAVE_WRITEV |
| 10643 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10644 | #endif |
| 10645 | #ifdef HAVE_PWRITE |
| 10646 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10647 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10648 | #ifdef HAVE_SENDFILE |
| 10649 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10650 | posix_sendfile__doc__}, |
| 10651 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10652 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10653 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10654 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10655 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10656 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10657 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10658 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10659 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10660 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10661 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 10662 | METH_VARARGS | METH_KEYWORDS, |
| 10663 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10664 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10665 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10666 | {"mknod", (PyCFunction)posix_mknod, |
| 10667 | METH_VARARGS | METH_KEYWORDS, |
| 10668 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10669 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10670 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10671 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10672 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10673 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10674 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10675 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10676 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10677 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10678 | #ifdef HAVE_TRUNCATE |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10679 | {"truncate", (PyCFunction)posix_truncate, |
| 10680 | METH_VARARGS | METH_KEYWORDS, |
| 10681 | posix_truncate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10682 | #endif |
| 10683 | #ifdef HAVE_POSIX_FALLOCATE |
| 10684 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10685 | #endif |
| 10686 | #ifdef HAVE_POSIX_FADVISE |
| 10687 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10688 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10689 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10691 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10692 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10693 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10694 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10695 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10696 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10697 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10698 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10699 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10700 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10701 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10702 | #ifdef HAVE_SYNC |
| 10703 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10704 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10705 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10706 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10707 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10708 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10709 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10710 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10711 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10712 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10713 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10714 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10715 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10716 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10717 | #endif /* WIFSTOPPED */ |
| 10718 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10719 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10720 | #endif /* WIFSIGNALED */ |
| 10721 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10722 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10723 | #endif /* WIFEXITED */ |
| 10724 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10725 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10726 | #endif /* WEXITSTATUS */ |
| 10727 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10728 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10729 | #endif /* WTERMSIG */ |
| 10730 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10731 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10732 | #endif /* WSTOPSIG */ |
| 10733 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10734 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10735 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10736 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10737 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10738 | {"statvfs", (PyCFunction)posix_statvfs, |
| 10739 | METH_VARARGS | METH_KEYWORDS, |
| 10740 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10741 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10742 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10744 | #endif |
| 10745 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10746 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10747 | #endif |
| 10748 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10749 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10750 | #endif |
| 10751 | #ifdef HAVE_PATHCONF |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10752 | {"pathconf", (PyCFunction)posix_pathconf, |
| 10753 | METH_VARARGS | METH_KEYWORDS, |
| 10754 | posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10755 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10756 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10757 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10758 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10759 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10760 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10761 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10762 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10763 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10764 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10765 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 10766 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10767 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10768 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10769 | #endif |
| 10770 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10771 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10772 | #endif |
| 10773 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10774 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10775 | #endif |
| 10776 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10777 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10778 | #endif |
| 10779 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10780 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10781 | {"setxattr", (PyCFunction)posix_setxattr, |
| 10782 | METH_VARARGS | METH_KEYWORDS, |
| 10783 | posix_setxattr__doc__}, |
| 10784 | {"getxattr", (PyCFunction)posix_getxattr, |
| 10785 | METH_VARARGS | METH_KEYWORDS, |
| 10786 | posix_getxattr__doc__}, |
| 10787 | {"removexattr", (PyCFunction)posix_removexattr, |
| 10788 | METH_VARARGS | METH_KEYWORDS, |
| 10789 | posix_removexattr__doc__}, |
| 10790 | {"listxattr", (PyCFunction)posix_listxattr, |
| 10791 | METH_VARARGS | METH_KEYWORDS, |
| 10792 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10793 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10794 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10795 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 10796 | #endif |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 10797 | {"cpu_count", (PyCFunction)posix_cpu_count, |
| 10798 | METH_NOARGS, posix_cpu_count__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10799 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10800 | }; |
| 10801 | |
| 10802 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10803 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10804 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10805 | enable_symlink() |
| 10806 | { |
| 10807 | HANDLE tok; |
| 10808 | TOKEN_PRIVILEGES tok_priv; |
| 10809 | LUID luid; |
| 10810 | int meth_idx = 0; |
| 10811 | |
| 10812 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10813 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10814 | |
| 10815 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10816 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10817 | |
| 10818 | tok_priv.PrivilegeCount = 1; |
| 10819 | tok_priv.Privileges[0].Luid = luid; |
| 10820 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 10821 | |
| 10822 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 10823 | sizeof(TOKEN_PRIVILEGES), |
| 10824 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10825 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10826 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10827 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 10828 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10829 | } |
| 10830 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 10831 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10832 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10833 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10834 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10835 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10836 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10837 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10838 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10839 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10840 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10841 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10842 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10843 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10844 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10845 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10846 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10847 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10848 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10849 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10850 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10851 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10852 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10853 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10854 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10855 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10856 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10857 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10858 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10859 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10860 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10861 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10862 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10863 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10864 | #endif |
| 10865 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10866 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10867 | #endif |
| 10868 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10869 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10870 | #endif |
| 10871 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10872 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10873 | #endif |
| 10874 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10875 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10876 | #endif |
| 10877 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10878 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10879 | #endif |
| 10880 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10881 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10882 | #endif |
| 10883 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10884 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10885 | #endif |
| 10886 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10887 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10888 | #endif |
| 10889 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10890 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10891 | #endif |
| 10892 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10893 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10894 | #endif |
| 10895 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10896 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10897 | #endif |
| 10898 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10899 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10900 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10901 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10902 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10903 | #endif |
| 10904 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10905 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10906 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10907 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10908 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10909 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10910 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10911 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10912 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10913 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10914 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10915 | #endif |
| 10916 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10917 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10918 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 10919 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10920 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 10921 | #endif |
| 10922 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10923 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 10924 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 10925 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10926 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 10927 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 10928 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10929 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 10930 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10931 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10932 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10933 | #endif |
| 10934 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10935 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10936 | #endif |
| 10937 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10938 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10939 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 10940 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10941 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 10942 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10943 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10944 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10945 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10946 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10947 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 10948 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10949 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 10950 | #endif |
| 10951 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10952 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 10953 | #endif |
| 10954 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10955 | /* MS Windows */ |
| 10956 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10957 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10958 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10959 | #endif |
| 10960 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10961 | /* Optimize for short life (keep in memory). */ |
| 10962 | /* MS forgot to define this one with a non-underscore form too. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10963 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10964 | #endif |
| 10965 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10966 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10967 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10968 | #endif |
| 10969 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10970 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10971 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10972 | #endif |
| 10973 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10974 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10975 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10976 | #endif |
| 10977 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10978 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 10979 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | /* Send a SIGIO signal whenever input or output |
| 10981 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10982 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 10983 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10984 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10985 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10986 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10987 | #endif |
| 10988 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10989 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10990 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10991 | #endif |
| 10992 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10993 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10994 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10995 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10996 | #ifdef O_NOLINKS |
| 10997 | /* Fails if link count of the named file is greater than 1 */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 10998 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10999 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11000 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11001 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11002 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11003 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11005 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11006 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11007 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11008 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11009 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11010 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11011 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11012 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11013 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11014 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11015 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11016 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11017 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11018 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11019 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11020 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11021 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11022 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11023 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11024 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11025 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11026 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11027 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11028 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11029 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11030 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11031 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11032 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11033 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11034 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11035 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11036 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11037 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11038 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11039 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11040 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11041 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11042 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11043 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11044 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11045 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11046 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11047 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11048 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11049 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11050 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11051 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11052 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11053 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11054 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11055 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11056 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11057 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11058 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11059 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11060 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11061 | #endif /* ST_RDONLY */ |
| 11062 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11063 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11064 | #endif /* ST_NOSUID */ |
| 11065 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11066 | /* FreeBSD sendfile() constants */ |
| 11067 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11068 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11069 | #endif |
| 11070 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11071 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11072 | #endif |
| 11073 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11074 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11075 | #endif |
| 11076 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11077 | /* constants for posix_fadvise */ |
| 11078 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11079 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11080 | #endif |
| 11081 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11082 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11083 | #endif |
| 11084 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11085 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11086 | #endif |
| 11087 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11088 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11089 | #endif |
| 11090 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11091 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11092 | #endif |
| 11093 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11094 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11095 | #endif |
| 11096 | |
| 11097 | /* constants for waitid */ |
| 11098 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11099 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 11100 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 11101 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11102 | #endif |
| 11103 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11104 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11105 | #endif |
| 11106 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11107 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11108 | #endif |
| 11109 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11110 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11111 | #endif |
| 11112 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11113 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11114 | #endif |
| 11115 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11116 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11117 | #endif |
| 11118 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11119 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11120 | #endif |
| 11121 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11122 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11123 | #endif |
| 11124 | |
| 11125 | /* constants for lockf */ |
| 11126 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11127 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11128 | #endif |
| 11129 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11130 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11131 | #endif |
| 11132 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11133 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11134 | #endif |
| 11135 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11136 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11137 | #endif |
| 11138 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11139 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11140 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 11141 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 11142 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 11143 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 11144 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11145 | #endif |
| 11146 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11147 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11148 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 11149 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 11150 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11151 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11152 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11153 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11154 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11155 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11156 | #endif |
| 11157 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11158 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11159 | #endif |
| 11160 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11161 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11162 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11163 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11164 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11165 | #endif |
| 11166 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11167 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11168 | #endif |
| 11169 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11170 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11171 | #endif |
| 11172 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11173 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11174 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11175 | #endif |
| 11176 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11177 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11178 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 11179 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 11180 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11181 | #endif |
| 11182 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11183 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11184 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11185 | #endif |
| 11186 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11187 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11188 | #endif |
| 11189 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11190 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11191 | #endif |
| 11192 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11193 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11194 | #endif |
| 11195 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11196 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11197 | #endif |
| 11198 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11199 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11200 | #endif |
| 11201 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 11202 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11203 | #endif |
| 11204 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11205 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11206 | } |
| 11207 | |
| 11208 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11209 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11210 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11211 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11212 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11213 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11214 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11215 | #define MODNAME "posix" |
| 11216 | #endif |
| 11217 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11218 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11219 | PyModuleDef_HEAD_INIT, |
| 11220 | MODNAME, |
| 11221 | posix__doc__, |
| 11222 | -1, |
| 11223 | posix_methods, |
| 11224 | NULL, |
| 11225 | NULL, |
| 11226 | NULL, |
| 11227 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11228 | }; |
| 11229 | |
| 11230 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11231 | static char *have_functions[] = { |
| 11232 | |
| 11233 | #ifdef HAVE_FACCESSAT |
| 11234 | "HAVE_FACCESSAT", |
| 11235 | #endif |
| 11236 | |
| 11237 | #ifdef HAVE_FCHDIR |
| 11238 | "HAVE_FCHDIR", |
| 11239 | #endif |
| 11240 | |
| 11241 | #ifdef HAVE_FCHMOD |
| 11242 | "HAVE_FCHMOD", |
| 11243 | #endif |
| 11244 | |
| 11245 | #ifdef HAVE_FCHMODAT |
| 11246 | "HAVE_FCHMODAT", |
| 11247 | #endif |
| 11248 | |
| 11249 | #ifdef HAVE_FCHOWN |
| 11250 | "HAVE_FCHOWN", |
| 11251 | #endif |
| 11252 | |
| 11253 | #ifdef HAVE_FEXECVE |
| 11254 | "HAVE_FEXECVE", |
| 11255 | #endif |
| 11256 | |
| 11257 | #ifdef HAVE_FDOPENDIR |
| 11258 | "HAVE_FDOPENDIR", |
| 11259 | #endif |
| 11260 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11261 | #ifdef HAVE_FPATHCONF |
| 11262 | "HAVE_FPATHCONF", |
| 11263 | #endif |
| 11264 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11265 | #ifdef HAVE_FSTATAT |
| 11266 | "HAVE_FSTATAT", |
| 11267 | #endif |
| 11268 | |
| 11269 | #ifdef HAVE_FSTATVFS |
| 11270 | "HAVE_FSTATVFS", |
| 11271 | #endif |
| 11272 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11273 | #ifdef HAVE_FTRUNCATE |
| 11274 | "HAVE_FTRUNCATE", |
| 11275 | #endif |
| 11276 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11277 | #ifdef HAVE_FUTIMENS |
| 11278 | "HAVE_FUTIMENS", |
| 11279 | #endif |
| 11280 | |
| 11281 | #ifdef HAVE_FUTIMES |
| 11282 | "HAVE_FUTIMES", |
| 11283 | #endif |
| 11284 | |
| 11285 | #ifdef HAVE_FUTIMESAT |
| 11286 | "HAVE_FUTIMESAT", |
| 11287 | #endif |
| 11288 | |
| 11289 | #ifdef HAVE_LINKAT |
| 11290 | "HAVE_LINKAT", |
| 11291 | #endif |
| 11292 | |
| 11293 | #ifdef HAVE_LCHFLAGS |
| 11294 | "HAVE_LCHFLAGS", |
| 11295 | #endif |
| 11296 | |
| 11297 | #ifdef HAVE_LCHMOD |
| 11298 | "HAVE_LCHMOD", |
| 11299 | #endif |
| 11300 | |
| 11301 | #ifdef HAVE_LCHOWN |
| 11302 | "HAVE_LCHOWN", |
| 11303 | #endif |
| 11304 | |
| 11305 | #ifdef HAVE_LSTAT |
| 11306 | "HAVE_LSTAT", |
| 11307 | #endif |
| 11308 | |
| 11309 | #ifdef HAVE_LUTIMES |
| 11310 | "HAVE_LUTIMES", |
| 11311 | #endif |
| 11312 | |
| 11313 | #ifdef HAVE_MKDIRAT |
| 11314 | "HAVE_MKDIRAT", |
| 11315 | #endif |
| 11316 | |
| 11317 | #ifdef HAVE_MKFIFOAT |
| 11318 | "HAVE_MKFIFOAT", |
| 11319 | #endif |
| 11320 | |
| 11321 | #ifdef HAVE_MKNODAT |
| 11322 | "HAVE_MKNODAT", |
| 11323 | #endif |
| 11324 | |
| 11325 | #ifdef HAVE_OPENAT |
| 11326 | "HAVE_OPENAT", |
| 11327 | #endif |
| 11328 | |
| 11329 | #ifdef HAVE_READLINKAT |
| 11330 | "HAVE_READLINKAT", |
| 11331 | #endif |
| 11332 | |
| 11333 | #ifdef HAVE_RENAMEAT |
| 11334 | "HAVE_RENAMEAT", |
| 11335 | #endif |
| 11336 | |
| 11337 | #ifdef HAVE_SYMLINKAT |
| 11338 | "HAVE_SYMLINKAT", |
| 11339 | #endif |
| 11340 | |
| 11341 | #ifdef HAVE_UNLINKAT |
| 11342 | "HAVE_UNLINKAT", |
| 11343 | #endif |
| 11344 | |
| 11345 | #ifdef HAVE_UTIMENSAT |
| 11346 | "HAVE_UTIMENSAT", |
| 11347 | #endif |
| 11348 | |
| 11349 | #ifdef MS_WINDOWS |
| 11350 | "MS_WINDOWS", |
| 11351 | #endif |
| 11352 | |
| 11353 | NULL |
| 11354 | }; |
| 11355 | |
| 11356 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11357 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11358 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11359 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11360 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11361 | PyObject *list; |
| 11362 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11363 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11364 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11365 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11366 | #endif |
| 11367 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11368 | m = PyModule_Create(&posixmodule); |
| 11369 | if (m == NULL) |
| 11370 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11371 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11372 | /* Initialize environ dictionary */ |
| 11373 | v = convertenviron(); |
| 11374 | Py_XINCREF(v); |
| 11375 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11376 | return NULL; |
| 11377 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11378 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11379 | if (all_ins(m)) |
| 11380 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11382 | if (setup_confname_tables(m)) |
| 11383 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11384 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11385 | Py_INCREF(PyExc_OSError); |
| 11386 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11387 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11388 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11389 | if (posix_putenv_garbage == NULL) |
| 11390 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11391 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11393 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11394 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11395 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11396 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11397 | #endif |
| 11398 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11399 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11400 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11401 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11402 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11403 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11404 | structseq_new = StatResultType.tp_new; |
| 11405 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11406 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11407 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11408 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11409 | #ifdef NEED_TICKS_PER_SECOND |
| 11410 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11411 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11412 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11413 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11414 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11415 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11416 | # endif |
| 11417 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11418 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11419 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11420 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11421 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11422 | SchedParamType.tp_new = sched_param_new; |
| 11423 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11424 | |
| 11425 | /* initialize TerminalSize_info */ |
| 11426 | PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11427 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11428 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11429 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11430 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11431 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11432 | Py_INCREF((PyObject*) &StatResultType); |
| 11433 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11434 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11435 | PyModule_AddObject(m, "statvfs_result", |
| 11436 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11437 | |
| 11438 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11439 | Py_INCREF(&SchedParamType); |
| 11440 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11441 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11442 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 11443 | times_result_desc.name = MODNAME ".times_result"; |
| 11444 | PyStructSequence_InitType(&TimesResultType, ×_result_desc); |
| 11445 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 11446 | |
| 11447 | uname_result_desc.name = MODNAME ".uname_result"; |
| 11448 | PyStructSequence_InitType(&UnameResultType, &uname_result_desc); |
| 11449 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 11450 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11451 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11452 | /* |
| 11453 | * Step 2 of weak-linking support on Mac OS X. |
| 11454 | * |
| 11455 | * The code below removes functions that are not available on the |
| 11456 | * currently active platform. |
| 11457 | * |
| 11458 | * This block allow one to use a python binary that was build on |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11459 | * OSX 10.4 on OSX 10.3, without losing access to new APIs on |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11460 | * OSX 10.4. |
| 11461 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11462 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11463 | if (fstatvfs == NULL) { |
| 11464 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11465 | return NULL; |
| 11466 | } |
| 11467 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11468 | #endif /* HAVE_FSTATVFS */ |
| 11469 | |
| 11470 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11471 | if (statvfs == NULL) { |
| 11472 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11473 | return NULL; |
| 11474 | } |
| 11475 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11476 | #endif /* HAVE_STATVFS */ |
| 11477 | |
| 11478 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11479 | if (lchown == NULL) { |
| 11480 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11481 | return NULL; |
| 11482 | } |
| 11483 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11484 | #endif /* HAVE_LCHOWN */ |
| 11485 | |
| 11486 | |
| 11487 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11488 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 11489 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11490 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 11491 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 11492 | billion = PyLong_FromLong(1000000000); |
| 11493 | if (!billion) |
| 11494 | return NULL; |
| 11495 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11496 | /* suppress "function not used" warnings */ |
| 11497 | { |
| 11498 | int ignored; |
| 11499 | fd_specified("", -1); |
| 11500 | follow_symlinks_specified("", 1); |
| 11501 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 11502 | dir_fd_converter(Py_None, &ignored); |
| 11503 | dir_fd_unavailable(Py_None, &ignored); |
| 11504 | } |
| 11505 | |
| 11506 | /* |
| 11507 | * provide list of locally available functions |
| 11508 | * so os.py can populate support_* lists |
| 11509 | */ |
| 11510 | list = PyList_New(0); |
| 11511 | if (!list) |
| 11512 | return NULL; |
| 11513 | for (trace = have_functions; *trace; trace++) { |
| 11514 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 11515 | if (!unicode) |
| 11516 | return NULL; |
| 11517 | if (PyList_Append(list, unicode)) |
| 11518 | return NULL; |
| 11519 | Py_DECREF(unicode); |
| 11520 | } |
| 11521 | PyModule_AddObject(m, "_have_functions", list); |
| 11522 | |
| 11523 | initialized = 1; |
| 11524 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11525 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11526 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11527 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11528 | |
| 11529 | #ifdef __cplusplus |
| 11530 | } |
| 11531 | #endif |