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 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 116 | #if defined(MS_WINDOWS) |
| 117 | # define TERMSIZE_USE_CONIO |
| 118 | #elif defined(HAVE_SYS_IOCTL_H) |
| 119 | # include <sys/ioctl.h> |
| 120 | # if defined(HAVE_TERMIOS_H) |
| 121 | # include <termios.h> |
| 122 | # endif |
| 123 | # if defined(TIOCGWINSZ) |
| 124 | # define TERMSIZE_USE_IOCTL |
| 125 | # endif |
| 126 | #endif /* MS_WINDOWS */ |
| 127 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 129 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 130 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 131 | #define HAVE_GETCWD 1 |
| 132 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 133 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #include <process.h> |
| 135 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 136 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #define HAVE_EXECV 1 |
| 138 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 139 | #define HAVE_OPENDIR 1 |
| 140 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 141 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 142 | #define HAVE_WAIT 1 |
| 143 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 144 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 145 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 146 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 147 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 148 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 149 | #define HAVE_EXECV 1 |
| 150 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 151 | #define HAVE_SYSTEM 1 |
| 152 | #define HAVE_CWAIT 1 |
| 153 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 154 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 155 | #else |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 156 | #if defined(__VMS) |
| 157 | /* Everything needed is defined in vms/pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 158 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | /* Unix functions that the configure script doesn't check for */ |
| 160 | #define HAVE_EXECV 1 |
| 161 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 162 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 163 | #define HAVE_FORK1 1 |
| 164 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 165 | #define HAVE_GETCWD 1 |
| 166 | #define HAVE_GETEGID 1 |
| 167 | #define HAVE_GETEUID 1 |
| 168 | #define HAVE_GETGID 1 |
| 169 | #define HAVE_GETPPID 1 |
| 170 | #define HAVE_GETUID 1 |
| 171 | #define HAVE_KILL 1 |
| 172 | #define HAVE_OPENDIR 1 |
| 173 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 174 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 176 | #define HAVE_TTYNAME 1 |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 177 | #endif /* __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | #endif /* _MSC_VER */ |
| 179 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 180 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 181 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 182 | |
| 183 | |
| 184 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 186 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 187 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 188 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 189 | (default) */ |
| 190 | extern char *ctermid_r(char *); |
| 191 | #endif |
| 192 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 193 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 194 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 196 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 197 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 198 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 199 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 201 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 202 | #endif |
| 203 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int chdir(char *); |
| 205 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 206 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 207 | extern int chdir(const char *); |
| 208 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 209 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 210 | #ifdef __BORLANDC__ |
| 211 | extern int chmod(const char *, int); |
| 212 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 213 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 214 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 215 | /*#ifdef HAVE_FCHMOD |
| 216 | extern int fchmod(int, mode_t); |
| 217 | #endif*/ |
| 218 | /*#ifdef HAVE_LCHMOD |
| 219 | extern int lchmod(const char *, mode_t); |
| 220 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 221 | extern int chown(const char *, uid_t, gid_t); |
| 222 | extern char *getcwd(char *, int); |
| 223 | extern char *strerror(int); |
| 224 | extern int link(const char *, const char *); |
| 225 | extern int rename(const char *, const char *); |
| 226 | extern int stat(const char *, struct stat *); |
| 227 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 229 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 230 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 232 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 233 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 236 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | #ifdef HAVE_UTIME_H |
| 239 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 240 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 242 | #ifdef HAVE_SYS_UTIME_H |
| 243 | #include <sys/utime.h> |
| 244 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 245 | #endif /* HAVE_SYS_UTIME_H */ |
| 246 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | #ifdef HAVE_SYS_TIMES_H |
| 248 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 249 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | |
| 251 | #ifdef HAVE_SYS_PARAM_H |
| 252 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 253 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
| 255 | #ifdef HAVE_SYS_UTSNAME_H |
| 256 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 257 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 259 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 261 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 262 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 263 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 264 | #include <direct.h> |
| 265 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 266 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 268 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 269 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 270 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 272 | #endif |
| 273 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 275 | #endif |
| 276 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 277 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 278 | #endif |
| 279 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 281 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 282 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 283 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 284 | #endif |
| 285 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 287 | #endif |
| 288 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 289 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 290 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 291 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 292 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 293 | #endif |
| 294 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 295 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 296 | #endif |
| 297 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 298 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 299 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 300 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 301 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 302 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 303 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 304 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 305 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 306 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 307 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 308 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 309 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 310 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 311 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 312 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 313 | #define MAXPATHLEN PATH_MAX |
| 314 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 315 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 316 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 317 | #endif /* MAXPATHLEN */ |
| 318 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 319 | #ifdef UNION_WAIT |
| 320 | /* Emulate some macros on systems that have a union instead of macros */ |
| 321 | |
| 322 | #ifndef WIFEXITED |
| 323 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 324 | #endif |
| 325 | |
| 326 | #ifndef WEXITSTATUS |
| 327 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 328 | #endif |
| 329 | |
| 330 | #ifndef WTERMSIG |
| 331 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 332 | #endif |
| 333 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 334 | #define WAIT_TYPE union wait |
| 335 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 336 | |
| 337 | #else /* !UNION_WAIT */ |
| 338 | #define WAIT_TYPE int |
| 339 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 340 | #endif /* UNION_WAIT */ |
| 341 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 342 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 343 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 344 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 345 | #define USE_CTERMID_R |
| 346 | #endif |
| 347 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 348 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 349 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 350 | #undef FSTAT |
| 351 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 352 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 353 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 354 | # define LSTAT win32_lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 355 | # define FSTAT win32_fstat |
| 356 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 357 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 358 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 359 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 360 | # define FSTAT fstat |
| 361 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 362 | #endif |
| 363 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 364 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 365 | #include <sys/mkdev.h> |
| 366 | #else |
| 367 | #if defined(MAJOR_IN_SYSMACROS) |
| 368 | #include <sys/sysmacros.h> |
| 369 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 370 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 371 | #include <sys/mkdev.h> |
| 372 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 373 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 374 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 375 | |
| 376 | #ifdef MS_WINDOWS |
| 377 | static int |
| 378 | win32_warn_bytes_api() |
| 379 | { |
| 380 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 381 | "The Windows bytes API has been deprecated, " |
| 382 | "use Unicode filenames instead", |
| 383 | 1); |
| 384 | } |
| 385 | #endif |
| 386 | |
| 387 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 388 | #ifndef MS_WINDOWS |
| 389 | PyObject * |
| 390 | _PyLong_FromUid(uid_t uid) |
| 391 | { |
| 392 | if (uid == (uid_t)-1) |
| 393 | return PyLong_FromLong(-1); |
| 394 | return PyLong_FromUnsignedLong(uid); |
| 395 | } |
| 396 | |
| 397 | PyObject * |
| 398 | _PyLong_FromGid(gid_t gid) |
| 399 | { |
| 400 | if (gid == (gid_t)-1) |
| 401 | return PyLong_FromLong(-1); |
| 402 | return PyLong_FromUnsignedLong(gid); |
| 403 | } |
| 404 | |
| 405 | int |
| 406 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 407 | { |
| 408 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 409 | long result; |
| 410 | if (PyFloat_Check(obj)) { |
| 411 | PyErr_SetString(PyExc_TypeError, |
| 412 | "integer argument expected, got float"); |
| 413 | return 0; |
| 414 | } |
| 415 | result = PyLong_AsLongAndOverflow(obj, &overflow); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 416 | if (overflow < 0) |
| 417 | goto OverflowDown; |
| 418 | if (!overflow && result == -1) { |
| 419 | /* error or -1 */ |
| 420 | if (PyErr_Occurred()) |
| 421 | return 0; |
| 422 | *(uid_t *)p = (uid_t)-1; |
| 423 | } |
| 424 | else { |
| 425 | /* unsigned uid_t */ |
| 426 | unsigned long uresult; |
| 427 | if (overflow > 0) { |
| 428 | uresult = PyLong_AsUnsignedLong(obj); |
| 429 | if (PyErr_Occurred()) { |
| 430 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 431 | goto OverflowUp; |
| 432 | return 0; |
| 433 | } |
| 434 | if ((uid_t)uresult == (uid_t)-1) |
| 435 | goto OverflowUp; |
| 436 | } else { |
| 437 | if (result < 0) |
| 438 | goto OverflowDown; |
| 439 | uresult = result; |
| 440 | } |
| 441 | if (sizeof(uid_t) < sizeof(long) && |
| 442 | (unsigned long)(uid_t)uresult != uresult) |
| 443 | goto OverflowUp; |
| 444 | *(uid_t *)p = (uid_t)uresult; |
| 445 | } |
| 446 | return 1; |
| 447 | |
| 448 | OverflowDown: |
| 449 | PyErr_SetString(PyExc_OverflowError, |
| 450 | "user id is less than minimum"); |
| 451 | return 0; |
| 452 | |
| 453 | OverflowUp: |
| 454 | PyErr_SetString(PyExc_OverflowError, |
| 455 | "user id is greater than maximum"); |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | int |
| 460 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 461 | { |
| 462 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 463 | long result; |
| 464 | if (PyFloat_Check(obj)) { |
| 465 | PyErr_SetString(PyExc_TypeError, |
| 466 | "integer argument expected, got float"); |
| 467 | return 0; |
| 468 | } |
| 469 | result = PyLong_AsLongAndOverflow(obj, &overflow); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 470 | if (overflow < 0) |
| 471 | goto OverflowDown; |
| 472 | if (!overflow && result == -1) { |
| 473 | /* error or -1 */ |
| 474 | if (PyErr_Occurred()) |
| 475 | return 0; |
| 476 | *(gid_t *)p = (gid_t)-1; |
| 477 | } |
| 478 | else { |
| 479 | /* unsigned gid_t */ |
| 480 | unsigned long uresult; |
| 481 | if (overflow > 0) { |
| 482 | uresult = PyLong_AsUnsignedLong(obj); |
| 483 | if (PyErr_Occurred()) { |
| 484 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 485 | goto OverflowUp; |
| 486 | return 0; |
| 487 | } |
| 488 | if ((gid_t)uresult == (gid_t)-1) |
| 489 | goto OverflowUp; |
| 490 | } else { |
| 491 | if (result < 0) |
| 492 | goto OverflowDown; |
| 493 | uresult = result; |
| 494 | } |
| 495 | if (sizeof(gid_t) < sizeof(long) && |
| 496 | (unsigned long)(gid_t)uresult != uresult) |
| 497 | goto OverflowUp; |
| 498 | *(gid_t *)p = (gid_t)uresult; |
| 499 | } |
| 500 | return 1; |
| 501 | |
| 502 | OverflowDown: |
| 503 | PyErr_SetString(PyExc_OverflowError, |
| 504 | "group id is less than minimum"); |
| 505 | return 0; |
| 506 | |
| 507 | OverflowUp: |
| 508 | PyErr_SetString(PyExc_OverflowError, |
| 509 | "group id is greater than maximum"); |
| 510 | return 0; |
| 511 | } |
| 512 | #endif /* MS_WINDOWS */ |
| 513 | |
| 514 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 515 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 516 | /* |
| 517 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 518 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 519 | * which doesn't play nicely with all the initializer lines in this file that |
| 520 | * look like this: |
| 521 | * int dir_fd = DEFAULT_DIR_FD; |
| 522 | */ |
| 523 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 524 | #else |
| 525 | #define DEFAULT_DIR_FD (-100) |
| 526 | #endif |
| 527 | |
| 528 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 529 | _fd_converter(PyObject *o, int *p, const char *allowed) |
| 530 | { |
| 531 | int overflow; |
| 532 | long long_value = PyLong_AsLongAndOverflow(o, &overflow); |
| 533 | if (PyFloat_Check(o) || |
| 534 | (long_value == -1 && !overflow && PyErr_Occurred())) { |
| 535 | PyErr_Clear(); |
| 536 | PyErr_Format(PyExc_TypeError, |
| 537 | "argument should be %s, not %.200s", |
| 538 | allowed, Py_TYPE(o)->tp_name); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 539 | return 0; |
| 540 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 541 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 542 | PyErr_SetString(PyExc_OverflowError, |
| 543 | "signed integer is greater than maximum"); |
| 544 | return 0; |
| 545 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 546 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 547 | PyErr_SetString(PyExc_OverflowError, |
| 548 | "signed integer is less than minimum"); |
| 549 | return 0; |
| 550 | } |
| 551 | *p = (int)long_value; |
| 552 | return 1; |
| 553 | } |
| 554 | |
| 555 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 556 | dir_fd_converter(PyObject *o, void *p) |
| 557 | { |
| 558 | if (o == Py_None) { |
| 559 | *(int *)p = DEFAULT_DIR_FD; |
| 560 | return 1; |
| 561 | } |
| 562 | return _fd_converter(o, (int *)p, "integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
| 566 | |
| 567 | /* |
| 568 | * A PyArg_ParseTuple "converter" function |
| 569 | * that handles filesystem paths in the manner |
| 570 | * preferred by the os module. |
| 571 | * |
| 572 | * path_converter accepts (Unicode) strings and their |
| 573 | * subclasses, and bytes and their subclasses. What |
| 574 | * it does with the argument depends on the platform: |
| 575 | * |
| 576 | * * On Windows, if we get a (Unicode) string we |
| 577 | * extract the wchar_t * and return it; if we get |
| 578 | * bytes we extract the char * and return that. |
| 579 | * |
| 580 | * * On all other platforms, strings are encoded |
| 581 | * to bytes using PyUnicode_FSConverter, then we |
| 582 | * extract the char * from the bytes object and |
| 583 | * return that. |
| 584 | * |
| 585 | * path_converter also optionally accepts signed |
| 586 | * integers (representing open file descriptors) instead |
| 587 | * of path strings. |
| 588 | * |
| 589 | * Input fields: |
| 590 | * path.nullable |
| 591 | * If nonzero, the path is permitted to be None. |
| 592 | * path.allow_fd |
| 593 | * If nonzero, the path is permitted to be a file handle |
| 594 | * (a signed int) instead of a string. |
| 595 | * path.function_name |
| 596 | * If non-NULL, path_converter will use that as the name |
| 597 | * of the function in error messages. |
| 598 | * (If path.argument_name is NULL it omits the function name.) |
| 599 | * path.argument_name |
| 600 | * If non-NULL, path_converter will use that as the name |
| 601 | * of the parameter in error messages. |
| 602 | * (If path.argument_name is NULL it uses "path".) |
| 603 | * |
| 604 | * Output fields: |
| 605 | * path.wide |
| 606 | * Points to the path if it was expressed as Unicode |
| 607 | * and was not encoded. (Only used on Windows.) |
| 608 | * path.narrow |
| 609 | * Points to the path if it was expressed as bytes, |
| 610 | * or it was Unicode and was encoded to bytes. |
| 611 | * path.fd |
| 612 | * Contains a file descriptor if path.accept_fd was true |
| 613 | * and the caller provided a signed integer instead of any |
| 614 | * sort of string. |
| 615 | * |
| 616 | * WARNING: if your "path" parameter is optional, and is |
| 617 | * unspecified, path_converter will never get called. |
| 618 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 619 | * yourself! |
| 620 | * path.length |
| 621 | * The length of the path in characters, if specified as |
| 622 | * a string. |
| 623 | * path.object |
| 624 | * The original object passed in. |
| 625 | * path.cleanup |
| 626 | * For internal use only. May point to a temporary object. |
| 627 | * (Pay no attention to the man behind the curtain.) |
| 628 | * |
| 629 | * At most one of path.wide or path.narrow will be non-NULL. |
| 630 | * If path was None and path.nullable was set, |
| 631 | * or if path was an integer and path.allow_fd was set, |
| 632 | * both path.wide and path.narrow will be NULL |
| 633 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 634 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 635 | * path_converter takes care to not write to the path_t |
| 636 | * unless it's successful. However it must reset the |
| 637 | * "cleanup" field each time it's called. |
| 638 | * |
| 639 | * Use as follows: |
| 640 | * path_t path; |
| 641 | * memset(&path, 0, sizeof(path)); |
| 642 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 643 | * // ... use values from path ... |
| 644 | * path_cleanup(&path); |
| 645 | * |
| 646 | * (Note that if PyArg_Parse fails you don't need to call |
| 647 | * path_cleanup(). However it is safe to do so.) |
| 648 | */ |
| 649 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 650 | const char *function_name; |
| 651 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 652 | int nullable; |
| 653 | int allow_fd; |
| 654 | wchar_t *wide; |
| 655 | char *narrow; |
| 656 | int fd; |
| 657 | Py_ssize_t length; |
| 658 | PyObject *object; |
| 659 | PyObject *cleanup; |
| 660 | } path_t; |
| 661 | |
| 662 | static void |
| 663 | path_cleanup(path_t *path) { |
| 664 | if (path->cleanup) { |
| 665 | Py_DECREF(path->cleanup); |
| 666 | path->cleanup = NULL; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | static int |
| 671 | path_converter(PyObject *o, void *p) { |
| 672 | path_t *path = (path_t *)p; |
| 673 | PyObject *unicode, *bytes; |
| 674 | Py_ssize_t length; |
| 675 | char *narrow; |
| 676 | |
| 677 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 678 | PyErr_Format(exc, "%s%s" fmt, \ |
| 679 | path->function_name ? path->function_name : "", \ |
| 680 | path->function_name ? ": " : "", \ |
| 681 | path->argument_name ? path->argument_name : "path") |
| 682 | |
| 683 | /* Py_CLEANUP_SUPPORTED support */ |
| 684 | if (o == NULL) { |
| 685 | path_cleanup(path); |
| 686 | return 1; |
| 687 | } |
| 688 | |
| 689 | /* ensure it's always safe to call path_cleanup() */ |
| 690 | path->cleanup = NULL; |
| 691 | |
| 692 | if (o == Py_None) { |
| 693 | if (!path->nullable) { |
| 694 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 695 | "can't specify None for %s argument"); |
| 696 | return 0; |
| 697 | } |
| 698 | path->wide = NULL; |
| 699 | path->narrow = NULL; |
| 700 | path->length = 0; |
| 701 | path->object = o; |
| 702 | path->fd = -1; |
| 703 | return 1; |
| 704 | } |
| 705 | |
| 706 | unicode = PyUnicode_FromObject(o); |
| 707 | if (unicode) { |
| 708 | #ifdef MS_WINDOWS |
| 709 | wchar_t *wide; |
| 710 | length = PyUnicode_GET_SIZE(unicode); |
| 711 | if (length > 32767) { |
| 712 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 713 | Py_DECREF(unicode); |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | wide = PyUnicode_AsUnicode(unicode); |
| 718 | if (!wide) { |
| 719 | Py_DECREF(unicode); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | path->wide = wide; |
| 724 | path->narrow = NULL; |
| 725 | path->length = length; |
| 726 | path->object = o; |
| 727 | path->fd = -1; |
| 728 | path->cleanup = unicode; |
| 729 | return Py_CLEANUP_SUPPORTED; |
| 730 | #else |
| 731 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 732 | Py_DECREF(unicode); |
| 733 | if (!converted) |
| 734 | bytes = NULL; |
| 735 | #endif |
| 736 | } |
| 737 | else { |
| 738 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 739 | if (PyObject_CheckBuffer(o)) |
| 740 | bytes = PyBytes_FromObject(o); |
| 741 | else |
| 742 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 743 | if (!bytes) { |
| 744 | PyErr_Clear(); |
| 745 | if (path->allow_fd) { |
| 746 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 747 | int result = _fd_converter(o, &fd, |
| 748 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 749 | if (result) { |
| 750 | path->wide = NULL; |
| 751 | path->narrow = NULL; |
| 752 | path->length = 0; |
| 753 | path->object = o; |
| 754 | path->fd = fd; |
| 755 | return result; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if (!bytes) { |
| 762 | if (!PyErr_Occurred()) |
| 763 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | #ifdef MS_WINDOWS |
| 768 | if (win32_warn_bytes_api()) { |
| 769 | Py_DECREF(bytes); |
| 770 | return 0; |
| 771 | } |
| 772 | #endif |
| 773 | |
| 774 | length = PyBytes_GET_SIZE(bytes); |
| 775 | #ifdef MS_WINDOWS |
| 776 | if (length > MAX_PATH) { |
| 777 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 778 | Py_DECREF(bytes); |
| 779 | return 0; |
| 780 | } |
| 781 | #endif |
| 782 | |
| 783 | narrow = PyBytes_AS_STRING(bytes); |
| 784 | if (length != strlen(narrow)) { |
| 785 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); |
| 786 | Py_DECREF(bytes); |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | path->wide = NULL; |
| 791 | path->narrow = narrow; |
| 792 | path->length = length; |
| 793 | path->object = o; |
| 794 | path->fd = -1; |
| 795 | path->cleanup = bytes; |
| 796 | return Py_CLEANUP_SUPPORTED; |
| 797 | } |
| 798 | |
| 799 | static void |
| 800 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 801 | PyErr_Format(PyExc_NotImplementedError, |
| 802 | "%s%s%s unavailable on this platform", |
| 803 | (function_name != NULL) ? function_name : "", |
| 804 | (function_name != NULL) ? ": ": "", |
| 805 | argument_name); |
| 806 | } |
| 807 | |
| 808 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 809 | dir_fd_unavailable(PyObject *o, void *p) |
| 810 | { |
| 811 | int dir_fd; |
| 812 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 813 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 814 | if (dir_fd != DEFAULT_DIR_FD) { |
| 815 | argument_unavailable_error(NULL, "dir_fd"); |
| 816 | return 0; |
| 817 | } |
| 818 | *(int *)p = dir_fd; |
| 819 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static int |
| 823 | fd_specified(char *function_name, int fd) { |
| 824 | if (fd == -1) |
| 825 | return 0; |
| 826 | |
| 827 | argument_unavailable_error(function_name, "fd"); |
| 828 | return 1; |
| 829 | } |
| 830 | |
| 831 | static int |
| 832 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 833 | if (follow_symlinks) |
| 834 | return 0; |
| 835 | |
| 836 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 837 | return 1; |
| 838 | } |
| 839 | |
| 840 | static int |
| 841 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 842 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 843 | PyErr_Format(PyExc_ValueError, |
| 844 | "%s: can't specify dir_fd without matching path", |
| 845 | function_name); |
| 846 | return 1; |
| 847 | } |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static int |
| 852 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 853 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 854 | PyErr_Format(PyExc_ValueError, |
| 855 | "%s: can't specify both dir_fd and fd", |
| 856 | function_name); |
| 857 | return 1; |
| 858 | } |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static int |
| 863 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 864 | int follow_symlinks) { |
| 865 | if ((fd > 0) && (!follow_symlinks)) { |
| 866 | PyErr_Format(PyExc_ValueError, |
| 867 | "%s: cannot use fd and follow_symlinks together", |
| 868 | function_name); |
| 869 | return 1; |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int |
| 875 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 876 | int follow_symlinks) { |
| 877 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 878 | PyErr_Format(PyExc_ValueError, |
| 879 | "%s: cannot use dir_fd and follow_symlinks together", |
| 880 | function_name); |
| 881 | return 1; |
| 882 | } |
| 883 | return 0; |
| 884 | } |
| 885 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 886 | /* A helper used by a number of POSIX-only functions */ |
| 887 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 888 | static int |
| 889 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 890 | { |
| 891 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 892 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 893 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 894 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 895 | #endif |
| 896 | if (PyErr_Occurred()) |
| 897 | return 0; |
| 898 | return 1; |
| 899 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 900 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 901 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 902 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 903 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 904 | * valid and raise an assertion if it isn't. |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 905 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 906 | * an assertion can be useful, but it does contradict the POSIX standard |
| 907 | * which for write(2) states: |
| 908 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 909 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 910 | * writing." |
| 911 | * Furthermore, python allows the user to enter any old integer |
| 912 | * as a fd and should merely raise a python exception on error. |
| 913 | * The Microsoft CRT doesn't provide an official way to check for the |
| 914 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 915 | * 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] | 916 | * internal structures involved. |
| 917 | * The structures below must be updated for each version of visual studio |
| 918 | * according to the file internal.h in the CRT source, until MS comes |
| 919 | * up with a less hacky way to do this. |
| 920 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 921 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 922 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 923 | /* The actual size of the structure is determined at runtime. |
| 924 | * Only the first items must be present. |
| 925 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 926 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 927 | intptr_t osfhnd; |
| 928 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 929 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 930 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 931 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 932 | #define IOINFO_L2E 5 |
| 933 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 934 | #define IOINFO_ARRAYS 64 |
| 935 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 936 | #define FOPEN 0x01 |
| 937 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 938 | |
| 939 | /* This function emulates what the windows CRT does to validate file handles */ |
| 940 | int |
| 941 | _PyVerify_fd(int fd) |
| 942 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 943 | const int i1 = fd >> IOINFO_L2E; |
| 944 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 945 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 946 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 948 | /* Determine the actual size of the ioinfo structure, |
| 949 | * as used by the CRT loaded in memory |
| 950 | */ |
| 951 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 952 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 953 | } |
| 954 | if (sizeof_ioinfo == 0) { |
| 955 | /* This should not happen... */ |
| 956 | goto fail; |
| 957 | } |
| 958 | |
| 959 | /* See that it isn't a special CLEAR fileno */ |
| 960 | if (fd != _NO_CONSOLE_FILENO) { |
| 961 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 962 | * we check pointer validity and other info |
| 963 | */ |
| 964 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 965 | /* finally, check that the file is open */ |
| 966 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 967 | if (info->osfile & FOPEN) { |
| 968 | return 1; |
| 969 | } |
| 970 | } |
| 971 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 972 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 973 | errno = EBADF; |
| 974 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 978 | static int |
| 979 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 980 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 981 | if (!_PyVerify_fd(fd1)) |
| 982 | return 0; |
| 983 | if (fd2 == _NO_CONSOLE_FILENO) |
| 984 | return 0; |
| 985 | if ((unsigned)fd2 < _NHANDLE_) |
| 986 | return 1; |
| 987 | else |
| 988 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 989 | } |
| 990 | #else |
| 991 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 992 | #define _PyVerify_fd_dup2(A, B) (1) |
| 993 | #endif |
| 994 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 995 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 996 | /* The following structure was copied from |
| 997 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 998 | include doesn't seem to be present in the Windows SDK (at least as included |
| 999 | with Visual Studio Express). */ |
| 1000 | typedef struct _REPARSE_DATA_BUFFER { |
| 1001 | ULONG ReparseTag; |
| 1002 | USHORT ReparseDataLength; |
| 1003 | USHORT Reserved; |
| 1004 | union { |
| 1005 | struct { |
| 1006 | USHORT SubstituteNameOffset; |
| 1007 | USHORT SubstituteNameLength; |
| 1008 | USHORT PrintNameOffset; |
| 1009 | USHORT PrintNameLength; |
| 1010 | ULONG Flags; |
| 1011 | WCHAR PathBuffer[1]; |
| 1012 | } SymbolicLinkReparseBuffer; |
| 1013 | |
| 1014 | struct { |
| 1015 | USHORT SubstituteNameOffset; |
| 1016 | USHORT SubstituteNameLength; |
| 1017 | USHORT PrintNameOffset; |
| 1018 | USHORT PrintNameLength; |
| 1019 | WCHAR PathBuffer[1]; |
| 1020 | } MountPointReparseBuffer; |
| 1021 | |
| 1022 | struct { |
| 1023 | UCHAR DataBuffer[1]; |
| 1024 | } GenericReparseBuffer; |
| 1025 | }; |
| 1026 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 1027 | |
| 1028 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 1029 | GenericReparseBuffer) |
| 1030 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 1031 | |
| 1032 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1033 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1034 | { |
| 1035 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1036 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 1037 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1038 | |
| 1039 | if (0 == DeviceIoControl( |
| 1040 | reparse_point_handle, |
| 1041 | FSCTL_GET_REPARSE_POINT, |
| 1042 | NULL, 0, /* in buffer */ |
| 1043 | target_buffer, sizeof(target_buffer), |
| 1044 | &n_bytes_returned, |
| 1045 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1046 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (reparse_tag) |
| 1049 | *reparse_tag = rdb->ReparseTag; |
| 1050 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1051 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1052 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1053 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1054 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1055 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1056 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1057 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1058 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1059 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1060 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1061 | */ |
| 1062 | #include <crt_externs.h> |
| 1063 | static char **environ; |
| 1064 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1065 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1066 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1067 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1068 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1069 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1070 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1071 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1072 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1073 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1074 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1075 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1076 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1077 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1078 | d = PyDict_New(); |
| 1079 | if (d == NULL) |
| 1080 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1081 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1082 | if (environ == NULL) |
| 1083 | environ = *_NSGetEnviron(); |
| 1084 | #endif |
| 1085 | #ifdef MS_WINDOWS |
| 1086 | /* _wenviron must be initialized in this way if the program is started |
| 1087 | through main() instead of wmain(). */ |
| 1088 | _wgetenv(L""); |
| 1089 | if (_wenviron == NULL) |
| 1090 | return d; |
| 1091 | /* This part ignores errors */ |
| 1092 | for (e = _wenviron; *e != NULL; e++) { |
| 1093 | PyObject *k; |
| 1094 | PyObject *v; |
| 1095 | wchar_t *p = wcschr(*e, L'='); |
| 1096 | if (p == NULL) |
| 1097 | continue; |
| 1098 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1099 | if (k == NULL) { |
| 1100 | PyErr_Clear(); |
| 1101 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1102 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1103 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1104 | if (v == NULL) { |
| 1105 | PyErr_Clear(); |
| 1106 | Py_DECREF(k); |
| 1107 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1108 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1109 | if (PyDict_GetItem(d, k) == NULL) { |
| 1110 | if (PyDict_SetItem(d, k, v) != 0) |
| 1111 | PyErr_Clear(); |
| 1112 | } |
| 1113 | Py_DECREF(k); |
| 1114 | Py_DECREF(v); |
| 1115 | } |
| 1116 | #else |
| 1117 | if (environ == NULL) |
| 1118 | return d; |
| 1119 | /* This part ignores errors */ |
| 1120 | for (e = environ; *e != NULL; e++) { |
| 1121 | PyObject *k; |
| 1122 | PyObject *v; |
| 1123 | char *p = strchr(*e, '='); |
| 1124 | if (p == NULL) |
| 1125 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1126 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1127 | if (k == NULL) { |
| 1128 | PyErr_Clear(); |
| 1129 | continue; |
| 1130 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1131 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1132 | if (v == NULL) { |
| 1133 | PyErr_Clear(); |
| 1134 | Py_DECREF(k); |
| 1135 | continue; |
| 1136 | } |
| 1137 | if (PyDict_GetItem(d, k) == NULL) { |
| 1138 | if (PyDict_SetItem(d, k, v) != 0) |
| 1139 | PyErr_Clear(); |
| 1140 | } |
| 1141 | Py_DECREF(k); |
| 1142 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1143 | } |
| 1144 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1145 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1148 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1149 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1150 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1151 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1152 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1153 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1154 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1155 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1156 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1157 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1158 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1159 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1160 | /* XXX We should pass the function name along in the future. |
| 1161 | (winreg.c also wants to pass the function name.) |
| 1162 | This would however require an additional param to the |
| 1163 | Windows error object, which is non-trivial. |
| 1164 | */ |
| 1165 | errno = GetLastError(); |
| 1166 | if (filename) |
| 1167 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1168 | else |
| 1169 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1170 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1171 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1172 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1173 | win32_error_object(char* function, PyObject* filename) |
| 1174 | { |
| 1175 | /* XXX - see win32_error for comments on 'function' */ |
| 1176 | errno = GetLastError(); |
| 1177 | if (filename) |
| 1178 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1179 | PyExc_OSError, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1180 | errno, |
| 1181 | filename); |
| 1182 | else |
| 1183 | return PyErr_SetFromWindowsErr(errno); |
| 1184 | } |
| 1185 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1186 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1187 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1188 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1189 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1190 | { |
| 1191 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1192 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1193 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1194 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1195 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1196 | #endif |
| 1197 | } |
| 1198 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1199 | /* POSIX generic methods */ |
| 1200 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1201 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1202 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1203 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1204 | int fd; |
| 1205 | int res; |
| 1206 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1207 | if (fd < 0) |
| 1208 | return NULL; |
| 1209 | if (!_PyVerify_fd(fd)) |
| 1210 | return posix_error(); |
| 1211 | Py_BEGIN_ALLOW_THREADS |
| 1212 | res = (*func)(fd); |
| 1213 | Py_END_ALLOW_THREADS |
| 1214 | if (res < 0) |
| 1215 | return posix_error(); |
| 1216 | Py_INCREF(Py_None); |
| 1217 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1218 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1219 | |
| 1220 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1221 | posix_1str(const char *func_name, PyObject *args, char *format, |
| 1222 | int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1223 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1224 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1225 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1226 | memset(&path, 0, sizeof(path)); |
| 1227 | path.function_name = func_name; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1228 | if (!PyArg_ParseTuple(args, format, |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1229 | path_converter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1230 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1231 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1232 | res = (*func)(path.narrow); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1233 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1234 | if (res < 0) { |
| 1235 | path_error(&path); |
| 1236 | path_cleanup(&path); |
| 1237 | return NULL; |
| 1238 | } |
| 1239 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1240 | Py_INCREF(Py_None); |
| 1241 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1244 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1245 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1246 | /* This is a reimplementation of the C library's chdir function, |
| 1247 | but one that produces Win32 errors instead of DOS error codes. |
| 1248 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1249 | it also needs to set "magic" environment variables indicating |
| 1250 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1251 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1252 | win32_chdir(LPCSTR path) |
| 1253 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1254 | char new_path[MAX_PATH+1]; |
| 1255 | int result; |
| 1256 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1257 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1258 | if(!SetCurrentDirectoryA(path)) |
| 1259 | return FALSE; |
| 1260 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 1261 | if (!result) |
| 1262 | return FALSE; |
| 1263 | /* In the ANSI API, there should not be any paths longer |
| 1264 | than MAX_PATH. */ |
| 1265 | assert(result <= MAX_PATH+1); |
| 1266 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1267 | strncmp(new_path, "//", 2) == 0) |
| 1268 | /* UNC path, nothing to do. */ |
| 1269 | return TRUE; |
| 1270 | env[1] = new_path[0]; |
| 1271 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /* The Unicode version differs from the ANSI version |
| 1275 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1276 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1277 | win32_wchdir(LPCWSTR path) |
| 1278 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1279 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 1280 | int result; |
| 1281 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1282 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1283 | if(!SetCurrentDirectoryW(path)) |
| 1284 | return FALSE; |
| 1285 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 1286 | if (!result) |
| 1287 | return FALSE; |
| 1288 | if (result > MAX_PATH+1) { |
| 1289 | new_path = malloc(result * sizeof(wchar_t)); |
| 1290 | if (!new_path) { |
| 1291 | SetLastError(ERROR_OUTOFMEMORY); |
| 1292 | return FALSE; |
| 1293 | } |
| 1294 | result = GetCurrentDirectoryW(result, new_path); |
| 1295 | if (!result) { |
| 1296 | free(new_path); |
| 1297 | return FALSE; |
| 1298 | } |
| 1299 | } |
| 1300 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1301 | wcsncmp(new_path, L"//", 2) == 0) |
| 1302 | /* UNC path, nothing to do. */ |
| 1303 | return TRUE; |
| 1304 | env[1] = new_path[0]; |
| 1305 | result = SetEnvironmentVariableW(env, new_path); |
| 1306 | if (new_path != _new_path) |
| 1307 | free(new_path); |
| 1308 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1309 | } |
| 1310 | #endif |
| 1311 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1312 | #ifdef MS_WINDOWS |
| 1313 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1314 | - time stamps are restricted to second resolution |
| 1315 | - file modification times suffer from forth-and-back conversions between |
| 1316 | UTC and local time |
| 1317 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1318 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1319 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1320 | |
| 1321 | struct win32_stat{ |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1322 | unsigned long st_dev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1323 | __int64 st_ino; |
| 1324 | unsigned short st_mode; |
| 1325 | int st_nlink; |
| 1326 | int st_uid; |
| 1327 | int st_gid; |
Brian Curtin | 87e63a2 | 2012-12-31 11:59:48 -0600 | [diff] [blame] | 1328 | unsigned long st_rdev; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1329 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1330 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1331 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1332 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1333 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1334 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1335 | int st_ctime_nsec; |
| 1336 | }; |
| 1337 | |
| 1338 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1339 | |
| 1340 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1341 | 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] | 1342 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1343 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1344 | /* Cannot simply cast and dereference in_ptr, |
| 1345 | since it might not be aligned properly */ |
| 1346 | __int64 in; |
| 1347 | memcpy(&in, in_ptr, sizeof(in)); |
| 1348 | *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] | 1349 | *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] | 1350 | } |
| 1351 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1352 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1353 | 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] | 1354 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1355 | /* XXX endianness */ |
| 1356 | __int64 out; |
| 1357 | out = time_in + secs_between_epochs; |
| 1358 | out = out * 10000000 + nsec_in / 100; |
| 1359 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1362 | /* Below, we *know* that ugo+r is 0444 */ |
| 1363 | #if _S_IREAD != 0400 |
| 1364 | #error Unsupported C library |
| 1365 | #endif |
| 1366 | static int |
| 1367 | attributes_to_mode(DWORD attr) |
| 1368 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1369 | int m = 0; |
| 1370 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1371 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1372 | else |
| 1373 | m |= _S_IFREG; |
| 1374 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1375 | m |= 0444; |
| 1376 | else |
| 1377 | m |= 0666; |
| 1378 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1382 | 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] | 1383 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1384 | memset(result, 0, sizeof(*result)); |
| 1385 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1386 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 1387 | result->st_dev = info->dwVolumeSerialNumber; |
| 1388 | result->st_rdev = result->st_dev; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1389 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1390 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1391 | 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] | 1392 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1393 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1394 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1395 | /* first clear the S_IFMT bits */ |
| 1396 | result->st_mode ^= (result->st_mode & 0170000); |
| 1397 | /* now set the bits that make this a symlink */ |
| 1398 | result->st_mode |= 0120000; |
| 1399 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1400 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1401 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1404 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1405 | 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] | 1406 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1407 | HANDLE hFindFile; |
| 1408 | WIN32_FIND_DATAA FileData; |
| 1409 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1410 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1411 | return FALSE; |
| 1412 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1413 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1414 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1415 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1416 | info->ftCreationTime = FileData.ftCreationTime; |
| 1417 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1418 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1419 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1420 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1421 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1422 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1423 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1424 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1428 | 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] | 1429 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1430 | HANDLE hFindFile; |
| 1431 | WIN32_FIND_DATAW FileData; |
| 1432 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1433 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1434 | return FALSE; |
| 1435 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1436 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1437 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1438 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1439 | info->ftCreationTime = FileData.ftCreationTime; |
| 1440 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1441 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1442 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1443 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1444 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1445 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1446 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1447 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1450 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1451 | static int has_GetFinalPathNameByHandle = -1; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1452 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1453 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1454 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1455 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1456 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1457 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1458 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1459 | DWORD); |
| 1460 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1461 | /* only recheck */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1462 | if (-1 == has_GetFinalPathNameByHandle) |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1463 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1464 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1465 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1466 | "GetFinalPathNameByHandleA"); |
| 1467 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1468 | "GetFinalPathNameByHandleW"); |
| 1469 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1470 | Py_GetFinalPathNameByHandleW; |
| 1471 | } |
| 1472 | return has_GetFinalPathNameByHandle; |
| 1473 | } |
| 1474 | |
| 1475 | static BOOL |
| 1476 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1477 | { |
| 1478 | int buf_size, result_length; |
| 1479 | wchar_t *buf; |
| 1480 | |
| 1481 | /* We have a good handle to the target, use it to determine |
| 1482 | the target path name (then we'll call lstat on it). */ |
| 1483 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1484 | VOLUME_NAME_DOS); |
| 1485 | if(!buf_size) |
| 1486 | return FALSE; |
| 1487 | |
| 1488 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1489 | if (!buf) { |
| 1490 | SetLastError(ERROR_OUTOFMEMORY); |
| 1491 | return FALSE; |
| 1492 | } |
| 1493 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1494 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1495 | buf, buf_size, VOLUME_NAME_DOS); |
| 1496 | |
| 1497 | if(!result_length) { |
| 1498 | free(buf); |
| 1499 | return FALSE; |
| 1500 | } |
| 1501 | |
| 1502 | if(!CloseHandle(hdl)) { |
| 1503 | free(buf); |
| 1504 | return FALSE; |
| 1505 | } |
| 1506 | |
| 1507 | buf[result_length] = 0; |
| 1508 | |
| 1509 | *target_path = buf; |
| 1510 | return TRUE; |
| 1511 | } |
| 1512 | |
| 1513 | static int |
| 1514 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1515 | BOOL traverse); |
| 1516 | static int |
| 1517 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1518 | BOOL traverse) |
| 1519 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1520 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1521 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1522 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1523 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1524 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1525 | const char *dot; |
| 1526 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1527 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1528 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1529 | traverse reparse point. */ |
| 1530 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1533 | hFile = CreateFileA( |
| 1534 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1535 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1536 | 0, /* share mode */ |
| 1537 | NULL, /* security attributes */ |
| 1538 | OPEN_EXISTING, |
| 1539 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1540 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1541 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1542 | the symlink path agin and not the actual final path. */ |
| 1543 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1544 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1545 | NULL); |
| 1546 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1547 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1548 | /* Either the target doesn't exist, or we don't have access to |
| 1549 | get a handle to it. If the former, we need to return an error. |
| 1550 | If the latter, we can use attributes_from_dir. */ |
| 1551 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1552 | return -1; |
| 1553 | /* Could not get attributes on open file. Fall back to |
| 1554 | reading the directory. */ |
| 1555 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1556 | /* Very strange. This should not fail now */ |
| 1557 | return -1; |
| 1558 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1559 | if (traverse) { |
| 1560 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1561 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1562 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1563 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1564 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1565 | } else { |
| 1566 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1567 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1568 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1569 | } |
| 1570 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1571 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1572 | return -1; |
| 1573 | |
| 1574 | /* Close the outer open file handle now that we're about to |
| 1575 | reopen it with different flags. */ |
| 1576 | if (!CloseHandle(hFile)) |
| 1577 | return -1; |
| 1578 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1579 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1580 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1581 | the file without the reparse handling flag set. */ |
| 1582 | hFile2 = CreateFileA( |
| 1583 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1584 | NULL, OPEN_EXISTING, |
| 1585 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1586 | NULL); |
| 1587 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1588 | return -1; |
| 1589 | |
| 1590 | if (!get_target_path(hFile2, &target_path)) |
| 1591 | return -1; |
| 1592 | |
| 1593 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1594 | free(target_path); |
| 1595 | return code; |
| 1596 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1597 | } else |
| 1598 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1599 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1600 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1601 | |
| 1602 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1603 | dot = strrchr(path, '.'); |
| 1604 | if (dot) { |
| 1605 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1606 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1607 | result->st_mode |= 0111; |
| 1608 | } |
| 1609 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1613 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1614 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1615 | { |
| 1616 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1617 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1618 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1619 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1620 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1621 | const wchar_t *dot; |
| 1622 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1623 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1624 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1625 | traverse reparse point. */ |
| 1626 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1629 | hFile = CreateFileW( |
| 1630 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1631 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1632 | 0, /* share mode */ |
| 1633 | NULL, /* security attributes */ |
| 1634 | OPEN_EXISTING, |
| 1635 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1636 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1637 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1638 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1639 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1640 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1641 | NULL); |
| 1642 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1643 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1644 | /* Either the target doesn't exist, or we don't have access to |
| 1645 | get a handle to it. If the former, we need to return an error. |
| 1646 | If the latter, we can use attributes_from_dir. */ |
| 1647 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1648 | return -1; |
| 1649 | /* Could not get attributes on open file. Fall back to |
| 1650 | reading the directory. */ |
| 1651 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1652 | /* Very strange. This should not fail now */ |
| 1653 | return -1; |
| 1654 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1655 | if (traverse) { |
| 1656 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1657 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1658 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1659 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1660 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1661 | } else { |
| 1662 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1663 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1664 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1665 | } |
| 1666 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1667 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1668 | return -1; |
| 1669 | |
| 1670 | /* Close the outer open file handle now that we're about to |
| 1671 | reopen it with different flags. */ |
| 1672 | if (!CloseHandle(hFile)) |
| 1673 | return -1; |
| 1674 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1675 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1676 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1677 | the file without the reparse handling flag set. */ |
| 1678 | hFile2 = CreateFileW( |
| 1679 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1680 | NULL, OPEN_EXISTING, |
| 1681 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1682 | NULL); |
| 1683 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1684 | return -1; |
| 1685 | |
| 1686 | if (!get_target_path(hFile2, &target_path)) |
| 1687 | return -1; |
| 1688 | |
| 1689 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1690 | free(target_path); |
| 1691 | return code; |
| 1692 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1693 | } else |
| 1694 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1695 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1696 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1697 | |
| 1698 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1699 | dot = wcsrchr(path, '.'); |
| 1700 | if (dot) { |
| 1701 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1702 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1703 | result->st_mode |= 0111; |
| 1704 | } |
| 1705 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1709 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1710 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1711 | /* Protocol violation: we explicitly clear errno, instead of |
| 1712 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1713 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1714 | errno = 0; |
| 1715 | return code; |
| 1716 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1717 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1718 | static int |
| 1719 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1720 | { |
| 1721 | /* Protocol violation: we explicitly clear errno, instead of |
| 1722 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1723 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1724 | errno = 0; |
| 1725 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1726 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1727 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1728 | |
| 1729 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1730 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1731 | default does not traverse symlinks and instead returns attributes for |
| 1732 | the symlink. |
| 1733 | |
| 1734 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1735 | win32_stat will first explicitly resolve the symlink target and then will |
| 1736 | call win32_lstat on that result. |
| 1737 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1738 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1739 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1740 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1741 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1742 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1743 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1746 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1747 | 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] | 1748 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1749 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | static int |
| 1753 | win32_stat(const char* path, struct win32_stat *result) |
| 1754 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1755 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1758 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1759 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1760 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1761 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | static int |
| 1765 | win32_fstat(int file_number, struct win32_stat *result) |
| 1766 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1767 | BY_HANDLE_FILE_INFORMATION info; |
| 1768 | HANDLE h; |
| 1769 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1770 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 1771 | if (!_PyVerify_fd(file_number)) |
| 1772 | h = INVALID_HANDLE_VALUE; |
| 1773 | else |
| 1774 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1775 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1776 | /* Protocol violation: we explicitly clear errno, instead of |
| 1777 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1778 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1779 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1780 | if (h == INVALID_HANDLE_VALUE) { |
| 1781 | /* This is really a C library error (invalid file handle). |
| 1782 | We set the Win32 error to the closes one matching. */ |
| 1783 | SetLastError(ERROR_INVALID_HANDLE); |
| 1784 | return -1; |
| 1785 | } |
| 1786 | memset(result, 0, sizeof(*result)); |
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 | type = GetFileType(h); |
| 1789 | if (type == FILE_TYPE_UNKNOWN) { |
| 1790 | DWORD error = GetLastError(); |
| 1791 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1792 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1793 | } |
| 1794 | /* else: valid but unknown file */ |
| 1795 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1796 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1797 | if (type != FILE_TYPE_DISK) { |
| 1798 | if (type == FILE_TYPE_CHAR) |
| 1799 | result->st_mode = _S_IFCHR; |
| 1800 | else if (type == FILE_TYPE_PIPE) |
| 1801 | result->st_mode = _S_IFIFO; |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | if (!GetFileInformationByHandle(h, &info)) { |
| 1806 | return -1; |
| 1807 | } |
| 1808 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1809 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1810 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1811 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1812 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | #endif /* MS_WINDOWS */ |
| 1816 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1817 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1818 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1819 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1820 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1821 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1822 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1823 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1824 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1825 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1826 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1827 | |
| 1828 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1829 | {"st_mode", "protection bits"}, |
| 1830 | {"st_ino", "inode"}, |
| 1831 | {"st_dev", "device"}, |
| 1832 | {"st_nlink", "number of hard links"}, |
| 1833 | {"st_uid", "user ID of owner"}, |
| 1834 | {"st_gid", "group ID of owner"}, |
| 1835 | {"st_size", "total size, in bytes"}, |
| 1836 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1837 | {NULL, "integer time of last access"}, |
| 1838 | {NULL, "integer time of last modification"}, |
| 1839 | {NULL, "integer time of last change"}, |
| 1840 | {"st_atime", "time of last access"}, |
| 1841 | {"st_mtime", "time of last modification"}, |
| 1842 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1843 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1844 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1845 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1846 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1847 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1848 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1849 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1850 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1851 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1852 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1853 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1854 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1855 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1856 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1857 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1858 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1859 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1860 | #endif |
| 1861 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1862 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1863 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1864 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1865 | }; |
| 1866 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1867 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1868 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1869 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1870 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1871 | #endif |
| 1872 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1873 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1874 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1875 | #else |
| 1876 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1877 | #endif |
| 1878 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1879 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1880 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1881 | #else |
| 1882 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1883 | #endif |
| 1884 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1885 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1886 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1887 | #else |
| 1888 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1889 | #endif |
| 1890 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1891 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1892 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1893 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1894 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1895 | #endif |
| 1896 | |
| 1897 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1898 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1899 | #else |
| 1900 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1901 | #endif |
| 1902 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1903 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1904 | "stat_result", /* name */ |
| 1905 | stat_result__doc__, /* doc */ |
| 1906 | stat_result_fields, |
| 1907 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1908 | }; |
| 1909 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1910 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1911 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1912 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1913 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1914 | 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] | 1915 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1916 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1917 | |
| 1918 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1919 | {"f_bsize", }, |
| 1920 | {"f_frsize", }, |
| 1921 | {"f_blocks", }, |
| 1922 | {"f_bfree", }, |
| 1923 | {"f_bavail", }, |
| 1924 | {"f_files", }, |
| 1925 | {"f_ffree", }, |
| 1926 | {"f_favail", }, |
| 1927 | {"f_flag", }, |
| 1928 | {"f_namemax",}, |
| 1929 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1930 | }; |
| 1931 | |
| 1932 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1933 | "statvfs_result", /* name */ |
| 1934 | statvfs_result__doc__, /* doc */ |
| 1935 | statvfs_result_fields, |
| 1936 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1937 | }; |
| 1938 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1939 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1940 | PyDoc_STRVAR(waitid_result__doc__, |
| 1941 | "waitid_result: Result from waitid.\n\n\ |
| 1942 | This object may be accessed either as a tuple of\n\ |
| 1943 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1944 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1945 | \n\ |
| 1946 | See os.waitid for more information."); |
| 1947 | |
| 1948 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1949 | {"si_pid", }, |
| 1950 | {"si_uid", }, |
| 1951 | {"si_signo", }, |
| 1952 | {"si_status", }, |
| 1953 | {"si_code", }, |
| 1954 | {0} |
| 1955 | }; |
| 1956 | |
| 1957 | static PyStructSequence_Desc waitid_result_desc = { |
| 1958 | "waitid_result", /* name */ |
| 1959 | waitid_result__doc__, /* doc */ |
| 1960 | waitid_result_fields, |
| 1961 | 5 |
| 1962 | }; |
| 1963 | static PyTypeObject WaitidResultType; |
| 1964 | #endif |
| 1965 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1966 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1967 | static PyTypeObject StatResultType; |
| 1968 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1969 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1970 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1971 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1972 | static newfunc structseq_new; |
| 1973 | |
| 1974 | static PyObject * |
| 1975 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1976 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1977 | PyStructSequence *result; |
| 1978 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1979 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1980 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1981 | if (!result) |
| 1982 | return NULL; |
| 1983 | /* If we have been initialized from a tuple, |
| 1984 | st_?time might be set to None. Initialize it |
| 1985 | from the int slots. */ |
| 1986 | for (i = 7; i <= 9; i++) { |
| 1987 | if (result->ob_item[i+3] == Py_None) { |
| 1988 | Py_DECREF(Py_None); |
| 1989 | Py_INCREF(result->ob_item[i]); |
| 1990 | result->ob_item[i+3] = result->ob_item[i]; |
| 1991 | } |
| 1992 | } |
| 1993 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | |
| 1997 | |
| 1998 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1999 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2000 | |
| 2001 | PyDoc_STRVAR(stat_float_times__doc__, |
| 2002 | "stat_float_times([newval]) -> oldval\n\n\ |
| 2003 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 2004 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 2005 | future calls return ints. \n\ |
| 2006 | If newval is omitted, return the current setting.\n"); |
| 2007 | |
| 2008 | static PyObject* |
| 2009 | stat_float_times(PyObject* self, PyObject *args) |
| 2010 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2011 | int newval = -1; |
| 2012 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 2013 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 2014 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2015 | "stat_float_times() is deprecated", |
| 2016 | 1)) |
| 2017 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2018 | if (newval == -1) |
| 2019 | /* Return old value */ |
| 2020 | return PyBool_FromLong(_stat_float_times); |
| 2021 | _stat_float_times = newval; |
| 2022 | Py_INCREF(Py_None); |
| 2023 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2024 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2025 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2026 | static PyObject *billion = NULL; |
| 2027 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2028 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2029 | 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] | 2030 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2031 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2032 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2033 | PyObject *s_in_ns = NULL; |
| 2034 | PyObject *ns_total = NULL; |
| 2035 | PyObject *float_s = NULL; |
| 2036 | |
| 2037 | if (!(s && ns_fractional)) |
| 2038 | goto exit; |
| 2039 | |
| 2040 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2041 | if (!s_in_ns) |
| 2042 | goto exit; |
| 2043 | |
| 2044 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2045 | if (!ns_total) |
| 2046 | goto exit; |
| 2047 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2048 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2049 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2050 | if (!float_s) |
| 2051 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2052 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2053 | else { |
| 2054 | float_s = s; |
| 2055 | Py_INCREF(float_s); |
| 2056 | } |
| 2057 | |
| 2058 | PyStructSequence_SET_ITEM(v, index, s); |
| 2059 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2060 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2061 | s = NULL; |
| 2062 | float_s = NULL; |
| 2063 | ns_total = NULL; |
| 2064 | exit: |
| 2065 | Py_XDECREF(s); |
| 2066 | Py_XDECREF(ns_fractional); |
| 2067 | Py_XDECREF(s_in_ns); |
| 2068 | Py_XDECREF(ns_total); |
| 2069 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2072 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2073 | (used by posix_stat() and posix_fstat()) */ |
| 2074 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2075 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2076 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2077 | unsigned long ansec, mnsec, cnsec; |
| 2078 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2079 | if (v == NULL) |
| 2080 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2081 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2082 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2083 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2084 | PyStructSequence_SET_ITEM(v, 1, |
| 2085 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2086 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2087 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2088 | #endif |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2089 | #ifdef MS_WINDOWS |
| 2090 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
| 2091 | #elif defined(HAVE_LONG_LONG) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2092 | PyStructSequence_SET_ITEM(v, 2, |
| 2093 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2094 | #else |
Brian Curtin | 9cc4321 | 2013-01-01 12:31:06 -0600 | [diff] [blame] | 2095 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2096 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2097 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2098 | #if defined(MS_WINDOWS) |
| 2099 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2100 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2101 | #else |
| 2102 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2103 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2104 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2105 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2106 | PyStructSequence_SET_ITEM(v, 6, |
| 2107 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2108 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2109 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2110 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2111 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2112 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2113 | ansec = st->st_atim.tv_nsec; |
| 2114 | mnsec = st->st_mtim.tv_nsec; |
| 2115 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2116 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2117 | ansec = st->st_atimespec.tv_nsec; |
| 2118 | mnsec = st->st_mtimespec.tv_nsec; |
| 2119 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2120 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2121 | ansec = st->st_atime_nsec; |
| 2122 | mnsec = st->st_mtime_nsec; |
| 2123 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2124 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2125 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2126 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2127 | fill_time(v, 7, st->st_atime, ansec); |
| 2128 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2129 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2130 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2131 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2132 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2133 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2134 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2135 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2136 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2137 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2138 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2139 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2140 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2141 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2142 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2143 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2144 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2145 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2146 | #endif |
| 2147 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2148 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2149 | PyObject *val; |
| 2150 | unsigned long bsec,bnsec; |
| 2151 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2152 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2153 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2154 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2155 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2156 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2157 | if (_stat_float_times) { |
| 2158 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2159 | } else { |
| 2160 | val = PyLong_FromLong((long)bsec); |
| 2161 | } |
| 2162 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2163 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2164 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2165 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2166 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2167 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2168 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2169 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2170 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2171 | if (PyErr_Occurred()) { |
| 2172 | Py_DECREF(v); |
| 2173 | return NULL; |
| 2174 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2175 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2176 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2179 | /* POSIX methods */ |
| 2180 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2181 | |
| 2182 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2183 | posix_do_stat(char *function_name, path_t *path, |
| 2184 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2185 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2186 | STRUCT_STAT st; |
| 2187 | int result; |
| 2188 | |
| 2189 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2190 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2191 | return NULL; |
| 2192 | #endif |
| 2193 | |
| 2194 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2195 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2196 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2197 | return NULL; |
| 2198 | |
| 2199 | Py_BEGIN_ALLOW_THREADS |
| 2200 | if (path->fd != -1) |
| 2201 | result = FSTAT(path->fd, &st); |
| 2202 | else |
| 2203 | #ifdef MS_WINDOWS |
| 2204 | if (path->wide) { |
| 2205 | if (follow_symlinks) |
| 2206 | result = win32_stat_w(path->wide, &st); |
| 2207 | else |
| 2208 | result = win32_lstat_w(path->wide, &st); |
| 2209 | } |
| 2210 | else |
| 2211 | #endif |
| 2212 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2213 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2214 | result = LSTAT(path->narrow, &st); |
| 2215 | else |
| 2216 | #endif |
| 2217 | #ifdef HAVE_FSTATAT |
| 2218 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2219 | result = fstatat(dir_fd, path->narrow, &st, |
| 2220 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2221 | else |
| 2222 | #endif |
| 2223 | result = STAT(path->narrow, &st); |
| 2224 | Py_END_ALLOW_THREADS |
| 2225 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2226 | if (result != 0) { |
| 2227 | return path_error(path); |
| 2228 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2229 | |
| 2230 | return _pystat_fromstructstat(&st); |
| 2231 | } |
| 2232 | |
| 2233 | PyDoc_STRVAR(posix_stat__doc__, |
| 2234 | "stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result\n\n\ |
| 2235 | Perform a stat system call on the given path.\n\ |
| 2236 | \n\ |
| 2237 | path may be specified as either a string or as an open file descriptor.\n\ |
| 2238 | \n\ |
| 2239 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2240 | and path should be relative; path will then be relative to that directory.\n\ |
| 2241 | dir_fd may not be supported on your platform; if it is unavailable, using\n\ |
| 2242 | it will raise a NotImplementedError.\n\ |
| 2243 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2244 | link, stat will examine the symbolic link itself instead of the file the\n\ |
| 2245 | link points to.\n\ |
| 2246 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2247 | an open file descriptor."); |
| 2248 | |
| 2249 | static PyObject * |
| 2250 | posix_stat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2251 | { |
| 2252 | static char *keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2253 | path_t path; |
| 2254 | int dir_fd = DEFAULT_DIR_FD; |
| 2255 | int follow_symlinks = 1; |
| 2256 | PyObject *return_value; |
| 2257 | |
| 2258 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2259 | path.function_name = "stat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2260 | path.allow_fd = 1; |
| 2261 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords, |
| 2262 | path_converter, &path, |
| 2263 | #ifdef HAVE_FSTATAT |
| 2264 | dir_fd_converter, &dir_fd, |
| 2265 | #else |
| 2266 | dir_fd_unavailable, &dir_fd, |
| 2267 | #endif |
| 2268 | &follow_symlinks)) |
| 2269 | return NULL; |
| 2270 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2271 | path_cleanup(&path); |
| 2272 | return return_value; |
| 2273 | } |
| 2274 | |
| 2275 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2276 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2277 | Like stat(), but do not follow symbolic links.\n\ |
| 2278 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2279 | |
| 2280 | static PyObject * |
| 2281 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2282 | { |
| 2283 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2284 | path_t path; |
| 2285 | int dir_fd = DEFAULT_DIR_FD; |
| 2286 | int follow_symlinks = 0; |
| 2287 | PyObject *return_value; |
| 2288 | |
| 2289 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2290 | path.function_name = "lstat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2291 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2292 | path_converter, &path, |
| 2293 | #ifdef HAVE_FSTATAT |
| 2294 | dir_fd_converter, &dir_fd |
| 2295 | #else |
| 2296 | dir_fd_unavailable, &dir_fd |
| 2297 | #endif |
| 2298 | )) |
| 2299 | return NULL; |
| 2300 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2301 | path_cleanup(&path); |
| 2302 | return return_value; |
| 2303 | } |
| 2304 | |
| 2305 | PyDoc_STRVAR(posix_access__doc__, |
| 2306 | "access(path, mode, *, dir_fd=None, effective_ids=False,\ |
| 2307 | follow_symlinks=True)\n\n\ |
| 2308 | Use the real uid/gid to test for access to a path. Returns True if granted,\n\ |
| 2309 | False otherwise.\n\ |
| 2310 | \n\ |
| 2311 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2312 | and path should be relative; path will then be relative to that directory.\n\ |
| 2313 | If effective_ids is True, access will use the effective uid/gid instead of\n\ |
| 2314 | the real uid/gid.\n\ |
| 2315 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2316 | link, access will examine the symbolic link itself instead of the file the\n\ |
| 2317 | link points to.\n\ |
| 2318 | dir_fd, effective_ids, and follow_symlinks may not be implemented\n\ |
| 2319 | on your platform. If they are unavailable, using them will raise a\n\ |
| 2320 | NotImplementedError.\n\ |
| 2321 | \n\ |
| 2322 | Note that most operations will use the effective uid/gid, therefore this\n\ |
| 2323 | routine can be used in a suid/sgid environment to test if the invoking user\n\ |
| 2324 | has the specified access to the path.\n\ |
| 2325 | The mode argument can be F_OK to test existence, or the inclusive-OR\n\ |
| 2326 | of R_OK, W_OK, and X_OK."); |
| 2327 | |
| 2328 | static PyObject * |
| 2329 | posix_access(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2330 | { |
| 2331 | static char *keywords[] = {"path", "mode", "dir_fd", "effective_ids", |
| 2332 | "follow_symlinks", NULL}; |
| 2333 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2334 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2335 | int dir_fd = DEFAULT_DIR_FD; |
| 2336 | int effective_ids = 0; |
| 2337 | int follow_symlinks = 1; |
| 2338 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2339 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2340 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2341 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2342 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2343 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2344 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2345 | |
| 2346 | memset(&path, 0, sizeof(path)); |
| 2347 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", keywords, |
| 2348 | path_converter, &path, &mode, |
| 2349 | #ifdef HAVE_FACCESSAT |
| 2350 | dir_fd_converter, &dir_fd, |
| 2351 | #else |
| 2352 | dir_fd_unavailable, &dir_fd, |
| 2353 | #endif |
| 2354 | &effective_ids, &follow_symlinks)) |
| 2355 | return NULL; |
| 2356 | |
| 2357 | #ifndef HAVE_FACCESSAT |
| 2358 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2359 | goto exit; |
| 2360 | |
| 2361 | if (effective_ids) { |
| 2362 | argument_unavailable_error("access", "effective_ids"); |
| 2363 | goto exit; |
| 2364 | } |
| 2365 | #endif |
| 2366 | |
| 2367 | #ifdef MS_WINDOWS |
| 2368 | Py_BEGIN_ALLOW_THREADS |
| 2369 | if (path.wide != NULL) |
| 2370 | attr = GetFileAttributesW(path.wide); |
| 2371 | else |
| 2372 | attr = GetFileAttributesA(path.narrow); |
| 2373 | Py_END_ALLOW_THREADS |
| 2374 | |
| 2375 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2376 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2377 | * * we didn't get a -1, and |
| 2378 | * * write access wasn't requested, |
| 2379 | * * or the file isn't read-only, |
| 2380 | * * or it's a directory. |
| 2381 | * (Directories cannot be read-only on Windows.) |
| 2382 | */ |
| 2383 | return_value = PyBool_FromLong( |
| 2384 | (attr != 0xFFFFFFFF) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2385 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2386 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2387 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2388 | #else |
| 2389 | |
| 2390 | Py_BEGIN_ALLOW_THREADS |
| 2391 | #ifdef HAVE_FACCESSAT |
| 2392 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2393 | effective_ids || |
| 2394 | !follow_symlinks) { |
| 2395 | int flags = 0; |
| 2396 | if (!follow_symlinks) |
| 2397 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2398 | if (effective_ids) |
| 2399 | flags |= AT_EACCESS; |
| 2400 | result = faccessat(dir_fd, path.narrow, mode, flags); |
| 2401 | } |
| 2402 | else |
| 2403 | #endif |
| 2404 | result = access(path.narrow, mode); |
| 2405 | Py_END_ALLOW_THREADS |
| 2406 | return_value = PyBool_FromLong(!result); |
| 2407 | #endif |
| 2408 | |
| 2409 | #ifndef HAVE_FACCESSAT |
| 2410 | exit: |
| 2411 | #endif |
| 2412 | path_cleanup(&path); |
| 2413 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2416 | #ifndef F_OK |
| 2417 | #define F_OK 0 |
| 2418 | #endif |
| 2419 | #ifndef R_OK |
| 2420 | #define R_OK 4 |
| 2421 | #endif |
| 2422 | #ifndef W_OK |
| 2423 | #define W_OK 2 |
| 2424 | #endif |
| 2425 | #ifndef X_OK |
| 2426 | #define X_OK 1 |
| 2427 | #endif |
| 2428 | |
| 2429 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2430 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2431 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2432 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2433 | |
| 2434 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2435 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2436 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2437 | int id; |
| 2438 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2440 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 2441 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2442 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2443 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2444 | /* file descriptor 0 only, the default input device (stdin) */ |
| 2445 | if (id == 0) { |
| 2446 | ret = ttyname(); |
| 2447 | } |
| 2448 | else { |
| 2449 | ret = NULL; |
| 2450 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2451 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2452 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2453 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2454 | if (ret == NULL) |
| 2455 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2456 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2457 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2458 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2459 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2460 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2461 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2462 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2463 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2464 | |
| 2465 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2466 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2467 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2468 | char *ret; |
| 2469 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2470 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2471 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2472 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2473 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2474 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2475 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2476 | if (ret == NULL) |
| 2477 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2478 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2479 | } |
| 2480 | #endif |
| 2481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2482 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2483 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2484 | Change the current working directory to the specified path.\n\ |
| 2485 | \n\ |
| 2486 | path may always be specified as a string.\n\ |
| 2487 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2488 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2489 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2490 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2491 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2492 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2493 | path_t path; |
| 2494 | int result; |
| 2495 | PyObject *return_value = NULL; |
| 2496 | static char *keywords[] = {"path", NULL}; |
| 2497 | |
| 2498 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2499 | path.function_name = "chdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2500 | #ifdef HAVE_FCHDIR |
| 2501 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2502 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2503 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2504 | path_converter, &path |
| 2505 | )) |
| 2506 | return NULL; |
| 2507 | |
| 2508 | Py_BEGIN_ALLOW_THREADS |
| 2509 | #ifdef MS_WINDOWS |
| 2510 | if (path.wide) |
| 2511 | result = win32_wchdir(path.wide); |
| 2512 | else |
| 2513 | result = win32_chdir(path.narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2514 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2515 | #else |
| 2516 | #ifdef HAVE_FCHDIR |
| 2517 | if (path.fd != -1) |
| 2518 | result = fchdir(path.fd); |
| 2519 | else |
| 2520 | #endif |
| 2521 | result = chdir(path.narrow); |
| 2522 | #endif |
| 2523 | Py_END_ALLOW_THREADS |
| 2524 | |
| 2525 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2526 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2527 | goto exit; |
| 2528 | } |
| 2529 | |
| 2530 | return_value = Py_None; |
| 2531 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2532 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2533 | exit: |
| 2534 | path_cleanup(&path); |
| 2535 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2538 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2539 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2540 | "fchdir(fd)\n\n\ |
| 2541 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2542 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2543 | |
| 2544 | static PyObject * |
| 2545 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2546 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2547 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2548 | } |
| 2549 | #endif /* HAVE_FCHDIR */ |
| 2550 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2551 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2552 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2553 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2554 | Change the access permissions of a file.\n\ |
| 2555 | \n\ |
| 2556 | path may always be specified as a string.\n\ |
| 2557 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2558 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2559 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2560 | and path should be relative; path will then be relative to that directory.\n\ |
| 2561 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2562 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2563 | link points to.\n\ |
| 2564 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2565 | an open file descriptor.\n\ |
| 2566 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2567 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2568 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2569 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2570 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2571 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2572 | path_t path; |
| 2573 | int mode; |
| 2574 | int dir_fd = DEFAULT_DIR_FD; |
| 2575 | int follow_symlinks = 1; |
| 2576 | int result; |
| 2577 | PyObject *return_value = NULL; |
| 2578 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2579 | "follow_symlinks", NULL}; |
| 2580 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2581 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2582 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2583 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2584 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2585 | #ifdef HAVE_FCHMODAT |
| 2586 | int fchmodat_nofollow_unsupported = 0; |
| 2587 | #endif |
| 2588 | |
| 2589 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2590 | path.function_name = "chmod"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2591 | #ifdef HAVE_FCHMOD |
| 2592 | path.allow_fd = 1; |
| 2593 | #endif |
| 2594 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2595 | path_converter, &path, |
| 2596 | &mode, |
| 2597 | #ifdef HAVE_FCHMODAT |
| 2598 | dir_fd_converter, &dir_fd, |
| 2599 | #else |
| 2600 | dir_fd_unavailable, &dir_fd, |
| 2601 | #endif |
| 2602 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2603 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2604 | |
| 2605 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2606 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2607 | goto exit; |
| 2608 | #endif |
| 2609 | |
| 2610 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2611 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2612 | if (path.wide) |
| 2613 | attr = GetFileAttributesW(path.wide); |
| 2614 | else |
| 2615 | attr = GetFileAttributesA(path.narrow); |
| 2616 | if (attr == 0xFFFFFFFF) |
| 2617 | result = 0; |
| 2618 | else { |
| 2619 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2620 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2621 | else |
| 2622 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2623 | if (path.wide) |
| 2624 | result = SetFileAttributesW(path.wide, attr); |
| 2625 | else |
| 2626 | result = SetFileAttributesA(path.narrow, attr); |
| 2627 | } |
| 2628 | Py_END_ALLOW_THREADS |
| 2629 | |
| 2630 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 2631 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2632 | goto exit; |
| 2633 | } |
| 2634 | #else /* MS_WINDOWS */ |
| 2635 | Py_BEGIN_ALLOW_THREADS |
| 2636 | #ifdef HAVE_FCHMOD |
| 2637 | if (path.fd != -1) |
| 2638 | result = fchmod(path.fd, mode); |
| 2639 | else |
| 2640 | #endif |
| 2641 | #ifdef HAVE_LCHMOD |
| 2642 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2643 | result = lchmod(path.narrow, mode); |
| 2644 | else |
| 2645 | #endif |
| 2646 | #ifdef HAVE_FCHMODAT |
| 2647 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2648 | /* |
| 2649 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2650 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2651 | * and then says it isn't implemented yet. |
| 2652 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2653 | * |
| 2654 | * Once it is supported, os.chmod will automatically |
| 2655 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2656 | * Until then, we need to be careful what exception we raise. |
| 2657 | */ |
| 2658 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2659 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2660 | /* |
| 2661 | * But wait! We can't throw the exception without allowing threads, |
| 2662 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2663 | */ |
| 2664 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2665 | result && |
| 2666 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2667 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2668 | } |
| 2669 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2670 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2671 | result = chmod(path.narrow, mode); |
| 2672 | Py_END_ALLOW_THREADS |
| 2673 | |
| 2674 | if (result) { |
| 2675 | #ifdef HAVE_FCHMODAT |
| 2676 | if (fchmodat_nofollow_unsupported) { |
| 2677 | if (dir_fd != DEFAULT_DIR_FD) |
| 2678 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2679 | dir_fd, follow_symlinks); |
| 2680 | else |
| 2681 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2682 | } |
| 2683 | else |
| 2684 | #endif |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2685 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2686 | goto exit; |
| 2687 | } |
| 2688 | #endif |
| 2689 | |
| 2690 | Py_INCREF(Py_None); |
| 2691 | return_value = Py_None; |
| 2692 | exit: |
| 2693 | path_cleanup(&path); |
| 2694 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2697 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2698 | #ifdef HAVE_FCHMOD |
| 2699 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2700 | "fchmod(fd, mode)\n\n\ |
| 2701 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2702 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2703 | |
| 2704 | static PyObject * |
| 2705 | posix_fchmod(PyObject *self, PyObject *args) |
| 2706 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2707 | int fd, mode, res; |
| 2708 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2709 | return NULL; |
| 2710 | Py_BEGIN_ALLOW_THREADS |
| 2711 | res = fchmod(fd, mode); |
| 2712 | Py_END_ALLOW_THREADS |
| 2713 | if (res < 0) |
| 2714 | return posix_error(); |
| 2715 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2716 | } |
| 2717 | #endif /* HAVE_FCHMOD */ |
| 2718 | |
| 2719 | #ifdef HAVE_LCHMOD |
| 2720 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2721 | "lchmod(path, mode)\n\n\ |
| 2722 | 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] | 2723 | affects the link itself rather than the target.\n\ |
| 2724 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2725 | |
| 2726 | static PyObject * |
| 2727 | posix_lchmod(PyObject *self, PyObject *args) |
| 2728 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2729 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2730 | int i; |
| 2731 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2732 | memset(&path, 0, sizeof(path)); |
| 2733 | path.function_name = "lchmod"; |
| 2734 | if (!PyArg_ParseTuple(args, "O&i:lchmod", |
| 2735 | path_converter, &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2736 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2737 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2738 | res = lchmod(path.narrow, i); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2739 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2740 | if (res < 0) { |
| 2741 | path_error(&path); |
| 2742 | path_cleanup(&path); |
| 2743 | return NULL; |
| 2744 | } |
| 2745 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2746 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2747 | } |
| 2748 | #endif /* HAVE_LCHMOD */ |
| 2749 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2750 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2751 | #ifdef HAVE_CHFLAGS |
| 2752 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2753 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 2754 | Set file flags.\n\ |
| 2755 | \n\ |
| 2756 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2757 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 2758 | file the link points to.\n\ |
| 2759 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 2760 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2761 | |
| 2762 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2763 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2764 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2765 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2766 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2767 | int follow_symlinks = 1; |
| 2768 | int result; |
| 2769 | PyObject *return_value; |
| 2770 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 2771 | |
| 2772 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2773 | path.function_name = "chflags"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2774 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 2775 | path_converter, &path, |
| 2776 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2777 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2778 | |
| 2779 | #ifndef HAVE_LCHFLAGS |
| 2780 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 2781 | goto exit; |
| 2782 | #endif |
| 2783 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2784 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2785 | #ifdef HAVE_LCHFLAGS |
| 2786 | if (!follow_symlinks) |
| 2787 | result = lchflags(path.narrow, flags); |
| 2788 | else |
| 2789 | #endif |
| 2790 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2791 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2792 | |
| 2793 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2794 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2795 | goto exit; |
| 2796 | } |
| 2797 | |
| 2798 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2799 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2800 | |
| 2801 | exit: |
| 2802 | path_cleanup(&path); |
| 2803 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2804 | } |
| 2805 | #endif /* HAVE_CHFLAGS */ |
| 2806 | |
| 2807 | #ifdef HAVE_LCHFLAGS |
| 2808 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2809 | "lchflags(path, flags)\n\n\ |
| 2810 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2811 | This function will not follow symbolic links.\n\ |
| 2812 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2813 | |
| 2814 | static PyObject * |
| 2815 | posix_lchflags(PyObject *self, PyObject *args) |
| 2816 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2817 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2818 | unsigned long flags; |
| 2819 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2820 | memset(&path, 0, sizeof(path)); |
| 2821 | path.function_name = "lchflags"; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2822 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2823 | path_converter, &path, &flags)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2824 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2825 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2826 | res = lchflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2827 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2828 | if (res < 0) { |
| 2829 | path_error(&path); |
| 2830 | path_cleanup(&path); |
| 2831 | return NULL; |
| 2832 | } |
| 2833 | path_cleanup(&path); |
| 2834 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2835 | } |
| 2836 | #endif /* HAVE_LCHFLAGS */ |
| 2837 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2838 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2839 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2840 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2841 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2842 | |
| 2843 | static PyObject * |
| 2844 | posix_chroot(PyObject *self, PyObject *args) |
| 2845 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2846 | return posix_1str("chroot", args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2847 | } |
| 2848 | #endif |
| 2849 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2850 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2851 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2852 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2853 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2854 | |
| 2855 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2856 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2857 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2858 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2859 | } |
| 2860 | #endif /* HAVE_FSYNC */ |
| 2861 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2862 | #ifdef HAVE_SYNC |
| 2863 | PyDoc_STRVAR(posix_sync__doc__, |
| 2864 | "sync()\n\n\ |
| 2865 | Force write of everything to disk."); |
| 2866 | |
| 2867 | static PyObject * |
| 2868 | posix_sync(PyObject *self, PyObject *noargs) |
| 2869 | { |
| 2870 | Py_BEGIN_ALLOW_THREADS |
| 2871 | sync(); |
| 2872 | Py_END_ALLOW_THREADS |
| 2873 | Py_RETURN_NONE; |
| 2874 | } |
| 2875 | #endif |
| 2876 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2877 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2878 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2879 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2880 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2881 | #endif |
| 2882 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2883 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2884 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2885 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2886 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2887 | |
| 2888 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2889 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2890 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2891 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2892 | } |
| 2893 | #endif /* HAVE_FDATASYNC */ |
| 2894 | |
| 2895 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2896 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2897 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2898 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2899 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2900 | \n\ |
| 2901 | path may always be specified as a string.\n\ |
| 2902 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2903 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2904 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2905 | and path should be relative; path will then be relative to that directory.\n\ |
| 2906 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2907 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 2908 | link points to.\n\ |
| 2909 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2910 | an open file descriptor.\n\ |
| 2911 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2912 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2913 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2914 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2915 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2916 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2917 | path_t path; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2918 | uid_t uid; |
| 2919 | gid_t gid; |
| 2920 | int dir_fd = DEFAULT_DIR_FD; |
| 2921 | int follow_symlinks = 1; |
| 2922 | int result; |
| 2923 | PyObject *return_value = NULL; |
| 2924 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 2925 | "follow_symlinks", NULL}; |
| 2926 | |
| 2927 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2928 | path.function_name = "chown"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2929 | #ifdef HAVE_FCHOWN |
| 2930 | path.allow_fd = 1; |
| 2931 | #endif |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2932 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&|$O&p:chown", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2933 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2934 | _Py_Uid_Converter, &uid, |
| 2935 | _Py_Gid_Converter, &gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2936 | #ifdef HAVE_FCHOWNAT |
| 2937 | dir_fd_converter, &dir_fd, |
| 2938 | #else |
| 2939 | dir_fd_unavailable, &dir_fd, |
| 2940 | #endif |
| 2941 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2942 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2943 | |
| 2944 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 2945 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 2946 | goto exit; |
| 2947 | #endif |
| 2948 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 2949 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 2950 | goto exit; |
| 2951 | |
| 2952 | #ifdef __APPLE__ |
| 2953 | /* |
| 2954 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 2955 | * (But we still have an lchown symbol because of weak-linking.) |
| 2956 | * It doesn't have fchownat either. So there's no possibility |
| 2957 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2958 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2959 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 2960 | follow_symlinks_specified("chown", follow_symlinks); |
| 2961 | goto exit; |
| 2962 | } |
| 2963 | #endif |
| 2964 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2965 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2966 | #ifdef HAVE_FCHOWN |
| 2967 | if (path.fd != -1) |
| 2968 | result = fchown(path.fd, uid, gid); |
| 2969 | else |
| 2970 | #endif |
| 2971 | #ifdef HAVE_LCHOWN |
| 2972 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2973 | result = lchown(path.narrow, uid, gid); |
| 2974 | else |
| 2975 | #endif |
| 2976 | #ifdef HAVE_FCHOWNAT |
| 2977 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 2978 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 2979 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2980 | else |
| 2981 | #endif |
| 2982 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2983 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2984 | |
| 2985 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2986 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2987 | goto exit; |
| 2988 | } |
| 2989 | |
| 2990 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2991 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2992 | |
| 2993 | exit: |
| 2994 | path_cleanup(&path); |
| 2995 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2996 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2997 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2998 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2999 | #ifdef HAVE_FCHOWN |
| 3000 | PyDoc_STRVAR(posix_fchown__doc__, |
| 3001 | "fchown(fd, uid, gid)\n\n\ |
| 3002 | 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] | 3003 | 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] | 3004 | |
| 3005 | static PyObject * |
| 3006 | posix_fchown(PyObject *self, PyObject *args) |
| 3007 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3008 | int fd; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3009 | uid_t uid; |
| 3010 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3011 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3012 | if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd, |
| 3013 | _Py_Uid_Converter, &uid, |
| 3014 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3015 | return NULL; |
| 3016 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3017 | res = fchown(fd, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3018 | Py_END_ALLOW_THREADS |
| 3019 | if (res < 0) |
| 3020 | return posix_error(); |
| 3021 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3022 | } |
| 3023 | #endif /* HAVE_FCHOWN */ |
| 3024 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3025 | #ifdef HAVE_LCHOWN |
| 3026 | PyDoc_STRVAR(posix_lchown__doc__, |
| 3027 | "lchown(path, uid, gid)\n\n\ |
| 3028 | 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] | 3029 | This function will not follow symbolic links.\n\ |
| 3030 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3031 | |
| 3032 | static PyObject * |
| 3033 | posix_lchown(PyObject *self, PyObject *args) |
| 3034 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3035 | path_t path; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3036 | uid_t uid; |
| 3037 | gid_t gid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3038 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3039 | memset(&path, 0, sizeof(path)); |
| 3040 | path.function_name = "lchown"; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3041 | if (!PyArg_ParseTuple(args, "O&O&O&:lchown", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3042 | path_converter, &path, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3043 | _Py_Uid_Converter, &uid, |
| 3044 | _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3045 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3046 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | c2d0200 | 2013-02-10 22:03:08 +0200 | [diff] [blame] | 3047 | res = lchown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3048 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3049 | if (res < 0) { |
| 3050 | path_error(&path); |
| 3051 | path_cleanup(&path); |
| 3052 | return NULL; |
| 3053 | } |
| 3054 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3055 | Py_INCREF(Py_None); |
| 3056 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3057 | } |
| 3058 | #endif /* HAVE_LCHOWN */ |
| 3059 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3060 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3061 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3062 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3063 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3064 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3065 | char buf[1026]; |
| 3066 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3067 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3068 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3069 | if (!use_bytes) { |
| 3070 | wchar_t wbuf[1026]; |
| 3071 | wchar_t *wbuf2 = wbuf; |
| 3072 | PyObject *resobj; |
| 3073 | DWORD len; |
| 3074 | Py_BEGIN_ALLOW_THREADS |
| 3075 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 3076 | /* If the buffer is large enough, len does not include the |
| 3077 | terminating \0. If the buffer is too small, len includes |
| 3078 | the space needed for the terminator. */ |
| 3079 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 3080 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 3081 | if (wbuf2) |
| 3082 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3083 | } |
| 3084 | Py_END_ALLOW_THREADS |
| 3085 | if (!wbuf2) { |
| 3086 | PyErr_NoMemory(); |
| 3087 | return NULL; |
| 3088 | } |
| 3089 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3090 | if (wbuf2 != wbuf) |
| 3091 | free(wbuf2); |
| 3092 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3093 | } |
| 3094 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3095 | if (wbuf2 != wbuf) |
| 3096 | free(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3097 | return resobj; |
| 3098 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3099 | |
| 3100 | if (win32_warn_bytes_api()) |
| 3101 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3102 | #endif |
| 3103 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3104 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3105 | res = getcwd(buf, sizeof buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3106 | Py_END_ALLOW_THREADS |
| 3107 | if (res == NULL) |
| 3108 | return posix_error(); |
| 3109 | if (use_bytes) |
| 3110 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3111 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3112 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3113 | |
| 3114 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 3115 | "getcwd() -> path\n\n\ |
| 3116 | Return a unicode string representing the current working directory."); |
| 3117 | |
| 3118 | static PyObject * |
| 3119 | posix_getcwd_unicode(PyObject *self) |
| 3120 | { |
| 3121 | return posix_getcwd(0); |
| 3122 | } |
| 3123 | |
| 3124 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 3125 | "getcwdb() -> path\n\n\ |
| 3126 | Return a bytes string representing the current working directory."); |
| 3127 | |
| 3128 | static PyObject * |
| 3129 | posix_getcwd_bytes(PyObject *self) |
| 3130 | { |
| 3131 | return posix_getcwd(1); |
| 3132 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3133 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3134 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3135 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3136 | #define HAVE_LINK 1 |
| 3137 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3138 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3139 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3140 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3141 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 3142 | Create a hard link to a file.\n\ |
| 3143 | \n\ |
| 3144 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3145 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3146 | should be relative; the path will then be relative to that directory.\n\ |
| 3147 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3148 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3149 | file the link points to.\n\ |
| 3150 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3151 | platform. If they are unavailable, using them will raise a\n\ |
| 3152 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3153 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3154 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3155 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3156 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3157 | path_t src, dst; |
| 3158 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3159 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3160 | int follow_symlinks = 1; |
| 3161 | PyObject *return_value = NULL; |
| 3162 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3163 | "follow_symlinks", NULL}; |
| 3164 | #ifdef MS_WINDOWS |
| 3165 | BOOL result; |
| 3166 | #else |
| 3167 | int result; |
| 3168 | #endif |
| 3169 | |
| 3170 | memset(&src, 0, sizeof(src)); |
| 3171 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3172 | src.function_name = "link"; |
| 3173 | dst.function_name = "link"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3174 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3175 | path_converter, &src, |
| 3176 | path_converter, &dst, |
| 3177 | dir_fd_converter, &src_dir_fd, |
| 3178 | dir_fd_converter, &dst_dir_fd, |
| 3179 | &follow_symlinks)) |
| 3180 | return NULL; |
| 3181 | |
| 3182 | #ifndef HAVE_LINKAT |
| 3183 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3184 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3185 | goto exit; |
| 3186 | } |
| 3187 | #endif |
| 3188 | |
| 3189 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3190 | PyErr_SetString(PyExc_NotImplementedError, |
| 3191 | "link: src and dst must be the same type"); |
| 3192 | goto exit; |
| 3193 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3194 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3195 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3196 | Py_BEGIN_ALLOW_THREADS |
| 3197 | if (src.wide) |
| 3198 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3199 | else |
| 3200 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3201 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3202 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3203 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 3204 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3205 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3206 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3207 | #else |
| 3208 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3209 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3210 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3211 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3212 | (!follow_symlinks)) |
| 3213 | result = linkat(src_dir_fd, src.narrow, |
| 3214 | dst_dir_fd, dst.narrow, |
| 3215 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3216 | else |
| 3217 | #endif |
| 3218 | result = link(src.narrow, dst.narrow); |
| 3219 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3220 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3221 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3222 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3223 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3224 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3225 | #endif |
| 3226 | |
| 3227 | return_value = Py_None; |
| 3228 | Py_INCREF(Py_None); |
| 3229 | |
| 3230 | exit: |
| 3231 | path_cleanup(&src); |
| 3232 | path_cleanup(&dst); |
| 3233 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3234 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3235 | #endif |
| 3236 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3237 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3238 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3239 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3240 | "listdir(path='.') -> list_of_filenames\n\n\ |
| 3241 | 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] | 3242 | 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] | 3243 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3244 | \n\ |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3245 | path can be specified as either str or bytes. If path is bytes,\n\ |
| 3246 | the filenames returned will also be bytes; in all other circumstances\n\ |
| 3247 | the filenames returned will be str.\n\ |
| 3248 | On some platforms, path may also be specified as an open file descriptor;\n\ |
| 3249 | the file descriptor must refer to a directory.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3250 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3251 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3252 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3253 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3254 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3255 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3256 | static char *keywords[] = {"path", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3257 | PyObject *v; |
| 3258 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3259 | BOOL result; |
| 3260 | WIN32_FIND_DATA FileData; |
| 3261 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 3262 | char *bufptr = namebuf; |
| 3263 | /* only claim to have space for MAX_PATH */ |
| 3264 | Py_ssize_t len = sizeof(namebuf)-5; |
| 3265 | PyObject *po = NULL; |
| 3266 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3267 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3268 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3269 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3270 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3271 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3272 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3273 | po_wchars = L"."; |
| 3274 | len = 1; |
| 3275 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3276 | po_wchars = path->wide; |
| 3277 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3278 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3279 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3280 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 3281 | if (!wnamebuf) { |
| 3282 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3283 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3284 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3285 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3286 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3287 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3288 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 3289 | wnamebuf[len++] = L'\\'; |
| 3290 | wcscpy(wnamebuf + len, L"*.*"); |
| 3291 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3292 | if ((list = PyList_New(0)) == NULL) { |
| 3293 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3294 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3295 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3296 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3297 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3298 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3299 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3300 | if (error == ERROR_FILE_NOT_FOUND) |
| 3301 | goto exit; |
| 3302 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3303 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3304 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3305 | } |
| 3306 | do { |
| 3307 | /* Skip over . and .. */ |
| 3308 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3309 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3310 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3311 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3312 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3313 | Py_DECREF(list); |
| 3314 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3315 | break; |
| 3316 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3317 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3318 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3319 | Py_DECREF(list); |
| 3320 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3321 | break; |
| 3322 | } |
| 3323 | Py_DECREF(v); |
| 3324 | } |
| 3325 | Py_BEGIN_ALLOW_THREADS |
| 3326 | result = FindNextFileW(hFindFile, &wFileData); |
| 3327 | Py_END_ALLOW_THREADS |
| 3328 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3329 | it got to the end of the directory. */ |
| 3330 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3331 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3332 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3333 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3334 | } |
| 3335 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3336 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3337 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3338 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3339 | strcpy(namebuf, path->narrow); |
| 3340 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3341 | if (len > 0) { |
| 3342 | char ch = namebuf[len-1]; |
| 3343 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 3344 | namebuf[len++] = '/'; |
| 3345 | strcpy(namebuf + len, "*.*"); |
| 3346 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3347 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3348 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3349 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3350 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3351 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3352 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3353 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3354 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3355 | int error = GetLastError(); |
| 3356 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3357 | goto exit; |
| 3358 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3359 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3360 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3361 | } |
| 3362 | do { |
| 3363 | /* Skip over . and .. */ |
| 3364 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3365 | strcmp(FileData.cFileName, "..") != 0) { |
| 3366 | v = PyBytes_FromString(FileData.cFileName); |
| 3367 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3368 | Py_DECREF(list); |
| 3369 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3370 | break; |
| 3371 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3372 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3373 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3374 | Py_DECREF(list); |
| 3375 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3376 | break; |
| 3377 | } |
| 3378 | Py_DECREF(v); |
| 3379 | } |
| 3380 | Py_BEGIN_ALLOW_THREADS |
| 3381 | result = FindNextFile(hFindFile, &FileData); |
| 3382 | Py_END_ALLOW_THREADS |
| 3383 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3384 | it got to the end of the directory. */ |
| 3385 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3386 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3387 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3388 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3389 | } |
| 3390 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3391 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3392 | exit: |
| 3393 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3394 | if (FindClose(hFindFile) == FALSE) { |
| 3395 | if (list != NULL) { |
| 3396 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3397 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3398 | } |
| 3399 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3400 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3401 | if (wnamebuf) |
| 3402 | free(wnamebuf); |
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 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3405 | } /* end of _listdir_windows_no_opendir */ |
| 3406 | |
| 3407 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3408 | |
| 3409 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3410 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3411 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3412 | int fd = -1; |
| 3413 | |
| 3414 | PyObject *v; |
| 3415 | DIR *dirp = NULL; |
| 3416 | struct dirent *ep; |
| 3417 | int return_str; /* if false, return bytes */ |
| 3418 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3419 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3420 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3421 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3422 | /* closedir() closes the FD, so we duplicate it */ |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3423 | Py_BEGIN_ALLOW_THREADS |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3424 | fd = dup(path->fd); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3425 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3426 | |
| 3427 | if (fd == -1) { |
| 3428 | list = posix_error(); |
| 3429 | goto exit; |
| 3430 | } |
| 3431 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3432 | return_str = 1; |
| 3433 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3434 | Py_BEGIN_ALLOW_THREADS |
| 3435 | dirp = fdopendir(fd); |
| 3436 | Py_END_ALLOW_THREADS |
| 3437 | } |
| 3438 | else |
| 3439 | #endif |
| 3440 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3441 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3442 | if (path->narrow) { |
| 3443 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3444 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3445 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3446 | } |
| 3447 | else { |
| 3448 | name = "."; |
| 3449 | return_str = 1; |
| 3450 | } |
| 3451 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3452 | Py_BEGIN_ALLOW_THREADS |
| 3453 | dirp = opendir(name); |
| 3454 | Py_END_ALLOW_THREADS |
| 3455 | } |
| 3456 | |
| 3457 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3458 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3459 | goto exit; |
| 3460 | } |
| 3461 | if ((list = PyList_New(0)) == NULL) { |
| 3462 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3463 | } |
| 3464 | for (;;) { |
| 3465 | errno = 0; |
| 3466 | Py_BEGIN_ALLOW_THREADS |
| 3467 | ep = readdir(dirp); |
| 3468 | Py_END_ALLOW_THREADS |
| 3469 | if (ep == NULL) { |
| 3470 | if (errno == 0) { |
| 3471 | break; |
| 3472 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3473 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3474 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3475 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3476 | } |
| 3477 | } |
| 3478 | if (ep->d_name[0] == '.' && |
| 3479 | (NAMLEN(ep) == 1 || |
| 3480 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3481 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3482 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3483 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3484 | else |
| 3485 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3486 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3487 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3488 | break; |
| 3489 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3490 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3491 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3492 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3493 | break; |
| 3494 | } |
| 3495 | Py_DECREF(v); |
| 3496 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3497 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3498 | exit: |
| 3499 | if (dirp != NULL) { |
| 3500 | Py_BEGIN_ALLOW_THREADS |
| 3501 | if (fd > -1) |
| 3502 | rewinddir(dirp); |
| 3503 | closedir(dirp); |
| 3504 | Py_END_ALLOW_THREADS |
| 3505 | } |
| 3506 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3507 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3508 | } /* end of _posix_listdir */ |
| 3509 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3510 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3511 | static PyObject * |
| 3512 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3513 | { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3514 | path_t path; |
| 3515 | PyObject *list = NULL; |
| 3516 | static char *keywords[] = {"path", NULL}; |
| 3517 | PyObject *return_value; |
| 3518 | |
| 3519 | memset(&path, 0, sizeof(path)); |
| 3520 | path.function_name = "listdir"; |
| 3521 | path.nullable = 1; |
| 3522 | #ifdef HAVE_FDOPENDIR |
| 3523 | path.allow_fd = 1; |
| 3524 | path.fd = -1; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3525 | #endif |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3526 | |
| 3527 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3528 | path_converter, &path)) { |
| 3529 | return NULL; |
| 3530 | } |
| 3531 | |
| 3532 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3533 | return_value = _listdir_windows_no_opendir(&path, list); |
| 3534 | #else |
| 3535 | return_value = _posix_listdir(&path, list); |
| 3536 | #endif |
| 3537 | path_cleanup(&path); |
| 3538 | return return_value; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3539 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3540 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3541 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3542 | /* A helper function for abspath on win32 */ |
| 3543 | static PyObject * |
| 3544 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3545 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3546 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3547 | char outbuf[MAX_PATH*2]; |
| 3548 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3549 | PyObject *po; |
| 3550 | |
| 3551 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3552 | { |
| 3553 | wchar_t *wpath; |
| 3554 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 3555 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3556 | DWORD result; |
| 3557 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3558 | |
| 3559 | wpath = PyUnicode_AsUnicode(po); |
| 3560 | if (wpath == NULL) |
| 3561 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3562 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3563 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3564 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3565 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3566 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3567 | if (!woutbufp) |
| 3568 | return PyErr_NoMemory(); |
| 3569 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3570 | } |
| 3571 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3572 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3573 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3574 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3575 | if (woutbufp != woutbuf) |
| 3576 | free(woutbufp); |
| 3577 | return v; |
| 3578 | } |
| 3579 | /* Drop the argument parsing error as narrow strings |
| 3580 | are also valid. */ |
| 3581 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3582 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3583 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3584 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3585 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3586 | if (win32_warn_bytes_api()) |
| 3587 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3588 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3589 | outbuf, &temp)) { |
| 3590 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3591 | return NULL; |
| 3592 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3593 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3594 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3595 | Py_FileSystemDefaultEncoding, NULL); |
| 3596 | } |
| 3597 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3598 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3599 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3600 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3601 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3602 | /* A helper function for samepath on windows */ |
| 3603 | static PyObject * |
| 3604 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3605 | { |
| 3606 | HANDLE hFile; |
| 3607 | int buf_size; |
| 3608 | wchar_t *target_path; |
| 3609 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3610 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3611 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3612 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3613 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3614 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3615 | path = PyUnicode_AsUnicode(po); |
| 3616 | if (path == NULL) |
| 3617 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3618 | |
| 3619 | if(!check_GetFinalPathNameByHandle()) { |
| 3620 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3621 | NotImplementedError. */ |
| 3622 | return PyErr_Format(PyExc_NotImplementedError, |
| 3623 | "GetFinalPathNameByHandle not available on this platform"); |
| 3624 | } |
| 3625 | |
| 3626 | hFile = CreateFileW( |
| 3627 | path, |
| 3628 | 0, /* desired access */ |
| 3629 | 0, /* share mode */ |
| 3630 | NULL, /* security attributes */ |
| 3631 | OPEN_EXISTING, |
| 3632 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3633 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3634 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3635 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3636 | if(hFile == INVALID_HANDLE_VALUE) |
| 3637 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3638 | |
| 3639 | /* We have a good handle to the target, use it to determine the |
| 3640 | target path name. */ |
| 3641 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3642 | |
| 3643 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3644 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3645 | |
| 3646 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3647 | if(!target_path) |
| 3648 | return PyErr_NoMemory(); |
| 3649 | |
| 3650 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3651 | buf_size, VOLUME_NAME_DOS); |
| 3652 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3653 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3654 | |
| 3655 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3656 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3657 | |
| 3658 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3659 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3660 | free(target_path); |
| 3661 | return result; |
| 3662 | |
| 3663 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3664 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3665 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3666 | "Return true if the pathname refers to an existing directory."); |
| 3667 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3668 | static PyObject * |
| 3669 | posix__isdir(PyObject *self, PyObject *args) |
| 3670 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3671 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3672 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3673 | DWORD attributes; |
| 3674 | |
| 3675 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3676 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3677 | if (wpath == NULL) |
| 3678 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3679 | |
| 3680 | attributes = GetFileAttributesW(wpath); |
| 3681 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3682 | Py_RETURN_FALSE; |
| 3683 | goto check; |
| 3684 | } |
| 3685 | /* Drop the argument parsing error as narrow strings |
| 3686 | are also valid. */ |
| 3687 | PyErr_Clear(); |
| 3688 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3689 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3690 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3691 | if (win32_warn_bytes_api()) |
| 3692 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3693 | attributes = GetFileAttributesA(path); |
| 3694 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3695 | Py_RETURN_FALSE; |
| 3696 | |
| 3697 | check: |
| 3698 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3699 | Py_RETURN_TRUE; |
| 3700 | else |
| 3701 | Py_RETURN_FALSE; |
| 3702 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3703 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3705 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3706 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3707 | Create a directory.\n\ |
| 3708 | \n\ |
| 3709 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3710 | and path should be relative; path will then be relative to that directory.\n\ |
| 3711 | dir_fd may not be implemented on your platform.\n\ |
| 3712 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 3713 | \n\ |
| 3714 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3715 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3716 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3717 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3718 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3719 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3720 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3721 | int dir_fd = DEFAULT_DIR_FD; |
| 3722 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 3723 | PyObject *return_value = NULL; |
| 3724 | int result; |
| 3725 | |
| 3726 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3727 | path.function_name = "mkdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3728 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 3729 | path_converter, &path, &mode, |
| 3730 | #ifdef HAVE_MKDIRAT |
| 3731 | dir_fd_converter, &dir_fd |
| 3732 | #else |
| 3733 | dir_fd_unavailable, &dir_fd |
| 3734 | #endif |
| 3735 | )) |
| 3736 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3737 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3738 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3739 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3740 | if (path.wide) |
| 3741 | result = CreateDirectoryW(path.wide, NULL); |
| 3742 | else |
| 3743 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3744 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3745 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3746 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3747 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3748 | goto exit; |
| 3749 | } |
| 3750 | #else |
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 HAVE_MKDIRAT |
| 3753 | if (dir_fd != DEFAULT_DIR_FD) |
| 3754 | result = mkdirat(dir_fd, path.narrow, mode); |
| 3755 | else |
| 3756 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3757 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3758 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3759 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3760 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3761 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3762 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3763 | if (result < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3764 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3765 | goto exit; |
| 3766 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3767 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3768 | return_value = Py_None; |
| 3769 | Py_INCREF(Py_None); |
| 3770 | exit: |
| 3771 | path_cleanup(&path); |
| 3772 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3775 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3776 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3777 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3778 | #include <sys/resource.h> |
| 3779 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3780 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3781 | |
| 3782 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3783 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3784 | "nice(inc) -> new_priority\n\n\ |
| 3785 | 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] | 3786 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3787 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3788 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3789 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3790 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3791 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3792 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3793 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3794 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3795 | /* There are two flavours of 'nice': one that returns the new |
| 3796 | priority (as required by almost all standards out there) and the |
| 3797 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3798 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3799 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3800 | If we are of the nice family that returns the new priority, we |
| 3801 | need to clear errno before the call, and check if errno is filled |
| 3802 | before calling posix_error() on a returnvalue of -1, because the |
| 3803 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3804 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3805 | errno = 0; |
| 3806 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3807 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3808 | if (value == 0) |
| 3809 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3810 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3811 | if (value == -1 && errno != 0) |
| 3812 | /* either nice() or getpriority() returned an error */ |
| 3813 | return posix_error(); |
| 3814 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3815 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3816 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3817 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3818 | |
| 3819 | #ifdef HAVE_GETPRIORITY |
| 3820 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3821 | "getpriority(which, who) -> current_priority\n\n\ |
| 3822 | Get program scheduling priority."); |
| 3823 | |
| 3824 | static PyObject * |
| 3825 | posix_getpriority(PyObject *self, PyObject *args) |
| 3826 | { |
| 3827 | int which, who, retval; |
| 3828 | |
| 3829 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3830 | return NULL; |
| 3831 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3832 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3833 | if (errno != 0) |
| 3834 | return posix_error(); |
| 3835 | return PyLong_FromLong((long)retval); |
| 3836 | } |
| 3837 | #endif /* HAVE_GETPRIORITY */ |
| 3838 | |
| 3839 | |
| 3840 | #ifdef HAVE_SETPRIORITY |
| 3841 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3842 | "setpriority(which, who, prio) -> None\n\n\ |
| 3843 | Set program scheduling priority."); |
| 3844 | |
| 3845 | static PyObject * |
| 3846 | posix_setpriority(PyObject *self, PyObject *args) |
| 3847 | { |
| 3848 | int which, who, prio, retval; |
| 3849 | |
| 3850 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3851 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3852 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3853 | if (retval == -1) |
| 3854 | return posix_error(); |
| 3855 | Py_RETURN_NONE; |
| 3856 | } |
| 3857 | #endif /* HAVE_SETPRIORITY */ |
| 3858 | |
| 3859 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3860 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3861 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3862 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3863 | char *function_name = is_replace ? "replace" : "rename"; |
| 3864 | path_t src; |
| 3865 | path_t dst; |
| 3866 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3867 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3868 | int dir_fd_specified; |
| 3869 | PyObject *return_value = NULL; |
| 3870 | char format[24]; |
| 3871 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 3872 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3873 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3874 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3875 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3876 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3877 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3878 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3879 | |
| 3880 | memset(&src, 0, sizeof(src)); |
| 3881 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3882 | src.function_name = function_name; |
| 3883 | dst.function_name = function_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3884 | strcpy(format, "O&O&|$O&O&:"); |
| 3885 | strcat(format, function_name); |
| 3886 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 3887 | path_converter, &src, |
| 3888 | path_converter, &dst, |
| 3889 | dir_fd_converter, &src_dir_fd, |
| 3890 | dir_fd_converter, &dst_dir_fd)) |
| 3891 | return NULL; |
| 3892 | |
| 3893 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 3894 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 3895 | #ifndef HAVE_RENAMEAT |
| 3896 | if (dir_fd_specified) { |
| 3897 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 3898 | goto exit; |
| 3899 | } |
| 3900 | #endif |
| 3901 | |
| 3902 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3903 | PyErr_Format(PyExc_ValueError, |
| 3904 | "%s: src and dst must be the same type", function_name); |
| 3905 | goto exit; |
| 3906 | } |
| 3907 | |
| 3908 | #ifdef MS_WINDOWS |
| 3909 | Py_BEGIN_ALLOW_THREADS |
| 3910 | if (src.wide) |
| 3911 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 3912 | else |
| 3913 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 3914 | Py_END_ALLOW_THREADS |
| 3915 | |
| 3916 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 3917 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3918 | goto exit; |
| 3919 | } |
| 3920 | |
| 3921 | #else |
| 3922 | Py_BEGIN_ALLOW_THREADS |
| 3923 | #ifdef HAVE_RENAMEAT |
| 3924 | if (dir_fd_specified) |
| 3925 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 3926 | else |
| 3927 | #endif |
| 3928 | result = rename(src.narrow, dst.narrow); |
| 3929 | Py_END_ALLOW_THREADS |
| 3930 | |
| 3931 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3932 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3933 | goto exit; |
| 3934 | } |
| 3935 | #endif |
| 3936 | |
| 3937 | Py_INCREF(Py_None); |
| 3938 | return_value = Py_None; |
| 3939 | exit: |
| 3940 | path_cleanup(&src); |
| 3941 | path_cleanup(&dst); |
| 3942 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3945 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3946 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 3947 | Rename a file or directory.\n\ |
| 3948 | \n\ |
| 3949 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3950 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3951 | should be relative; the path will then be relative to that directory.\n\ |
| 3952 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 3953 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3954 | |
| 3955 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3956 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3957 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3958 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3962 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 3963 | Rename a file or directory, overwriting the destination.\n\ |
| 3964 | \n\ |
| 3965 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3966 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3967 | should be relative; the path will then be relative to that directory.\n\ |
| 3968 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 3969 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3970 | |
| 3971 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3972 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3973 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3974 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3975 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3976 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3977 | PyDoc_STRVAR(posix_rmdir__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3978 | "rmdir(path, *, dir_fd=None)\n\n\ |
| 3979 | Remove a directory.\n\ |
| 3980 | \n\ |
| 3981 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3982 | and path should be relative; path will then be relative to that directory.\n\ |
| 3983 | dir_fd may not be implemented on your platform.\n\ |
| 3984 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3985 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3986 | static PyObject * |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3987 | posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3988 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3989 | path_t path; |
| 3990 | int dir_fd = DEFAULT_DIR_FD; |
| 3991 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 3992 | int result; |
| 3993 | PyObject *return_value = NULL; |
| 3994 | |
| 3995 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3996 | path.function_name = "rmdir"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3997 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords, |
| 3998 | path_converter, &path, |
| 3999 | #ifdef HAVE_UNLINKAT |
| 4000 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4001 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4002 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4003 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4004 | )) |
| 4005 | return NULL; |
| 4006 | |
| 4007 | Py_BEGIN_ALLOW_THREADS |
| 4008 | #ifdef MS_WINDOWS |
| 4009 | if (path.wide) |
| 4010 | result = RemoveDirectoryW(path.wide); |
| 4011 | else |
| 4012 | result = RemoveDirectoryA(path.narrow); |
| 4013 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4014 | #else |
| 4015 | #ifdef HAVE_UNLINKAT |
| 4016 | if (dir_fd != DEFAULT_DIR_FD) |
| 4017 | result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR); |
| 4018 | else |
| 4019 | #endif |
| 4020 | result = rmdir(path.narrow); |
| 4021 | #endif |
| 4022 | Py_END_ALLOW_THREADS |
| 4023 | |
| 4024 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4025 | return_value = path_error(&path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4026 | goto exit; |
| 4027 | } |
| 4028 | |
| 4029 | return_value = Py_None; |
| 4030 | Py_INCREF(Py_None); |
| 4031 | |
| 4032 | exit: |
| 4033 | path_cleanup(&path); |
| 4034 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4037 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4038 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4039 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4040 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4041 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4042 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4043 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4044 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4045 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4046 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 4047 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4048 | wchar_t *command; |
| 4049 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 4050 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4051 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4052 | Py_BEGIN_ALLOW_THREADS |
| 4053 | sts = _wsystem(command); |
| 4054 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4055 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4056 | PyObject *command_obj; |
| 4057 | char *command; |
| 4058 | if (!PyArg_ParseTuple(args, "O&:system", |
| 4059 | PyUnicode_FSConverter, &command_obj)) |
| 4060 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4061 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4062 | command = PyBytes_AsString(command_obj); |
| 4063 | Py_BEGIN_ALLOW_THREADS |
| 4064 | sts = system(command); |
| 4065 | Py_END_ALLOW_THREADS |
| 4066 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4067 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4068 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4069 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4070 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4071 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4072 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4073 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4074 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4075 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4076 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4077 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4078 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4079 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4080 | int i; |
| 4081 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 4082 | return NULL; |
| 4083 | i = (int)umask(i); |
| 4084 | if (i < 0) |
| 4085 | return posix_error(); |
| 4086 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4089 | #ifdef MS_WINDOWS |
| 4090 | |
| 4091 | /* override the default DeleteFileW behavior so that directory |
| 4092 | symlinks can be removed with this function, the same as with |
| 4093 | Unix symlinks */ |
| 4094 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4095 | { |
| 4096 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4097 | WIN32_FIND_DATAW find_data; |
| 4098 | HANDLE find_data_handle; |
| 4099 | int is_directory = 0; |
| 4100 | int is_link = 0; |
| 4101 | |
| 4102 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4103 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4104 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4105 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4106 | it is a symlink */ |
| 4107 | if(is_directory && |
| 4108 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4109 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4110 | |
| 4111 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 4112 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 4113 | FindClose(find_data_handle); |
| 4114 | } |
| 4115 | } |
| 4116 | } |
| 4117 | |
| 4118 | if (is_directory && is_link) |
| 4119 | return RemoveDirectoryW(lpFileName); |
| 4120 | |
| 4121 | return DeleteFileW(lpFileName); |
| 4122 | } |
| 4123 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4124 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4125 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4126 | "unlink(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4127 | Remove a file (same as remove()).\n\ |
| 4128 | \n\ |
| 4129 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4130 | and path should be relative; path will then be relative to that directory.\n\ |
| 4131 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4132 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4134 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4135 | "remove(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4136 | Remove a file (same as unlink()).\n\ |
| 4137 | \n\ |
| 4138 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4139 | and path should be relative; path will then be relative to that directory.\n\ |
| 4140 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4141 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4142 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4143 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4144 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4145 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4146 | path_t path; |
| 4147 | int dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4148 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4149 | int result; |
| 4150 | PyObject *return_value = NULL; |
| 4151 | |
| 4152 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4153 | path.function_name = "unlink"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4154 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4155 | path_converter, &path, |
| 4156 | #ifdef HAVE_UNLINKAT |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4157 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4158 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4159 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4160 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4161 | )) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4162 | return NULL; |
| 4163 | |
| 4164 | Py_BEGIN_ALLOW_THREADS |
| 4165 | #ifdef MS_WINDOWS |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4166 | if (path.wide) |
| 4167 | result = Py_DeleteFileW(path.wide); |
| 4168 | else |
| 4169 | result = DeleteFileA(path.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4170 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4171 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4172 | #ifdef HAVE_UNLINKAT |
| 4173 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4174 | result = unlinkat(dir_fd, path.narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4175 | else |
| 4176 | #endif /* HAVE_UNLINKAT */ |
| 4177 | result = unlink(path.narrow); |
| 4178 | #endif |
| 4179 | Py_END_ALLOW_THREADS |
| 4180 | |
| 4181 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4182 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4183 | goto exit; |
| 4184 | } |
| 4185 | |
| 4186 | return_value = Py_None; |
| 4187 | Py_INCREF(Py_None); |
| 4188 | |
| 4189 | exit: |
| 4190 | path_cleanup(&path); |
| 4191 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4194 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4195 | PyDoc_STRVAR(posix_uname__doc__, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4196 | "uname() -> uname_result\n\n\ |
| 4197 | Return an object identifying the current operating system.\n\ |
| 4198 | The object behaves like a named tuple with the following fields:\n\ |
| 4199 | (sysname, nodename, release, version, machine)"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4200 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4201 | static PyStructSequence_Field uname_result_fields[] = { |
| 4202 | {"sysname", "operating system name"}, |
| 4203 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4204 | {"release", "operating system release"}, |
| 4205 | {"version", "operating system version"}, |
| 4206 | {"machine", "hardware identifier"}, |
| 4207 | {NULL} |
| 4208 | }; |
| 4209 | |
| 4210 | PyDoc_STRVAR(uname_result__doc__, |
| 4211 | "uname_result: Result from os.uname().\n\n\ |
| 4212 | This object may be accessed either as a tuple of\n\ |
| 4213 | (sysname, nodename, release, version, machine),\n\ |
| 4214 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4215 | \n\ |
| 4216 | See os.uname for more information."); |
| 4217 | |
| 4218 | static PyStructSequence_Desc uname_result_desc = { |
| 4219 | "uname_result", /* name */ |
| 4220 | uname_result__doc__, /* doc */ |
| 4221 | uname_result_fields, |
| 4222 | 5 |
| 4223 | }; |
| 4224 | |
| 4225 | static PyTypeObject UnameResultType; |
| 4226 | |
| 4227 | |
| 4228 | #ifdef HAVE_UNAME |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4229 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4230 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4231 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4232 | struct utsname u; |
| 4233 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4234 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4235 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4236 | Py_BEGIN_ALLOW_THREADS |
| 4237 | res = uname(&u); |
| 4238 | Py_END_ALLOW_THREADS |
| 4239 | if (res < 0) |
| 4240 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4241 | |
| 4242 | value = PyStructSequence_New(&UnameResultType); |
| 4243 | if (value == NULL) |
| 4244 | return NULL; |
| 4245 | |
| 4246 | #define SET(i, field) \ |
| 4247 | { \ |
| 4248 | PyObject *o = PyUnicode_DecodeASCII(field, strlen(field), NULL); \ |
| 4249 | if (!o) { \ |
| 4250 | Py_DECREF(value); \ |
| 4251 | return NULL; \ |
| 4252 | } \ |
| 4253 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4254 | } \ |
| 4255 | |
| 4256 | SET(0, u.sysname); |
| 4257 | SET(1, u.nodename); |
| 4258 | SET(2, u.release); |
| 4259 | SET(3, u.version); |
| 4260 | SET(4, u.machine); |
| 4261 | |
| 4262 | #undef SET |
| 4263 | |
| 4264 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4265 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4266 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4267 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4268 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4269 | PyDoc_STRVAR(posix_utime__doc__, |
| 4270 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4271 | Set the access and modified time of path.\n\ |
| 4272 | \n\ |
| 4273 | path may always be specified as a string.\n\ |
| 4274 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4275 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4276 | \n\ |
| 4277 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4278 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4279 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4280 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4281 | since the epoch.\n\ |
| 4282 | If both times and ns are None, utime uses the current time.\n\ |
| 4283 | Specifying tuples for both times and ns is an error.\n\ |
| 4284 | \n\ |
| 4285 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4286 | and path should be relative; path will then be relative to that directory.\n\ |
| 4287 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4288 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4289 | link points to.\n\ |
| 4290 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4291 | as an open file descriptor.\n\ |
| 4292 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4293 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4294 | |
| 4295 | typedef struct { |
| 4296 | int now; |
| 4297 | time_t atime_s; |
| 4298 | long atime_ns; |
| 4299 | time_t mtime_s; |
| 4300 | long mtime_ns; |
| 4301 | } utime_t; |
| 4302 | |
| 4303 | /* |
| 4304 | * these macros assume that "utime" is a pointer to a utime_t |
| 4305 | * they also intentionally leak the declaration of a pointer named "time" |
| 4306 | */ |
| 4307 | #define UTIME_TO_TIMESPEC \ |
| 4308 | struct timespec ts[2]; \ |
| 4309 | struct timespec *time; \ |
| 4310 | if (utime->now) \ |
| 4311 | time = NULL; \ |
| 4312 | else { \ |
| 4313 | ts[0].tv_sec = utime->atime_s; \ |
| 4314 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4315 | ts[1].tv_sec = utime->mtime_s; \ |
| 4316 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4317 | time = ts; \ |
| 4318 | } \ |
| 4319 | |
| 4320 | #define UTIME_TO_TIMEVAL \ |
| 4321 | struct timeval tv[2]; \ |
| 4322 | struct timeval *time; \ |
| 4323 | if (utime->now) \ |
| 4324 | time = NULL; \ |
| 4325 | else { \ |
| 4326 | tv[0].tv_sec = utime->atime_s; \ |
| 4327 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4328 | tv[1].tv_sec = utime->mtime_s; \ |
| 4329 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4330 | time = tv; \ |
| 4331 | } \ |
| 4332 | |
| 4333 | #define UTIME_TO_UTIMBUF \ |
| 4334 | struct utimbuf u[2]; \ |
| 4335 | struct utimbuf *time; \ |
| 4336 | if (utime->now) \ |
| 4337 | time = NULL; \ |
| 4338 | else { \ |
| 4339 | u.actime = utime->atime_s; \ |
| 4340 | u.modtime = utime->mtime_s; \ |
| 4341 | time = u; \ |
| 4342 | } |
| 4343 | |
| 4344 | #define UTIME_TO_TIME_T \ |
| 4345 | time_t timet[2]; \ |
| 4346 | struct timet time; \ |
| 4347 | if (utime->now) \ |
| 4348 | time = NULL; \ |
| 4349 | else { \ |
| 4350 | timet[0] = utime->atime_s; \ |
| 4351 | timet[1] = utime->mtime_s; \ |
| 4352 | time = &timet; \ |
| 4353 | } \ |
| 4354 | |
| 4355 | |
| 4356 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4357 | |
| 4358 | #if UTIME_HAVE_DIR_FD |
| 4359 | |
| 4360 | static int |
| 4361 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4362 | { |
| 4363 | #ifdef HAVE_UTIMENSAT |
| 4364 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4365 | UTIME_TO_TIMESPEC; |
| 4366 | return utimensat(dir_fd, path, time, flags); |
| 4367 | #elif defined(HAVE_FUTIMESAT) |
| 4368 | UTIME_TO_TIMEVAL; |
| 4369 | /* |
| 4370 | * follow_symlinks will never be false here; |
| 4371 | * we only allow !follow_symlinks and dir_fd together |
| 4372 | * if we have utimensat() |
| 4373 | */ |
| 4374 | assert(follow_symlinks); |
| 4375 | return futimesat(dir_fd, path, time); |
| 4376 | #endif |
| 4377 | } |
| 4378 | |
| 4379 | #endif |
| 4380 | |
| 4381 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4382 | |
| 4383 | #if UTIME_HAVE_FD |
| 4384 | |
| 4385 | static int |
| 4386 | utime_fd(utime_t *utime, int fd) |
| 4387 | { |
| 4388 | #ifdef HAVE_FUTIMENS |
| 4389 | UTIME_TO_TIMESPEC; |
| 4390 | return futimens(fd, time); |
| 4391 | #else |
| 4392 | UTIME_TO_TIMEVAL; |
| 4393 | return futimes(fd, time); |
| 4394 | #endif |
| 4395 | } |
| 4396 | |
| 4397 | #endif |
| 4398 | |
| 4399 | |
| 4400 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4401 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4402 | |
| 4403 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4404 | |
| 4405 | static int |
| 4406 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4407 | { |
| 4408 | #ifdef HAVE_UTIMENSAT |
| 4409 | UTIME_TO_TIMESPEC; |
| 4410 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4411 | #else |
| 4412 | UTIME_TO_TIMEVAL; |
| 4413 | return lutimes(path, time); |
| 4414 | #endif |
| 4415 | } |
| 4416 | |
| 4417 | #endif |
| 4418 | |
| 4419 | #ifndef MS_WINDOWS |
| 4420 | |
| 4421 | static int |
| 4422 | utime_default(utime_t *utime, char *path) |
| 4423 | { |
| 4424 | #ifdef HAVE_UTIMENSAT |
| 4425 | UTIME_TO_TIMESPEC; |
| 4426 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4427 | #elif defined(HAVE_UTIMES) |
| 4428 | UTIME_TO_TIMEVAL; |
| 4429 | return utimes(path, time); |
| 4430 | #elif defined(HAVE_UTIME_H) |
| 4431 | UTIME_TO_UTIMBUF; |
| 4432 | return utime(path, time); |
| 4433 | #else |
| 4434 | UTIME_TO_TIME_T; |
| 4435 | return utime(path, time); |
| 4436 | #endif |
| 4437 | } |
| 4438 | |
| 4439 | #endif |
| 4440 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4441 | static int |
| 4442 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4443 | { |
| 4444 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4445 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4446 | divmod = PyNumber_Divmod(py_long, billion); |
| 4447 | if (!divmod) |
| 4448 | goto exit; |
| 4449 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4450 | if ((*s == -1) && PyErr_Occurred()) |
| 4451 | goto exit; |
| 4452 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4453 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4454 | goto exit; |
| 4455 | |
| 4456 | result = 1; |
| 4457 | exit: |
| 4458 | Py_XDECREF(divmod); |
| 4459 | return result; |
| 4460 | } |
| 4461 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4462 | static PyObject * |
| 4463 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4464 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4465 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4466 | PyObject *times = NULL; |
| 4467 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4468 | int dir_fd = DEFAULT_DIR_FD; |
| 4469 | int follow_symlinks = 1; |
| 4470 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4471 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4472 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4473 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4474 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4475 | #ifdef MS_WINDOWS |
| 4476 | HANDLE hFile; |
| 4477 | FILETIME atime, mtime; |
| 4478 | #else |
| 4479 | int result; |
| 4480 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4481 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4482 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4483 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4484 | memset(&path, 0, sizeof(path)); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4485 | path.function_name = "utime"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4486 | #if UTIME_HAVE_FD |
| 4487 | path.allow_fd = 1; |
| 4488 | #endif |
| 4489 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4490 | "O&|O$OO&p:utime", keywords, |
| 4491 | path_converter, &path, |
| 4492 | ×, &ns, |
| 4493 | #if UTIME_HAVE_DIR_FD |
| 4494 | dir_fd_converter, &dir_fd, |
| 4495 | #else |
| 4496 | dir_fd_unavailable, &dir_fd, |
| 4497 | #endif |
| 4498 | &follow_symlinks |
| 4499 | )) |
| 4500 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4501 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4502 | if (times && (times != Py_None) && ns) { |
| 4503 | PyErr_SetString(PyExc_ValueError, |
| 4504 | "utime: you may specify either 'times'" |
| 4505 | " or 'ns' but not both"); |
| 4506 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | if (times && (times != Py_None)) { |
| 4510 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4511 | PyErr_SetString(PyExc_TypeError, |
| 4512 | "utime: 'times' must be either" |
| 4513 | " a tuple of two ints or None"); |
| 4514 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4515 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4516 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4517 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4518 | &utime.atime_s, &utime.atime_ns) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4519 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4520 | &utime.mtime_s, &utime.mtime_ns) == -1) { |
| 4521 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4522 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4523 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4524 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4525 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4526 | PyErr_SetString(PyExc_TypeError, |
| 4527 | "utime: 'ns' must be a tuple of two ints"); |
| 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 (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4532 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4533 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4534 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4535 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4536 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4537 | } |
| 4538 | else { |
| 4539 | /* times and ns are both None/unspecified. use "now". */ |
| 4540 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4541 | } |
| 4542 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4543 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4544 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4545 | goto exit; |
| 4546 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4547 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4548 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4549 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4550 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4551 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4552 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4553 | #if !defined(HAVE_UTIMENSAT) |
| 4554 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4555 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4556 | "utime: cannot use dir_fd and follow_symlinks " |
| 4557 | "together on this platform"); |
| 4558 | goto exit; |
| 4559 | } |
| 4560 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4561 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4562 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4563 | Py_BEGIN_ALLOW_THREADS |
| 4564 | if (path.wide) |
| 4565 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4566 | NULL, OPEN_EXISTING, |
| 4567 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4568 | else |
| 4569 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4570 | NULL, OPEN_EXISTING, |
| 4571 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4572 | Py_END_ALLOW_THREADS |
| 4573 | if (hFile == INVALID_HANDLE_VALUE) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4574 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4575 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4576 | } |
| 4577 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4578 | if (utime.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4579 | SYSTEMTIME now; |
| 4580 | GetSystemTime(&now); |
| 4581 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 4582 | !SystemTimeToFileTime(&now, &atime)) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4583 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4584 | goto exit; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4585 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4586 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4587 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4588 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4589 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4590 | } |
| 4591 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4592 | /* Avoid putting the file name into the error here, |
| 4593 | as that may confuse the user into believing that |
| 4594 | something is wrong with the file, when it also |
| 4595 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4596 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4597 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4598 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4599 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4600 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4601 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4602 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4603 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4604 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4605 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4606 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4607 | |
| 4608 | #if UTIME_HAVE_DIR_FD |
| 4609 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4610 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4611 | else |
| 4612 | #endif |
| 4613 | |
| 4614 | #if UTIME_HAVE_FD |
| 4615 | if (path.fd != -1) |
| 4616 | result = utime_fd(&utime, path.fd); |
| 4617 | else |
| 4618 | #endif |
| 4619 | |
| 4620 | result = utime_default(&utime, path.narrow); |
| 4621 | |
| 4622 | Py_END_ALLOW_THREADS |
| 4623 | |
| 4624 | if (result < 0) { |
| 4625 | /* see previous comment about not putting filename in error here */ |
| 4626 | return_value = posix_error(); |
| 4627 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4628 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4629 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4630 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4631 | |
| 4632 | Py_INCREF(Py_None); |
| 4633 | return_value = Py_None; |
| 4634 | |
| 4635 | exit: |
| 4636 | path_cleanup(&path); |
| 4637 | #ifdef MS_WINDOWS |
| 4638 | if (hFile != INVALID_HANDLE_VALUE) |
| 4639 | CloseHandle(hFile); |
| 4640 | #endif |
| 4641 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4644 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4645 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4646 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4647 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4648 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4649 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4650 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4651 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4652 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4653 | int sts; |
| 4654 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 4655 | return NULL; |
| 4656 | _exit(sts); |
| 4657 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4660 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4661 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4662 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4663 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4664 | Py_ssize_t i; |
| 4665 | for (i = 0; i < count; i++) |
| 4666 | PyMem_Free(array[i]); |
| 4667 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4668 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4669 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4670 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4671 | int fsconvert_strdup(PyObject *o, char**out) |
| 4672 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4673 | PyObject *bytes; |
| 4674 | Py_ssize_t size; |
| 4675 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4676 | return 0; |
| 4677 | size = PyBytes_GET_SIZE(bytes); |
| 4678 | *out = PyMem_Malloc(size+1); |
| 4679 | if (!*out) |
| 4680 | return 0; |
| 4681 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4682 | Py_DECREF(bytes); |
| 4683 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4684 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4685 | #endif |
| 4686 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4687 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4688 | static char** |
| 4689 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4690 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4691 | char **envlist; |
| 4692 | Py_ssize_t i, pos, envc; |
| 4693 | PyObject *keys=NULL, *vals=NULL; |
| 4694 | PyObject *key, *val, *key2, *val2; |
| 4695 | char *p, *k, *v; |
| 4696 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4697 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4698 | i = PyMapping_Size(env); |
| 4699 | if (i < 0) |
| 4700 | return NULL; |
| 4701 | envlist = PyMem_NEW(char *, i + 1); |
| 4702 | if (envlist == NULL) { |
| 4703 | PyErr_NoMemory(); |
| 4704 | return NULL; |
| 4705 | } |
| 4706 | envc = 0; |
| 4707 | keys = PyMapping_Keys(env); |
| 4708 | vals = PyMapping_Values(env); |
| 4709 | if (!keys || !vals) |
| 4710 | goto error; |
| 4711 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4712 | PyErr_Format(PyExc_TypeError, |
| 4713 | "env.keys() or env.values() is not a list"); |
| 4714 | goto error; |
| 4715 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4716 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4717 | for (pos = 0; pos < i; pos++) { |
| 4718 | key = PyList_GetItem(keys, pos); |
| 4719 | val = PyList_GetItem(vals, pos); |
| 4720 | if (!key || !val) |
| 4721 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4722 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4723 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4724 | goto error; |
| 4725 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4726 | Py_DECREF(key2); |
| 4727 | goto error; |
| 4728 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4729 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4730 | k = PyBytes_AsString(key2); |
| 4731 | v = PyBytes_AsString(val2); |
| 4732 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4733 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4734 | p = PyMem_NEW(char, len); |
| 4735 | if (p == NULL) { |
| 4736 | PyErr_NoMemory(); |
| 4737 | Py_DECREF(key2); |
| 4738 | Py_DECREF(val2); |
| 4739 | goto error; |
| 4740 | } |
| 4741 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4742 | envlist[envc++] = p; |
| 4743 | Py_DECREF(key2); |
| 4744 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4745 | } |
| 4746 | Py_DECREF(vals); |
| 4747 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4748 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4749 | envlist[envc] = 0; |
| 4750 | *envc_ptr = envc; |
| 4751 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4752 | |
| 4753 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4754 | Py_XDECREF(keys); |
| 4755 | Py_XDECREF(vals); |
| 4756 | while (--envc >= 0) |
| 4757 | PyMem_DEL(envlist[envc]); |
| 4758 | PyMem_DEL(envlist); |
| 4759 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4760 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4761 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4762 | static char** |
| 4763 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4764 | { |
| 4765 | int i; |
| 4766 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4767 | if (argvlist == NULL) { |
| 4768 | PyErr_NoMemory(); |
| 4769 | return NULL; |
| 4770 | } |
| 4771 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4772 | PyObject* item = PySequence_ITEM(argv, i); |
| 4773 | if (item == NULL) |
| 4774 | goto fail; |
| 4775 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4776 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4777 | goto fail; |
| 4778 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4779 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4780 | } |
| 4781 | argvlist[*argc] = NULL; |
| 4782 | return argvlist; |
| 4783 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4784 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4785 | free_string_array(argvlist, *argc); |
| 4786 | return NULL; |
| 4787 | } |
| 4788 | #endif |
| 4789 | |
| 4790 | #ifdef HAVE_EXECV |
| 4791 | PyDoc_STRVAR(posix_execv__doc__, |
| 4792 | "execv(path, args)\n\n\ |
| 4793 | Execute an executable path with arguments, replacing current process.\n\ |
| 4794 | \n\ |
| 4795 | path: path of executable file\n\ |
| 4796 | args: tuple or list of strings"); |
| 4797 | |
| 4798 | static PyObject * |
| 4799 | posix_execv(PyObject *self, PyObject *args) |
| 4800 | { |
| 4801 | PyObject *opath; |
| 4802 | char *path; |
| 4803 | PyObject *argv; |
| 4804 | char **argvlist; |
| 4805 | Py_ssize_t argc; |
| 4806 | |
| 4807 | /* execv has two arguments: (path, argv), where |
| 4808 | argv is a list or tuple of strings. */ |
| 4809 | |
| 4810 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4811 | PyUnicode_FSConverter, |
| 4812 | &opath, &argv)) |
| 4813 | return NULL; |
| 4814 | path = PyBytes_AsString(opath); |
| 4815 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4816 | PyErr_SetString(PyExc_TypeError, |
| 4817 | "execv() arg 2 must be a tuple or list"); |
| 4818 | Py_DECREF(opath); |
| 4819 | return NULL; |
| 4820 | } |
| 4821 | argc = PySequence_Size(argv); |
| 4822 | if (argc < 1) { |
| 4823 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4824 | Py_DECREF(opath); |
| 4825 | return NULL; |
| 4826 | } |
| 4827 | |
| 4828 | argvlist = parse_arglist(argv, &argc); |
| 4829 | if (argvlist == NULL) { |
| 4830 | Py_DECREF(opath); |
| 4831 | return NULL; |
| 4832 | } |
| 4833 | |
| 4834 | execv(path, argvlist); |
| 4835 | |
| 4836 | /* If we get here it's definitely an error */ |
| 4837 | |
| 4838 | free_string_array(argvlist, argc); |
| 4839 | Py_DECREF(opath); |
| 4840 | return posix_error(); |
| 4841 | } |
| 4842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4843 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4844 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4845 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4846 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4847 | path: path of executable file\n\ |
| 4848 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4849 | env: dictionary of strings mapping to strings\n\ |
| 4850 | \n\ |
| 4851 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 4852 | execve will execute the program the file descriptor is open to.\n\ |
| 4853 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4854 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4855 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4856 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4857 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4858 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4859 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4860 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4861 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4862 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4863 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4864 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4865 | /* execve has three arguments: (path, argv, env), where |
| 4866 | argv is a list or tuple of strings and env is a dictionary |
| 4867 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4868 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4869 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4870 | path.function_name = "execve"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4871 | #ifdef HAVE_FEXECVE |
| 4872 | path.allow_fd = 1; |
| 4873 | #endif |
| 4874 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 4875 | path_converter, &path, |
| 4876 | &argv, &env |
| 4877 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4878 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4879 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4880 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4881 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4882 | "execve: argv must be a tuple or list"); |
| 4883 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4884 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4885 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4886 | if (!PyMapping_Check(env)) { |
| 4887 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4888 | "execve: environment must be a mapping object"); |
| 4889 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4890 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4891 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4892 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4893 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4894 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4895 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4896 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4897 | envlist = parse_envlist(env, &envc); |
| 4898 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4899 | goto fail; |
| 4900 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4901 | #ifdef HAVE_FEXECVE |
| 4902 | if (path.fd > -1) |
| 4903 | fexecve(path.fd, argvlist, envlist); |
| 4904 | else |
| 4905 | #endif |
| 4906 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4907 | |
| 4908 | /* If we get here it's definitely an error */ |
| 4909 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4910 | path_error(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4911 | |
| 4912 | while (--envc >= 0) |
| 4913 | PyMem_DEL(envlist[envc]); |
| 4914 | PyMem_DEL(envlist); |
| 4915 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4916 | if (argvlist) |
| 4917 | free_string_array(argvlist, argc); |
| 4918 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4919 | return NULL; |
| 4920 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4921 | #endif /* HAVE_EXECV */ |
| 4922 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4923 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4924 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4925 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4926 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4927 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4928 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4929 | mode: mode of process creation\n\ |
| 4930 | path: path of executable file\n\ |
| 4931 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4932 | |
| 4933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4934 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4935 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4936 | PyObject *opath; |
| 4937 | char *path; |
| 4938 | PyObject *argv; |
| 4939 | char **argvlist; |
| 4940 | int mode, i; |
| 4941 | Py_ssize_t argc; |
| 4942 | Py_intptr_t spawnval; |
| 4943 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4944 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4945 | /* spawnv has three arguments: (mode, path, argv), where |
| 4946 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4948 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4949 | PyUnicode_FSConverter, |
| 4950 | &opath, &argv)) |
| 4951 | return NULL; |
| 4952 | path = PyBytes_AsString(opath); |
| 4953 | if (PyList_Check(argv)) { |
| 4954 | argc = PyList_Size(argv); |
| 4955 | getitem = PyList_GetItem; |
| 4956 | } |
| 4957 | else if (PyTuple_Check(argv)) { |
| 4958 | argc = PyTuple_Size(argv); |
| 4959 | getitem = PyTuple_GetItem; |
| 4960 | } |
| 4961 | else { |
| 4962 | PyErr_SetString(PyExc_TypeError, |
| 4963 | "spawnv() arg 2 must be a tuple or list"); |
| 4964 | Py_DECREF(opath); |
| 4965 | return NULL; |
| 4966 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4967 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4968 | argvlist = PyMem_NEW(char *, argc+1); |
| 4969 | if (argvlist == NULL) { |
| 4970 | Py_DECREF(opath); |
| 4971 | return PyErr_NoMemory(); |
| 4972 | } |
| 4973 | for (i = 0; i < argc; i++) { |
| 4974 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4975 | &argvlist[i])) { |
| 4976 | free_string_array(argvlist, i); |
| 4977 | PyErr_SetString( |
| 4978 | PyExc_TypeError, |
| 4979 | "spawnv() arg 2 must contain only strings"); |
| 4980 | Py_DECREF(opath); |
| 4981 | return NULL; |
| 4982 | } |
| 4983 | } |
| 4984 | argvlist[argc] = NULL; |
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 | if (mode == _OLD_P_OVERLAY) |
| 4987 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4988 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4989 | Py_BEGIN_ALLOW_THREADS |
| 4990 | spawnval = _spawnv(mode, path, argvlist); |
| 4991 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4992 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4993 | free_string_array(argvlist, argc); |
| 4994 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4995 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4996 | if (spawnval == -1) |
| 4997 | return posix_error(); |
| 4998 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4999 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5000 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5001 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5002 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5003 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5007 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5008 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5009 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5010 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5011 | mode: mode of process creation\n\ |
| 5012 | path: path of executable file\n\ |
| 5013 | args: tuple or list of arguments\n\ |
| 5014 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5015 | |
| 5016 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5017 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5018 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5019 | PyObject *opath; |
| 5020 | char *path; |
| 5021 | PyObject *argv, *env; |
| 5022 | char **argvlist; |
| 5023 | char **envlist; |
| 5024 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5025 | int mode; |
| 5026 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5027 | Py_intptr_t spawnval; |
| 5028 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5029 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5030 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5031 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5032 | argv is a list or tuple of strings and env is a dictionary |
| 5033 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5034 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5035 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 5036 | PyUnicode_FSConverter, |
| 5037 | &opath, &argv, &env)) |
| 5038 | return NULL; |
| 5039 | path = PyBytes_AsString(opath); |
| 5040 | if (PyList_Check(argv)) { |
| 5041 | argc = PyList_Size(argv); |
| 5042 | getitem = PyList_GetItem; |
| 5043 | } |
| 5044 | else if (PyTuple_Check(argv)) { |
| 5045 | argc = PyTuple_Size(argv); |
| 5046 | getitem = PyTuple_GetItem; |
| 5047 | } |
| 5048 | else { |
| 5049 | PyErr_SetString(PyExc_TypeError, |
| 5050 | "spawnve() arg 2 must be a tuple or list"); |
| 5051 | goto fail_0; |
| 5052 | } |
| 5053 | if (!PyMapping_Check(env)) { |
| 5054 | PyErr_SetString(PyExc_TypeError, |
| 5055 | "spawnve() arg 3 must be a mapping object"); |
| 5056 | goto fail_0; |
| 5057 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5058 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5059 | argvlist = PyMem_NEW(char *, argc+1); |
| 5060 | if (argvlist == NULL) { |
| 5061 | PyErr_NoMemory(); |
| 5062 | goto fail_0; |
| 5063 | } |
| 5064 | for (i = 0; i < argc; i++) { |
| 5065 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5066 | &argvlist[i])) |
| 5067 | { |
| 5068 | lastarg = i; |
| 5069 | goto fail_1; |
| 5070 | } |
| 5071 | } |
| 5072 | lastarg = argc; |
| 5073 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5074 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5075 | envlist = parse_envlist(env, &envc); |
| 5076 | if (envlist == NULL) |
| 5077 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5078 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5079 | if (mode == _OLD_P_OVERLAY) |
| 5080 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5081 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5082 | Py_BEGIN_ALLOW_THREADS |
| 5083 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 5084 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5085 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5086 | if (spawnval == -1) |
| 5087 | (void) posix_error(); |
| 5088 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 5089 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5090 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5091 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5092 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5093 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5094 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5095 | while (--envc >= 0) |
| 5096 | PyMem_DEL(envlist[envc]); |
| 5097 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5098 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5099 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5100 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5101 | Py_DECREF(opath); |
| 5102 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5103 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5104 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5105 | #endif /* HAVE_SPAWNV */ |
| 5106 | |
| 5107 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5108 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5109 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5110 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5111 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 5112 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5113 | 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] | 5114 | |
| 5115 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5116 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5117 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5118 | pid_t pid; |
| 5119 | int result = 0; |
| 5120 | _PyImport_AcquireLock(); |
| 5121 | pid = fork1(); |
| 5122 | if (pid == 0) { |
| 5123 | /* child: this clobbers and resets the import lock. */ |
| 5124 | PyOS_AfterFork(); |
| 5125 | } else { |
| 5126 | /* parent: release the import lock. */ |
| 5127 | result = _PyImport_ReleaseLock(); |
| 5128 | } |
| 5129 | if (pid == -1) |
| 5130 | return posix_error(); |
| 5131 | if (result < 0) { |
| 5132 | /* Don't clobber the OSError if the fork failed. */ |
| 5133 | PyErr_SetString(PyExc_RuntimeError, |
| 5134 | "not holding the import lock"); |
| 5135 | return NULL; |
| 5136 | } |
| 5137 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5138 | } |
| 5139 | #endif |
| 5140 | |
| 5141 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5142 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5143 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5144 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5145 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5146 | 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] | 5147 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5148 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5149 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5150 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5151 | pid_t pid; |
| 5152 | int result = 0; |
| 5153 | _PyImport_AcquireLock(); |
| 5154 | pid = fork(); |
| 5155 | if (pid == 0) { |
| 5156 | /* child: this clobbers and resets the import lock. */ |
| 5157 | PyOS_AfterFork(); |
| 5158 | } else { |
| 5159 | /* parent: release the import lock. */ |
| 5160 | result = _PyImport_ReleaseLock(); |
| 5161 | } |
| 5162 | if (pid == -1) |
| 5163 | return posix_error(); |
| 5164 | if (result < 0) { |
| 5165 | /* Don't clobber the OSError if the fork failed. */ |
| 5166 | PyErr_SetString(PyExc_RuntimeError, |
| 5167 | "not holding the import lock"); |
| 5168 | return NULL; |
| 5169 | } |
| 5170 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5171 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5172 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5173 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5174 | #ifdef HAVE_SCHED_H |
| 5175 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5176 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5177 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5178 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5179 | "sched_get_priority_max(policy)\n\n\ |
| 5180 | Get the maximum scheduling priority for *policy*."); |
| 5181 | |
| 5182 | static PyObject * |
| 5183 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5184 | { |
| 5185 | int policy, max; |
| 5186 | |
| 5187 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5188 | return NULL; |
| 5189 | max = sched_get_priority_max(policy); |
| 5190 | if (max < 0) |
| 5191 | return posix_error(); |
| 5192 | return PyLong_FromLong(max); |
| 5193 | } |
| 5194 | |
| 5195 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5196 | "sched_get_priority_min(policy)\n\n\ |
| 5197 | Get the minimum scheduling priority for *policy*."); |
| 5198 | |
| 5199 | static PyObject * |
| 5200 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5201 | { |
| 5202 | int policy, min; |
| 5203 | |
| 5204 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5205 | return NULL; |
| 5206 | min = sched_get_priority_min(policy); |
| 5207 | if (min < 0) |
| 5208 | return posix_error(); |
| 5209 | return PyLong_FromLong(min); |
| 5210 | } |
| 5211 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5212 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5213 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5214 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5215 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5216 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5217 | "sched_getscheduler(pid)\n\n\ |
| 5218 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5219 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5220 | |
| 5221 | static PyObject * |
| 5222 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5223 | { |
| 5224 | pid_t pid; |
| 5225 | int policy; |
| 5226 | |
| 5227 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5228 | return NULL; |
| 5229 | policy = sched_getscheduler(pid); |
| 5230 | if (policy < 0) |
| 5231 | return posix_error(); |
| 5232 | return PyLong_FromLong(policy); |
| 5233 | } |
| 5234 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5235 | #endif |
| 5236 | |
| 5237 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5238 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5239 | static PyObject * |
| 5240 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5241 | { |
| 5242 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5243 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5244 | |
| 5245 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5246 | return NULL; |
| 5247 | res = PyStructSequence_New(type); |
| 5248 | if (!res) |
| 5249 | return NULL; |
| 5250 | Py_INCREF(priority); |
| 5251 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5252 | return res; |
| 5253 | } |
| 5254 | |
| 5255 | PyDoc_STRVAR(sched_param__doc__, |
| 5256 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5257 | Current has only one field: sched_priority"); |
| 5258 | |
| 5259 | static PyStructSequence_Field sched_param_fields[] = { |
| 5260 | {"sched_priority", "the scheduling priority"}, |
| 5261 | {0} |
| 5262 | }; |
| 5263 | |
| 5264 | static PyStructSequence_Desc sched_param_desc = { |
| 5265 | "sched_param", /* name */ |
| 5266 | sched_param__doc__, /* doc */ |
| 5267 | sched_param_fields, |
| 5268 | 1 |
| 5269 | }; |
| 5270 | |
| 5271 | static int |
| 5272 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5273 | { |
| 5274 | long priority; |
| 5275 | |
| 5276 | if (Py_TYPE(param) != &SchedParamType) { |
| 5277 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5278 | return 0; |
| 5279 | } |
| 5280 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5281 | if (priority == -1 && PyErr_Occurred()) |
| 5282 | return 0; |
| 5283 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5284 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5285 | return 0; |
| 5286 | } |
| 5287 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5288 | return 1; |
| 5289 | } |
| 5290 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5291 | #endif |
| 5292 | |
| 5293 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5294 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5295 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5296 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5297 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5298 | If *pid* is 0, the calling process is changed.\n\ |
| 5299 | *param* is an instance of sched_param."); |
| 5300 | |
| 5301 | static PyObject * |
| 5302 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5303 | { |
| 5304 | pid_t pid; |
| 5305 | int policy; |
| 5306 | struct sched_param param; |
| 5307 | |
| 5308 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5309 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5310 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5311 | |
| 5312 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5313 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5314 | ** scheduling policy under Solaris/Illumos, and others. |
| 5315 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5316 | */ |
| 5317 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5318 | return posix_error(); |
| 5319 | Py_RETURN_NONE; |
| 5320 | } |
| 5321 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5322 | #endif |
| 5323 | |
| 5324 | #ifdef HAVE_SCHED_SETPARAM |
| 5325 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5326 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5327 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5328 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5329 | sched_param class. A PID of 0 means the calling process."); |
| 5330 | |
| 5331 | static PyObject * |
| 5332 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5333 | { |
| 5334 | pid_t pid; |
| 5335 | struct sched_param param; |
| 5336 | PyObject *res, *priority; |
| 5337 | |
| 5338 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5339 | return NULL; |
| 5340 | if (sched_getparam(pid, ¶m)) |
| 5341 | return posix_error(); |
| 5342 | res = PyStructSequence_New(&SchedParamType); |
| 5343 | if (!res) |
| 5344 | return NULL; |
| 5345 | priority = PyLong_FromLong(param.sched_priority); |
| 5346 | if (!priority) { |
| 5347 | Py_DECREF(res); |
| 5348 | return NULL; |
| 5349 | } |
| 5350 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5351 | return res; |
| 5352 | } |
| 5353 | |
| 5354 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5355 | "sched_setparam(pid, param)\n\n\ |
| 5356 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5357 | A PID of 0 means the calling process."); |
| 5358 | |
| 5359 | static PyObject * |
| 5360 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5361 | { |
| 5362 | pid_t pid; |
| 5363 | struct sched_param param; |
| 5364 | |
| 5365 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5366 | &pid, &convert_sched_param, ¶m)) |
| 5367 | return NULL; |
| 5368 | if (sched_setparam(pid, ¶m)) |
| 5369 | return posix_error(); |
| 5370 | Py_RETURN_NONE; |
| 5371 | } |
| 5372 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5373 | #endif |
| 5374 | |
| 5375 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5376 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5377 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5378 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5379 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5380 | |
| 5381 | static PyObject * |
| 5382 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5383 | { |
| 5384 | pid_t pid; |
| 5385 | struct timespec interval; |
| 5386 | |
| 5387 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5388 | return NULL; |
| 5389 | if (sched_rr_get_interval(pid, &interval)) |
| 5390 | return posix_error(); |
| 5391 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5392 | } |
| 5393 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5394 | #endif |
| 5395 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5396 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5397 | "sched_yield()\n\n\ |
| 5398 | Voluntarily relinquish the CPU."); |
| 5399 | |
| 5400 | static PyObject * |
| 5401 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5402 | { |
| 5403 | if (sched_yield()) |
| 5404 | return posix_error(); |
| 5405 | Py_RETURN_NONE; |
| 5406 | } |
| 5407 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5408 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5409 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5410 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5411 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5412 | |
| 5413 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5414 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5415 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5416 | |
| 5417 | static PyObject * |
| 5418 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5419 | { |
| 5420 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5421 | int ncpus; |
| 5422 | size_t setsize; |
| 5423 | cpu_set_t *mask = NULL; |
| 5424 | PyObject *iterable, *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5425 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5426 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity", |
| 5427 | &pid, &iterable)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5428 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5429 | |
| 5430 | iterator = PyObject_GetIter(iterable); |
| 5431 | if (iterator == NULL) |
| 5432 | return NULL; |
| 5433 | |
| 5434 | ncpus = NCPUS_START; |
| 5435 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5436 | mask = CPU_ALLOC(ncpus); |
| 5437 | if (mask == NULL) { |
| 5438 | PyErr_NoMemory(); |
| 5439 | goto error; |
| 5440 | } |
| 5441 | CPU_ZERO_S(setsize, mask); |
| 5442 | |
| 5443 | while ((item = PyIter_Next(iterator))) { |
| 5444 | long cpu; |
| 5445 | if (!PyLong_Check(item)) { |
| 5446 | PyErr_Format(PyExc_TypeError, |
| 5447 | "expected an iterator of ints, " |
| 5448 | "but iterator yielded %R", |
| 5449 | Py_TYPE(item)); |
| 5450 | Py_DECREF(item); |
| 5451 | goto error; |
| 5452 | } |
| 5453 | cpu = PyLong_AsLong(item); |
| 5454 | Py_DECREF(item); |
| 5455 | if (cpu < 0) { |
| 5456 | if (!PyErr_Occurred()) |
| 5457 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5458 | goto error; |
| 5459 | } |
| 5460 | if (cpu > INT_MAX - 1) { |
| 5461 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5462 | goto error; |
| 5463 | } |
| 5464 | if (cpu >= ncpus) { |
| 5465 | /* Grow CPU mask to fit the CPU number */ |
| 5466 | int newncpus = ncpus; |
| 5467 | cpu_set_t *newmask; |
| 5468 | size_t newsetsize; |
| 5469 | while (newncpus <= cpu) { |
| 5470 | if (newncpus > INT_MAX / 2) |
| 5471 | newncpus = cpu + 1; |
| 5472 | else |
| 5473 | newncpus = newncpus * 2; |
| 5474 | } |
| 5475 | newmask = CPU_ALLOC(newncpus); |
| 5476 | if (newmask == NULL) { |
| 5477 | PyErr_NoMemory(); |
| 5478 | goto error; |
| 5479 | } |
| 5480 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5481 | CPU_ZERO_S(newsetsize, newmask); |
| 5482 | memcpy(newmask, mask, setsize); |
| 5483 | CPU_FREE(mask); |
| 5484 | setsize = newsetsize; |
| 5485 | mask = newmask; |
| 5486 | ncpus = newncpus; |
| 5487 | } |
| 5488 | CPU_SET_S(cpu, setsize, mask); |
| 5489 | } |
| 5490 | Py_CLEAR(iterator); |
| 5491 | |
| 5492 | if (sched_setaffinity(pid, setsize, mask)) { |
| 5493 | posix_error(); |
| 5494 | goto error; |
| 5495 | } |
| 5496 | CPU_FREE(mask); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5497 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5498 | |
| 5499 | error: |
| 5500 | if (mask) |
| 5501 | CPU_FREE(mask); |
| 5502 | Py_XDECREF(iterator); |
| 5503 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5507 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5508 | Return the affinity of the process with PID *pid*.\n\ |
| 5509 | The returned cpu_set will be of size *ncpus*."); |
| 5510 | |
| 5511 | static PyObject * |
| 5512 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5513 | { |
| 5514 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5515 | int cpu, ncpus, count; |
| 5516 | size_t setsize; |
| 5517 | cpu_set_t *mask = NULL; |
| 5518 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5519 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5520 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity", |
| 5521 | &pid)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5522 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5523 | |
| 5524 | ncpus = NCPUS_START; |
| 5525 | while (1) { |
| 5526 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5527 | mask = CPU_ALLOC(ncpus); |
| 5528 | if (mask == NULL) |
| 5529 | return PyErr_NoMemory(); |
| 5530 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5531 | break; |
| 5532 | CPU_FREE(mask); |
| 5533 | if (errno != EINVAL) |
| 5534 | return posix_error(); |
| 5535 | if (ncpus > INT_MAX / 2) { |
| 5536 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5537 | "a large enough CPU set"); |
| 5538 | return NULL; |
| 5539 | } |
| 5540 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5541 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5542 | |
| 5543 | res = PySet_New(NULL); |
| 5544 | if (res == NULL) |
| 5545 | goto error; |
| 5546 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5547 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5548 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5549 | --count; |
| 5550 | if (cpu_num == NULL) |
| 5551 | goto error; |
| 5552 | if (PySet_Add(res, cpu_num)) { |
| 5553 | Py_DECREF(cpu_num); |
| 5554 | goto error; |
| 5555 | } |
| 5556 | Py_DECREF(cpu_num); |
| 5557 | } |
| 5558 | } |
| 5559 | CPU_FREE(mask); |
| 5560 | return res; |
| 5561 | |
| 5562 | error: |
| 5563 | if (mask) |
| 5564 | CPU_FREE(mask); |
| 5565 | Py_XDECREF(res); |
| 5566 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5567 | } |
| 5568 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5569 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5570 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5571 | #endif /* HAVE_SCHED_H */ |
| 5572 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5573 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5574 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5575 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5576 | #define DEV_PTY_FILE "/dev/ptc" |
| 5577 | #define HAVE_DEV_PTMX |
| 5578 | #else |
| 5579 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5580 | #endif |
| 5581 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5582 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5583 | #ifdef HAVE_PTY_H |
| 5584 | #include <pty.h> |
| 5585 | #else |
| 5586 | #ifdef HAVE_LIBUTIL_H |
| 5587 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5588 | #else |
| 5589 | #ifdef HAVE_UTIL_H |
| 5590 | #include <util.h> |
| 5591 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5592 | #endif /* HAVE_LIBUTIL_H */ |
| 5593 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5594 | #ifdef HAVE_STROPTS_H |
| 5595 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5596 | #endif |
| 5597 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5598 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5599 | #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] | 5600 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5601 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5602 | 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] | 5603 | |
| 5604 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5605 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5606 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5607 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5608 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5609 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5610 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5611 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5612 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5613 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5614 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5615 | #endif |
| 5616 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5617 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5618 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5619 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5620 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5621 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5622 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5623 | if (slave_name == NULL) |
| 5624 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5625 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5626 | slave_fd = open(slave_name, O_RDWR); |
| 5627 | if (slave_fd < 0) |
| 5628 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5629 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5630 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5631 | if (master_fd < 0) |
| 5632 | return posix_error(); |
| 5633 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5634 | /* change permission of slave */ |
| 5635 | if (grantpt(master_fd) < 0) { |
| 5636 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5637 | return posix_error(); |
| 5638 | } |
| 5639 | /* unlock slave */ |
| 5640 | if (unlockpt(master_fd) < 0) { |
| 5641 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5642 | return posix_error(); |
| 5643 | } |
| 5644 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5645 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5646 | if (slave_name == NULL) |
| 5647 | return posix_error(); |
| 5648 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5649 | if (slave_fd < 0) |
| 5650 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5651 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5652 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5653 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5654 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5655 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5656 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5657 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5658 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5659 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5660 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5661 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5662 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5663 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5664 | |
| 5665 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5666 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5667 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5668 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5669 | 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] | 5670 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5671 | |
| 5672 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5673 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5674 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5675 | int master_fd = -1, result = 0; |
| 5676 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5677 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5678 | _PyImport_AcquireLock(); |
| 5679 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5680 | if (pid == 0) { |
| 5681 | /* child: this clobbers and resets the import lock. */ |
| 5682 | PyOS_AfterFork(); |
| 5683 | } else { |
| 5684 | /* parent: release the import lock. */ |
| 5685 | result = _PyImport_ReleaseLock(); |
| 5686 | } |
| 5687 | if (pid == -1) |
| 5688 | return posix_error(); |
| 5689 | if (result < 0) { |
| 5690 | /* Don't clobber the OSError if the fork failed. */ |
| 5691 | PyErr_SetString(PyExc_RuntimeError, |
| 5692 | "not holding the import lock"); |
| 5693 | return NULL; |
| 5694 | } |
| 5695 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5696 | } |
| 5697 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5698 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5699 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5700 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5701 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5702 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5703 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5704 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5705 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5706 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5707 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5708 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5709 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5710 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5711 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5712 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5713 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5714 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5715 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5716 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5717 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5718 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5719 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5720 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5721 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5722 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5723 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5724 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5725 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5726 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5727 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5728 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5729 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5730 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5731 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5732 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5733 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5734 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5735 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5736 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5737 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5738 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5739 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5740 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5741 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5742 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5743 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5744 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5745 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5746 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5747 | } |
| 5748 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5749 | #ifdef HAVE_GETGROUPLIST |
| 5750 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5751 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5752 | Returns a list of groups to which a user belongs.\n\n\ |
| 5753 | user: username to lookup\n\ |
| 5754 | group: base group id of the user"); |
| 5755 | |
| 5756 | static PyObject * |
| 5757 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5758 | { |
| 5759 | #ifdef NGROUPS_MAX |
| 5760 | #define MAX_GROUPS NGROUPS_MAX |
| 5761 | #else |
| 5762 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5763 | #define MAX_GROUPS 64 |
| 5764 | #endif |
| 5765 | |
| 5766 | const char *user; |
| 5767 | int i, ngroups; |
| 5768 | PyObject *list; |
| 5769 | #ifdef __APPLE__ |
| 5770 | int *groups, basegid; |
| 5771 | #else |
| 5772 | gid_t *groups, basegid; |
| 5773 | #endif |
| 5774 | ngroups = MAX_GROUPS; |
| 5775 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5776 | #ifdef __APPLE__ |
| 5777 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5778 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5779 | #else |
| 5780 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 5781 | _Py_Gid_Converter, &basegid)) |
| 5782 | return NULL; |
| 5783 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5784 | |
| 5785 | #ifdef __APPLE__ |
| 5786 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5787 | #else |
| 5788 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5789 | #endif |
| 5790 | if (groups == NULL) |
| 5791 | return PyErr_NoMemory(); |
| 5792 | |
| 5793 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5794 | PyMem_Del(groups); |
| 5795 | return posix_error(); |
| 5796 | } |
| 5797 | |
| 5798 | list = PyList_New(ngroups); |
| 5799 | if (list == NULL) { |
| 5800 | PyMem_Del(groups); |
| 5801 | return NULL; |
| 5802 | } |
| 5803 | |
| 5804 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5805 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5806 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5807 | #else |
| 5808 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 5809 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5810 | if (o == NULL) { |
| 5811 | Py_DECREF(list); |
| 5812 | PyMem_Del(groups); |
| 5813 | return NULL; |
| 5814 | } |
| 5815 | PyList_SET_ITEM(list, i, o); |
| 5816 | } |
| 5817 | |
| 5818 | PyMem_Del(groups); |
| 5819 | |
| 5820 | return list; |
| 5821 | } |
| 5822 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5823 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5824 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5825 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5826 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5827 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5828 | |
| 5829 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5830 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5831 | { |
| 5832 | PyObject *result = NULL; |
| 5833 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5834 | #ifdef NGROUPS_MAX |
| 5835 | #define MAX_GROUPS NGROUPS_MAX |
| 5836 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5837 | /* 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] | 5838 | #define MAX_GROUPS 64 |
| 5839 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5840 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5841 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5842 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5843 | * This is a helper variable to store the intermediate result when |
| 5844 | * that happens. |
| 5845 | * |
| 5846 | * To keep the code readable the OSX behaviour is unconditional, |
| 5847 | * according to the POSIX spec this should be safe on all unix-y |
| 5848 | * systems. |
| 5849 | */ |
| 5850 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5851 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5852 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5853 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5854 | if (n < 0) { |
| 5855 | if (errno == EINVAL) { |
| 5856 | n = getgroups(0, NULL); |
| 5857 | if (n == -1) { |
| 5858 | return posix_error(); |
| 5859 | } |
| 5860 | if (n == 0) { |
| 5861 | /* Avoid malloc(0) */ |
| 5862 | alt_grouplist = grouplist; |
| 5863 | } else { |
| 5864 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5865 | if (alt_grouplist == NULL) { |
| 5866 | errno = EINVAL; |
| 5867 | return posix_error(); |
| 5868 | } |
| 5869 | n = getgroups(n, alt_grouplist); |
| 5870 | if (n == -1) { |
| 5871 | PyMem_Free(alt_grouplist); |
| 5872 | return posix_error(); |
| 5873 | } |
| 5874 | } |
| 5875 | } else { |
| 5876 | return posix_error(); |
| 5877 | } |
| 5878 | } |
| 5879 | result = PyList_New(n); |
| 5880 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5881 | int i; |
| 5882 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5883 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5884 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5885 | Py_DECREF(result); |
| 5886 | result = NULL; |
| 5887 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5888 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5889 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5890 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5891 | } |
| 5892 | |
| 5893 | if (alt_grouplist != grouplist) { |
| 5894 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5895 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5896 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5897 | return result; |
| 5898 | } |
| 5899 | #endif |
| 5900 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5901 | #ifdef HAVE_INITGROUPS |
| 5902 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5903 | "initgroups(username, gid) -> None\n\n\ |
| 5904 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5905 | the groups of which the specified username is a member, plus the specified\n\ |
| 5906 | group id."); |
| 5907 | |
| 5908 | static PyObject * |
| 5909 | posix_initgroups(PyObject *self, PyObject *args) |
| 5910 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5911 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5912 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5913 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5914 | #ifdef __APPLE__ |
| 5915 | int gid; |
| 5916 | #else |
| 5917 | gid_t gid; |
| 5918 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5919 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5920 | #ifdef __APPLE__ |
| 5921 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 5922 | PyUnicode_FSConverter, &oname, |
| 5923 | &gid)) |
| 5924 | #else |
| 5925 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 5926 | PyUnicode_FSConverter, &oname, |
| 5927 | _Py_Gid_Converter, &gid)) |
| 5928 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5929 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5930 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5931 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5932 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5933 | Py_DECREF(oname); |
| 5934 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5935 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5936 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5937 | Py_INCREF(Py_None); |
| 5938 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5939 | } |
| 5940 | #endif |
| 5941 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5942 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5943 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5944 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5945 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5946 | |
| 5947 | static PyObject * |
| 5948 | posix_getpgid(PyObject *self, PyObject *args) |
| 5949 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5950 | pid_t pid, pgid; |
| 5951 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5952 | return NULL; |
| 5953 | pgid = getpgid(pid); |
| 5954 | if (pgid < 0) |
| 5955 | return posix_error(); |
| 5956 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5957 | } |
| 5958 | #endif /* HAVE_GETPGID */ |
| 5959 | |
| 5960 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5961 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5962 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5963 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5964 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5965 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5966 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5967 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5968 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5969 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5970 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5971 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5972 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5973 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5974 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5975 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5976 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5977 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5978 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5979 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5980 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5981 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5982 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5983 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5984 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5985 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5986 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5987 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5988 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5989 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5990 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5991 | return posix_error(); |
| 5992 | Py_INCREF(Py_None); |
| 5993 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5994 | } |
| 5995 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5996 | #endif /* HAVE_SETPGRP */ |
| 5997 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5998 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5999 | |
| 6000 | #ifdef MS_WINDOWS |
| 6001 | #include <tlhelp32.h> |
| 6002 | |
| 6003 | static PyObject* |
| 6004 | win32_getppid() |
| 6005 | { |
| 6006 | HANDLE snapshot; |
| 6007 | pid_t mypid; |
| 6008 | PyObject* result = NULL; |
| 6009 | BOOL have_record; |
| 6010 | PROCESSENTRY32 pe; |
| 6011 | |
| 6012 | mypid = getpid(); /* This function never fails */ |
| 6013 | |
| 6014 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6015 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6016 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6017 | |
| 6018 | pe.dwSize = sizeof(pe); |
| 6019 | have_record = Process32First(snapshot, &pe); |
| 6020 | while (have_record) { |
| 6021 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6022 | /* We could cache the ulong value in a static variable. */ |
| 6023 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6024 | break; |
| 6025 | } |
| 6026 | |
| 6027 | have_record = Process32Next(snapshot, &pe); |
| 6028 | } |
| 6029 | |
| 6030 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6031 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6032 | * error anyway, so let's raise it. */ |
| 6033 | if (!result) |
| 6034 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6035 | |
| 6036 | CloseHandle(snapshot); |
| 6037 | |
| 6038 | return result; |
| 6039 | } |
| 6040 | #endif /*MS_WINDOWS*/ |
| 6041 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6042 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6043 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6044 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6045 | Windows machines will still return its id; others systems will return the id\n\ |
| 6046 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6047 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6048 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6049 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6050 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6051 | #ifdef MS_WINDOWS |
| 6052 | return win32_getppid(); |
| 6053 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6054 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6055 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6056 | } |
| 6057 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6058 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6059 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6060 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6061 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6062 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6063 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6064 | |
| 6065 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6066 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6067 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6068 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6069 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6070 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6071 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6072 | |
| 6073 | if (GetUserNameW(user_name, &num_chars)) { |
| 6074 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6075 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6076 | } |
| 6077 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6078 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6079 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6080 | char *name; |
| 6081 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6082 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6083 | errno = 0; |
| 6084 | name = getlogin(); |
| 6085 | if (name == NULL) { |
| 6086 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6087 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6088 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6089 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6090 | } |
| 6091 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6092 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6093 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6094 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6095 | return result; |
| 6096 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6097 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6098 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6099 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6100 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6101 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6102 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6103 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6104 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6105 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6106 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6107 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6108 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6109 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6110 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6111 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6112 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6113 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6114 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6115 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6116 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6117 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6118 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6119 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6120 | pid_t pid; |
| 6121 | int sig; |
| 6122 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6123 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6124 | if (kill(pid, sig) == -1) |
| 6125 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6126 | Py_INCREF(Py_None); |
| 6127 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6128 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6129 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6130 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6131 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6132 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6133 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6134 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6135 | |
| 6136 | static PyObject * |
| 6137 | posix_killpg(PyObject *self, PyObject *args) |
| 6138 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6139 | int sig; |
| 6140 | pid_t pgid; |
| 6141 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6142 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6143 | take the same type. Moreover, pid_t is always at least as wide as |
| 6144 | int (else compilation of this module fails), which is safe. */ |
| 6145 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6146 | return NULL; |
| 6147 | if (killpg(pgid, sig) == -1) |
| 6148 | return posix_error(); |
| 6149 | Py_INCREF(Py_None); |
| 6150 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6151 | } |
| 6152 | #endif |
| 6153 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6154 | #ifdef MS_WINDOWS |
| 6155 | PyDoc_STRVAR(win32_kill__doc__, |
| 6156 | "kill(pid, sig)\n\n\ |
| 6157 | Kill a process with a signal."); |
| 6158 | |
| 6159 | static PyObject * |
| 6160 | win32_kill(PyObject *self, PyObject *args) |
| 6161 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6162 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6163 | DWORD pid, sig, err; |
| 6164 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6165 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6166 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 6167 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6168 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6169 | /* Console processes which share a common console can be sent CTRL+C or |
| 6170 | CTRL+BREAK events, provided they handle said events. */ |
| 6171 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 6172 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 6173 | err = GetLastError(); |
| 6174 | PyErr_SetFromWindowsErr(err); |
| 6175 | } |
| 6176 | else |
| 6177 | Py_RETURN_NONE; |
| 6178 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6179 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6180 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6181 | attempt to open and terminate the process. */ |
| 6182 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 6183 | if (handle == NULL) { |
| 6184 | err = GetLastError(); |
| 6185 | return PyErr_SetFromWindowsErr(err); |
| 6186 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6187 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6188 | if (TerminateProcess(handle, sig) == 0) { |
| 6189 | err = GetLastError(); |
| 6190 | result = PyErr_SetFromWindowsErr(err); |
| 6191 | } else { |
| 6192 | Py_INCREF(Py_None); |
| 6193 | result = Py_None; |
| 6194 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6195 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6196 | CloseHandle(handle); |
| 6197 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6198 | } |
| 6199 | #endif /* MS_WINDOWS */ |
| 6200 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6201 | #ifdef HAVE_PLOCK |
| 6202 | |
| 6203 | #ifdef HAVE_SYS_LOCK_H |
| 6204 | #include <sys/lock.h> |
| 6205 | #endif |
| 6206 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6207 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6208 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6209 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6210 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6211 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6212 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6213 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6214 | int op; |
| 6215 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6216 | return NULL; |
| 6217 | if (plock(op) == -1) |
| 6218 | return posix_error(); |
| 6219 | Py_INCREF(Py_None); |
| 6220 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6221 | } |
| 6222 | #endif |
| 6223 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6224 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6225 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6226 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6227 | Set the current process's user id."); |
| 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_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6231 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6232 | uid_t uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6233 | if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6234 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6235 | if (setuid(uid) < 0) |
| 6236 | return posix_error(); |
| 6237 | Py_INCREF(Py_None); |
| 6238 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6239 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6240 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6241 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6242 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6243 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6244 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6245 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6246 | Set the current process's effective user id."); |
| 6247 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6248 | static PyObject * |
| 6249 | posix_seteuid (PyObject *self, PyObject *args) |
| 6250 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6251 | uid_t euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6252 | if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6253 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6254 | if (seteuid(euid) < 0) { |
| 6255 | return posix_error(); |
| 6256 | } else { |
| 6257 | Py_INCREF(Py_None); |
| 6258 | return Py_None; |
| 6259 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6260 | } |
| 6261 | #endif /* HAVE_SETEUID */ |
| 6262 | |
| 6263 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6264 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6265 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6266 | Set the current process's effective group id."); |
| 6267 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6268 | static PyObject * |
| 6269 | posix_setegid (PyObject *self, PyObject *args) |
| 6270 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6271 | gid_t egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6272 | if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6273 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6274 | if (setegid(egid) < 0) { |
| 6275 | return posix_error(); |
| 6276 | } else { |
| 6277 | Py_INCREF(Py_None); |
| 6278 | return Py_None; |
| 6279 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6280 | } |
| 6281 | #endif /* HAVE_SETEGID */ |
| 6282 | |
| 6283 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6284 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6285 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6286 | Set the current process's real and effective user ids."); |
| 6287 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6288 | static PyObject * |
| 6289 | posix_setreuid (PyObject *self, PyObject *args) |
| 6290 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6291 | uid_t ruid, euid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6292 | if (!PyArg_ParseTuple(args, "O&O&:setreuid", |
| 6293 | _Py_Uid_Converter, &ruid, |
| 6294 | _Py_Uid_Converter, &euid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6295 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6296 | if (setreuid(ruid, euid) < 0) { |
| 6297 | return posix_error(); |
| 6298 | } else { |
| 6299 | Py_INCREF(Py_None); |
| 6300 | return Py_None; |
| 6301 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6302 | } |
| 6303 | #endif /* HAVE_SETREUID */ |
| 6304 | |
| 6305 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6306 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6307 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6308 | Set the current process's real and effective group ids."); |
| 6309 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6310 | static PyObject * |
| 6311 | posix_setregid (PyObject *self, PyObject *args) |
| 6312 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6313 | gid_t rgid, egid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6314 | if (!PyArg_ParseTuple(args, "O&O&:setregid", |
| 6315 | _Py_Gid_Converter, &rgid, |
| 6316 | _Py_Gid_Converter, &egid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6317 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6318 | if (setregid(rgid, egid) < 0) { |
| 6319 | return posix_error(); |
| 6320 | } else { |
| 6321 | Py_INCREF(Py_None); |
| 6322 | return Py_None; |
| 6323 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6324 | } |
| 6325 | #endif /* HAVE_SETREGID */ |
| 6326 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6327 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6328 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6329 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6330 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6331 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6332 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6333 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6334 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6335 | gid_t gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6336 | if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6337 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6338 | if (setgid(gid) < 0) |
| 6339 | return posix_error(); |
| 6340 | Py_INCREF(Py_None); |
| 6341 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6342 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6343 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6344 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6345 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6346 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6347 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6348 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6349 | |
| 6350 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6351 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6352 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6353 | int i, len; |
| 6354 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6355 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6356 | if (!PySequence_Check(groups)) { |
| 6357 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6358 | return NULL; |
| 6359 | } |
| 6360 | len = PySequence_Size(groups); |
| 6361 | if (len > MAX_GROUPS) { |
| 6362 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6363 | return NULL; |
| 6364 | } |
| 6365 | for(i = 0; i < len; i++) { |
| 6366 | PyObject *elem; |
| 6367 | elem = PySequence_GetItem(groups, i); |
| 6368 | if (!elem) |
| 6369 | return NULL; |
| 6370 | if (!PyLong_Check(elem)) { |
| 6371 | PyErr_SetString(PyExc_TypeError, |
| 6372 | "groups must be integers"); |
| 6373 | Py_DECREF(elem); |
| 6374 | return NULL; |
| 6375 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6376 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6377 | Py_DECREF(elem); |
| 6378 | return NULL; |
| 6379 | } |
| 6380 | } |
| 6381 | Py_DECREF(elem); |
| 6382 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6383 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6384 | if (setgroups(len, grouplist) < 0) |
| 6385 | return posix_error(); |
| 6386 | Py_INCREF(Py_None); |
| 6387 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6388 | } |
| 6389 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6390 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6391 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6392 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6393 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6394 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6395 | PyObject *result; |
| 6396 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6397 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6398 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6399 | if (pid == -1) |
| 6400 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6402 | if (struct_rusage == NULL) { |
| 6403 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6404 | if (m == NULL) |
| 6405 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6406 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6407 | Py_DECREF(m); |
| 6408 | if (struct_rusage == NULL) |
| 6409 | return NULL; |
| 6410 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6411 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6412 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6413 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6414 | if (!result) |
| 6415 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6416 | |
| 6417 | #ifndef doubletime |
| 6418 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6419 | #endif |
| 6420 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6421 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6422 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6423 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6424 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6425 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6426 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6427 | SET_INT(result, 2, ru->ru_maxrss); |
| 6428 | SET_INT(result, 3, ru->ru_ixrss); |
| 6429 | SET_INT(result, 4, ru->ru_idrss); |
| 6430 | SET_INT(result, 5, ru->ru_isrss); |
| 6431 | SET_INT(result, 6, ru->ru_minflt); |
| 6432 | SET_INT(result, 7, ru->ru_majflt); |
| 6433 | SET_INT(result, 8, ru->ru_nswap); |
| 6434 | SET_INT(result, 9, ru->ru_inblock); |
| 6435 | SET_INT(result, 10, ru->ru_oublock); |
| 6436 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6437 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6438 | SET_INT(result, 13, ru->ru_nsignals); |
| 6439 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6440 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6441 | #undef SET_INT |
| 6442 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6443 | if (PyErr_Occurred()) { |
| 6444 | Py_DECREF(result); |
| 6445 | return NULL; |
| 6446 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6447 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6448 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6449 | } |
| 6450 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6451 | |
| 6452 | #ifdef HAVE_WAIT3 |
| 6453 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6454 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6455 | Wait for completion of a child process."); |
| 6456 | |
| 6457 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6458 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6459 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6460 | pid_t pid; |
| 6461 | int options; |
| 6462 | struct rusage ru; |
| 6463 | WAIT_TYPE status; |
| 6464 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6465 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6466 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6467 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6468 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6469 | Py_BEGIN_ALLOW_THREADS |
| 6470 | pid = wait3(&status, options, &ru); |
| 6471 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6472 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6473 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6474 | } |
| 6475 | #endif /* HAVE_WAIT3 */ |
| 6476 | |
| 6477 | #ifdef HAVE_WAIT4 |
| 6478 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6479 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6480 | Wait for completion of a given child process."); |
| 6481 | |
| 6482 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6483 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6484 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6485 | pid_t pid; |
| 6486 | int options; |
| 6487 | struct rusage ru; |
| 6488 | WAIT_TYPE status; |
| 6489 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6492 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6493 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6494 | Py_BEGIN_ALLOW_THREADS |
| 6495 | pid = wait4(pid, &status, options, &ru); |
| 6496 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6497 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6498 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6499 | } |
| 6500 | #endif /* HAVE_WAIT4 */ |
| 6501 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6502 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6503 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6504 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6505 | Wait for the completion of one or more child processes.\n\n\ |
| 6506 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6507 | id specifies the pid to wait on.\n\ |
| 6508 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6509 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6510 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6511 | no children in a waitable state."); |
| 6512 | |
| 6513 | static PyObject * |
| 6514 | posix_waitid(PyObject *self, PyObject *args) |
| 6515 | { |
| 6516 | PyObject *result; |
| 6517 | idtype_t idtype; |
| 6518 | id_t id; |
| 6519 | int options, res; |
| 6520 | siginfo_t si; |
| 6521 | si.si_pid = 0; |
| 6522 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6523 | return NULL; |
| 6524 | Py_BEGIN_ALLOW_THREADS |
| 6525 | res = waitid(idtype, id, &si, options); |
| 6526 | Py_END_ALLOW_THREADS |
| 6527 | if (res == -1) |
| 6528 | return posix_error(); |
| 6529 | |
| 6530 | if (si.si_pid == 0) |
| 6531 | Py_RETURN_NONE; |
| 6532 | |
| 6533 | result = PyStructSequence_New(&WaitidResultType); |
| 6534 | if (!result) |
| 6535 | return NULL; |
| 6536 | |
| 6537 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6538 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6539 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6540 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6541 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6542 | if (PyErr_Occurred()) { |
| 6543 | Py_DECREF(result); |
| 6544 | return NULL; |
| 6545 | } |
| 6546 | |
| 6547 | return result; |
| 6548 | } |
| 6549 | #endif |
| 6550 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6551 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6552 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6553 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6554 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6555 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6556 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6557 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6558 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6559 | pid_t pid; |
| 6560 | int options; |
| 6561 | WAIT_TYPE status; |
| 6562 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6563 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6564 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6565 | return NULL; |
| 6566 | Py_BEGIN_ALLOW_THREADS |
| 6567 | pid = waitpid(pid, &status, options); |
| 6568 | Py_END_ALLOW_THREADS |
| 6569 | if (pid == -1) |
| 6570 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6571 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6572 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6573 | } |
| 6574 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6575 | #elif defined(HAVE_CWAIT) |
| 6576 | |
| 6577 | /* 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] | 6578 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6579 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6580 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6581 | |
| 6582 | static PyObject * |
| 6583 | posix_waitpid(PyObject *self, PyObject *args) |
| 6584 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6585 | Py_intptr_t pid; |
| 6586 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6587 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6588 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6589 | return NULL; |
| 6590 | Py_BEGIN_ALLOW_THREADS |
| 6591 | pid = _cwait(&status, pid, options); |
| 6592 | Py_END_ALLOW_THREADS |
| 6593 | if (pid == -1) |
| 6594 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6595 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6596 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6597 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6598 | } |
| 6599 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6600 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6601 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6602 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6603 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6604 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6605 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6606 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6607 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6608 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6609 | pid_t pid; |
| 6610 | WAIT_TYPE status; |
| 6611 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6612 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6613 | Py_BEGIN_ALLOW_THREADS |
| 6614 | pid = wait(&status); |
| 6615 | Py_END_ALLOW_THREADS |
| 6616 | if (pid == -1) |
| 6617 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6618 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6619 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6620 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6621 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6622 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6623 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6624 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 6625 | PyDoc_STRVAR(readlink__doc__, |
| 6626 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 6627 | Return a string representing the path to which the symbolic link points.\n\ |
| 6628 | \n\ |
| 6629 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 6630 | and path should be relative; path will then be relative to that directory.\n\ |
| 6631 | dir_fd may not be implemented on your platform.\n\ |
| 6632 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6633 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6634 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6635 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6636 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6637 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6638 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6639 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6640 | path_t path; |
| 6641 | int dir_fd = DEFAULT_DIR_FD; |
| 6642 | char buffer[MAXPATHLEN]; |
| 6643 | ssize_t length; |
| 6644 | PyObject *return_value = NULL; |
| 6645 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6646 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6647 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6648 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6649 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 6650 | path_converter, &path, |
| 6651 | #ifdef HAVE_READLINKAT |
| 6652 | dir_fd_converter, &dir_fd |
| 6653 | #else |
| 6654 | dir_fd_unavailable, &dir_fd |
| 6655 | #endif |
| 6656 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6657 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6658 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6659 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6660 | #ifdef HAVE_READLINKAT |
| 6661 | if (dir_fd != DEFAULT_DIR_FD) |
| 6662 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6663 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6664 | #endif |
| 6665 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 6666 | Py_END_ALLOW_THREADS |
| 6667 | |
| 6668 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6669 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6670 | goto exit; |
| 6671 | } |
| 6672 | |
| 6673 | if (PyUnicode_Check(path.object)) |
| 6674 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 6675 | else |
| 6676 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 6677 | exit: |
| 6678 | path_cleanup(&path); |
| 6679 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6680 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6681 | |
| 6682 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6683 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6684 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6685 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6686 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6687 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6688 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 6689 | Create a symbolic link pointing to src named dst.\n\n\ |
| 6690 | target_is_directory is required on Windows if the target is to be\n\ |
| 6691 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 6692 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 6693 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 6694 | \n\ |
| 6695 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 6696 | and path should be relative; path will then be relative to that directory.\n\ |
| 6697 | dir_fd may not be implemented on your platform.\n\ |
| 6698 | If it is unavailable, using it will raise a NotImplementedError."); |
| 6699 | |
| 6700 | #if defined(MS_WINDOWS) |
| 6701 | |
| 6702 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6703 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 6704 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
| 6705 | static int |
| 6706 | check_CreateSymbolicLink() |
| 6707 | { |
| 6708 | HINSTANCE hKernel32; |
| 6709 | /* only recheck */ |
| 6710 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 6711 | return 1; |
| 6712 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 6713 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6714 | "CreateSymbolicLinkW"); |
| 6715 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 6716 | "CreateSymbolicLinkA"); |
| 6717 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 6718 | } |
| 6719 | |
| 6720 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6721 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6722 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6723 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6724 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6725 | path_t src; |
| 6726 | path_t dst; |
| 6727 | int dir_fd = DEFAULT_DIR_FD; |
| 6728 | int target_is_directory = 0; |
| 6729 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 6730 | "dir_fd", NULL}; |
| 6731 | PyObject *return_value; |
| 6732 | #ifdef MS_WINDOWS |
| 6733 | DWORD result; |
| 6734 | #else |
| 6735 | int result; |
| 6736 | #endif |
| 6737 | |
| 6738 | memset(&src, 0, sizeof(src)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6739 | src.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6740 | src.argument_name = "src"; |
| 6741 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6742 | dst.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6743 | dst.argument_name = "dst"; |
| 6744 | |
| 6745 | #ifdef MS_WINDOWS |
| 6746 | if (!check_CreateSymbolicLink()) { |
| 6747 | PyErr_SetString(PyExc_NotImplementedError, |
| 6748 | "CreateSymbolicLink functions not found"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 6749 | return NULL; |
| 6750 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6751 | if (!win32_can_symlink) { |
| 6752 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 6753 | return NULL; |
| 6754 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6755 | #endif |
| 6756 | |
| 6757 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 6758 | keywords, |
| 6759 | path_converter, &src, |
| 6760 | path_converter, &dst, |
| 6761 | &target_is_directory, |
| 6762 | #ifdef HAVE_SYMLINKAT |
| 6763 | dir_fd_converter, &dir_fd |
| 6764 | #else |
| 6765 | dir_fd_unavailable, &dir_fd |
| 6766 | #endif |
| 6767 | )) |
| 6768 | return NULL; |
| 6769 | |
| 6770 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 6771 | PyErr_SetString(PyExc_ValueError, |
| 6772 | "symlink: src and dst must be the same type"); |
| 6773 | return_value = NULL; |
| 6774 | goto exit; |
| 6775 | } |
| 6776 | |
| 6777 | #ifdef MS_WINDOWS |
| 6778 | Py_BEGIN_ALLOW_THREADS |
| 6779 | if (dst.wide) |
| 6780 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 6781 | target_is_directory); |
| 6782 | else |
| 6783 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 6784 | target_is_directory); |
| 6785 | Py_END_ALLOW_THREADS |
| 6786 | |
| 6787 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 6788 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6789 | goto exit; |
| 6790 | } |
| 6791 | |
| 6792 | #else |
| 6793 | |
| 6794 | Py_BEGIN_ALLOW_THREADS |
| 6795 | #if HAVE_SYMLINKAT |
| 6796 | if (dir_fd != DEFAULT_DIR_FD) |
| 6797 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 6798 | else |
| 6799 | #endif |
| 6800 | result = symlink(src.narrow, dst.narrow); |
| 6801 | Py_END_ALLOW_THREADS |
| 6802 | |
| 6803 | if (result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 6804 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6805 | goto exit; |
| 6806 | } |
| 6807 | #endif |
| 6808 | |
| 6809 | return_value = Py_None; |
| 6810 | Py_INCREF(Py_None); |
| 6811 | goto exit; /* silence "unused label" warning */ |
| 6812 | exit: |
| 6813 | path_cleanup(&src); |
| 6814 | path_cleanup(&dst); |
| 6815 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6816 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6817 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6818 | #endif /* HAVE_SYMLINK */ |
| 6819 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6820 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6821 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6822 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6823 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6824 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6825 | { |
| 6826 | wchar_t *path; |
| 6827 | DWORD n_bytes_returned; |
| 6828 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6829 | PyObject *po, *result; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 6830 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6831 | HANDLE reparse_point_handle; |
| 6832 | |
| 6833 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6834 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6835 | wchar_t *print_name; |
| 6836 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6837 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 6838 | |
| 6839 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 6840 | &po, |
| 6841 | dir_fd_unavailable, &dir_fd |
| 6842 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6843 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6844 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6845 | path = PyUnicode_AsUnicode(po); |
| 6846 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6847 | return NULL; |
| 6848 | |
| 6849 | /* First get a handle to the reparse point */ |
| 6850 | Py_BEGIN_ALLOW_THREADS |
| 6851 | reparse_point_handle = CreateFileW( |
| 6852 | path, |
| 6853 | 0, |
| 6854 | 0, |
| 6855 | 0, |
| 6856 | OPEN_EXISTING, |
| 6857 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6858 | 0); |
| 6859 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6860 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6861 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6862 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6863 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6864 | Py_BEGIN_ALLOW_THREADS |
| 6865 | /* New call DeviceIoControl to read the reparse point */ |
| 6866 | io_result = DeviceIoControl( |
| 6867 | reparse_point_handle, |
| 6868 | FSCTL_GET_REPARSE_POINT, |
| 6869 | 0, 0, /* in buffer */ |
| 6870 | target_buffer, sizeof(target_buffer), |
| 6871 | &n_bytes_returned, |
| 6872 | 0 /* we're not using OVERLAPPED_IO */ |
| 6873 | ); |
| 6874 | CloseHandle(reparse_point_handle); |
| 6875 | Py_END_ALLOW_THREADS |
| 6876 | |
| 6877 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6878 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6879 | |
| 6880 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6881 | { |
| 6882 | PyErr_SetString(PyExc_ValueError, |
| 6883 | "not a symbolic link"); |
| 6884 | return NULL; |
| 6885 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6886 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6887 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6888 | |
| 6889 | result = PyUnicode_FromWideChar(print_name, |
| 6890 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6891 | return result; |
| 6892 | } |
| 6893 | |
| 6894 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6895 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6896 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6897 | static PyStructSequence_Field times_result_fields[] = { |
| 6898 | {"user", "user time"}, |
| 6899 | {"system", "system time"}, |
| 6900 | {"children_user", "user time of children"}, |
| 6901 | {"children_system", "system time of children"}, |
| 6902 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 6903 | {NULL} |
| 6904 | }; |
| 6905 | |
| 6906 | PyDoc_STRVAR(times_result__doc__, |
| 6907 | "times_result: Result from os.times().\n\n\ |
| 6908 | This object may be accessed either as a tuple of\n\ |
| 6909 | (user, system, children_user, children_system, elapsed),\n\ |
| 6910 | or via the attributes user, system, children_user, children_system,\n\ |
| 6911 | and elapsed.\n\ |
| 6912 | \n\ |
| 6913 | See os.times for more information."); |
| 6914 | |
| 6915 | static PyStructSequence_Desc times_result_desc = { |
| 6916 | "times_result", /* name */ |
| 6917 | times_result__doc__, /* doc */ |
| 6918 | times_result_fields, |
| 6919 | 5 |
| 6920 | }; |
| 6921 | |
| 6922 | static PyTypeObject TimesResultType; |
| 6923 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 6924 | #ifdef MS_WINDOWS |
| 6925 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 6926 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6927 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 6928 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6929 | |
| 6930 | static PyObject * |
| 6931 | build_times_result(double user, double system, |
| 6932 | double children_user, double children_system, |
| 6933 | double elapsed) |
| 6934 | { |
| 6935 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 6936 | if (value == NULL) |
| 6937 | return NULL; |
| 6938 | |
| 6939 | #define SET(i, field) \ |
| 6940 | { \ |
| 6941 | PyObject *o = PyFloat_FromDouble(field); \ |
| 6942 | if (!o) { \ |
| 6943 | Py_DECREF(value); \ |
| 6944 | return NULL; \ |
| 6945 | } \ |
| 6946 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 6947 | } \ |
| 6948 | |
| 6949 | SET(0, user); |
| 6950 | SET(1, system); |
| 6951 | SET(2, children_user); |
| 6952 | SET(3, children_system); |
| 6953 | SET(4, elapsed); |
| 6954 | |
| 6955 | #undef SET |
| 6956 | |
| 6957 | return value; |
| 6958 | } |
| 6959 | |
| 6960 | PyDoc_STRVAR(posix_times__doc__, |
| 6961 | "times() -> times_result\n\n\ |
| 6962 | Return an object containing floating point numbers indicating process\n\ |
| 6963 | times. The object behaves like a named tuple with these fields:\n\ |
| 6964 | (utime, stime, cutime, cstime, elapsed_time)"); |
| 6965 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 6966 | #if defined(MS_WINDOWS) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6967 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6968 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6969 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6970 | FILETIME create, exit, kernel, user; |
| 6971 | HANDLE hProc; |
| 6972 | hProc = GetCurrentProcess(); |
| 6973 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6974 | /* The fields of a FILETIME structure are the hi and lo part |
| 6975 | of a 64-bit value expressed in 100 nanosecond units. |
| 6976 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6977 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6978 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6979 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6980 | (double)(user.dwHighDateTime*429.4967296 + |
| 6981 | user.dwLowDateTime*1e-7), |
| 6982 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6983 | kernel.dwLowDateTime*1e-7), |
| 6984 | (double)0, |
| 6985 | (double)0, |
| 6986 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6987 | } |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 6988 | #else /* Not Windows */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 6989 | #define NEED_TICKS_PER_SECOND |
| 6990 | static long ticks_per_second = -1; |
| 6991 | static PyObject * |
| 6992 | posix_times(PyObject *self, PyObject *noargs) |
| 6993 | { |
| 6994 | struct tms t; |
| 6995 | clock_t c; |
| 6996 | errno = 0; |
| 6997 | c = times(&t); |
| 6998 | if (c == (clock_t) -1) |
| 6999 | return posix_error(); |
| 7000 | return build_times_result( |
| 7001 | (double)t.tms_utime / ticks_per_second, |
| 7002 | (double)t.tms_stime / ticks_per_second, |
| 7003 | (double)t.tms_cutime / ticks_per_second, |
| 7004 | (double)t.tms_cstime / ticks_per_second, |
| 7005 | (double)c / ticks_per_second); |
| 7006 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7007 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7008 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7009 | #endif /* HAVE_TIMES */ |
| 7010 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7011 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7012 | #ifdef HAVE_GETSID |
| 7013 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7014 | "getsid(pid) -> sid\n\n\ |
| 7015 | Call the system call getsid()."); |
| 7016 | |
| 7017 | static PyObject * |
| 7018 | posix_getsid(PyObject *self, PyObject *args) |
| 7019 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7020 | pid_t pid; |
| 7021 | int sid; |
| 7022 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7023 | return NULL; |
| 7024 | sid = getsid(pid); |
| 7025 | if (sid < 0) |
| 7026 | return posix_error(); |
| 7027 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7028 | } |
| 7029 | #endif /* HAVE_GETSID */ |
| 7030 | |
| 7031 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7032 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7033 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7034 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7035 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7036 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7037 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7038 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7039 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7040 | if (setsid() < 0) |
| 7041 | return posix_error(); |
| 7042 | Py_INCREF(Py_None); |
| 7043 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7044 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7045 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7046 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7047 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7048 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7049 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7050 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7051 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7052 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7053 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7054 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7055 | pid_t pid; |
| 7056 | int pgrp; |
| 7057 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7058 | return NULL; |
| 7059 | if (setpgid(pid, pgrp) < 0) |
| 7060 | return posix_error(); |
| 7061 | Py_INCREF(Py_None); |
| 7062 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7063 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7064 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7065 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7066 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7067 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7068 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7069 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7070 | 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] | 7071 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7072 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7073 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7074 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7075 | int fd; |
| 7076 | pid_t pgid; |
| 7077 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7078 | return NULL; |
| 7079 | pgid = tcgetpgrp(fd); |
| 7080 | if (pgid < 0) |
| 7081 | return posix_error(); |
| 7082 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7083 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7084 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7085 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7086 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7087 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7088 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7089 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7090 | 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] | 7091 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7092 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7093 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7094 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7095 | int fd; |
| 7096 | pid_t pgid; |
| 7097 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7098 | return NULL; |
| 7099 | if (tcsetpgrp(fd, pgid) < 0) |
| 7100 | return posix_error(); |
| 7101 | Py_INCREF(Py_None); |
| 7102 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7103 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7104 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7105 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7106 | /* Functions acting on file descriptors */ |
| 7107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7108 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7109 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7110 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7111 | \n\ |
| 7112 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7113 | and path should be relative; path will then be relative to that directory.\n\ |
| 7114 | dir_fd may not be implemented on your platform.\n\ |
| 7115 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7116 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7117 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7118 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7119 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7120 | path_t path; |
| 7121 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7122 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7123 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7124 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7125 | PyObject *return_value = NULL; |
| 7126 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7127 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7128 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7129 | path.function_name = "open"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7130 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7131 | path_converter, &path, |
| 7132 | &flags, &mode, |
| 7133 | #ifdef HAVE_OPENAT |
| 7134 | dir_fd_converter, &dir_fd |
| 7135 | #else |
| 7136 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7137 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7138 | )) |
| 7139 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7140 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7141 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7142 | #ifdef MS_WINDOWS |
| 7143 | if (path.wide) |
| 7144 | fd = _wopen(path.wide, flags, mode); |
| 7145 | else |
| 7146 | #endif |
| 7147 | #ifdef HAVE_OPENAT |
| 7148 | if (dir_fd != DEFAULT_DIR_FD) |
| 7149 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7150 | else |
| 7151 | #endif |
| 7152 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7153 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7154 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7155 | if (fd == -1) { |
Victor Stinner | 4e7d2d4 | 2012-11-05 01:20:58 +0100 | [diff] [blame] | 7156 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path.object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7157 | goto exit; |
| 7158 | } |
| 7159 | |
| 7160 | return_value = PyLong_FromLong((long)fd); |
| 7161 | |
| 7162 | exit: |
| 7163 | path_cleanup(&path); |
| 7164 | return return_value; |
| 7165 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7167 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7168 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7169 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7170 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7171 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7172 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7173 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7174 | int fd, res; |
| 7175 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7176 | return NULL; |
| 7177 | if (!_PyVerify_fd(fd)) |
| 7178 | return posix_error(); |
| 7179 | Py_BEGIN_ALLOW_THREADS |
| 7180 | res = close(fd); |
| 7181 | Py_END_ALLOW_THREADS |
| 7182 | if (res < 0) |
| 7183 | return posix_error(); |
| 7184 | Py_INCREF(Py_None); |
| 7185 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7186 | } |
| 7187 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7188 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7189 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7190 | "closerange(fd_low, fd_high)\n\n\ |
| 7191 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7192 | |
| 7193 | static PyObject * |
| 7194 | posix_closerange(PyObject *self, PyObject *args) |
| 7195 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7196 | int fd_from, fd_to, i; |
| 7197 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7198 | return NULL; |
| 7199 | Py_BEGIN_ALLOW_THREADS |
| 7200 | for (i = fd_from; i < fd_to; i++) |
| 7201 | if (_PyVerify_fd(i)) |
| 7202 | close(i); |
| 7203 | Py_END_ALLOW_THREADS |
| 7204 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7205 | } |
| 7206 | |
| 7207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7208 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7209 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7210 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7211 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7212 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7213 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7214 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7215 | int fd; |
| 7216 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7217 | return NULL; |
| 7218 | if (!_PyVerify_fd(fd)) |
| 7219 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7220 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7221 | if (fd < 0) |
| 7222 | return posix_error(); |
| 7223 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7224 | } |
| 7225 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7226 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7227 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7228 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7229 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7230 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7231 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7232 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7233 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7234 | int fd, fd2, res; |
| 7235 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 7236 | return NULL; |
| 7237 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7238 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7239 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7240 | if (res < 0) |
| 7241 | return posix_error(); |
| 7242 | Py_INCREF(Py_None); |
| 7243 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7244 | } |
| 7245 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7246 | #ifdef HAVE_LOCKF |
| 7247 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7248 | "lockf(fd, cmd, len)\n\n\ |
| 7249 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7250 | fd is an open file descriptor.\n\ |
| 7251 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7252 | F_TEST.\n\ |
| 7253 | len specifies the section of the file to lock."); |
| 7254 | |
| 7255 | static PyObject * |
| 7256 | posix_lockf(PyObject *self, PyObject *args) |
| 7257 | { |
| 7258 | int fd, cmd, res; |
| 7259 | off_t len; |
| 7260 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7261 | &fd, &cmd, _parse_off_t, &len)) |
| 7262 | return NULL; |
| 7263 | |
| 7264 | Py_BEGIN_ALLOW_THREADS |
| 7265 | res = lockf(fd, cmd, len); |
| 7266 | Py_END_ALLOW_THREADS |
| 7267 | |
| 7268 | if (res < 0) |
| 7269 | return posix_error(); |
| 7270 | |
| 7271 | Py_RETURN_NONE; |
| 7272 | } |
| 7273 | #endif |
| 7274 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7275 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7276 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7277 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7278 | Set the current position of a file descriptor.\n\ |
| 7279 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7280 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7281 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7282 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7283 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7284 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7285 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7286 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7287 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7288 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7289 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7290 | PyObject *posobj; |
| 7291 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7292 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7293 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7294 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7295 | switch (how) { |
| 7296 | case 0: how = SEEK_SET; break; |
| 7297 | case 1: how = SEEK_CUR; break; |
| 7298 | case 2: how = SEEK_END; break; |
| 7299 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7300 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7301 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7302 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7303 | pos = PyLong_AsLong(posobj); |
| 7304 | #else |
| 7305 | pos = PyLong_AsLongLong(posobj); |
| 7306 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7307 | if (PyErr_Occurred()) |
| 7308 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7309 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7310 | if (!_PyVerify_fd(fd)) |
| 7311 | return posix_error(); |
| 7312 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7313 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7314 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7315 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7316 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7317 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7318 | Py_END_ALLOW_THREADS |
| 7319 | if (res < 0) |
| 7320 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7321 | |
| 7322 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7323 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7324 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7325 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7326 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7327 | } |
| 7328 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7330 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7331 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7332 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7334 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7335 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7336 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7337 | int fd, size; |
| 7338 | Py_ssize_t n; |
| 7339 | PyObject *buffer; |
| 7340 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7341 | return NULL; |
| 7342 | if (size < 0) { |
| 7343 | errno = EINVAL; |
| 7344 | return posix_error(); |
| 7345 | } |
| 7346 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7347 | if (buffer == NULL) |
| 7348 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7349 | if (!_PyVerify_fd(fd)) { |
| 7350 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7351 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7352 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7353 | Py_BEGIN_ALLOW_THREADS |
| 7354 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7355 | Py_END_ALLOW_THREADS |
| 7356 | if (n < 0) { |
| 7357 | Py_DECREF(buffer); |
| 7358 | return posix_error(); |
| 7359 | } |
| 7360 | if (n != size) |
| 7361 | _PyBytes_Resize(&buffer, n); |
| 7362 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7365 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7366 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7367 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7368 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7369 | { |
| 7370 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7371 | Py_ssize_t blen, total = 0; |
| 7372 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7373 | *iov = PyMem_New(struct iovec, cnt); |
| 7374 | if (*iov == NULL) { |
| 7375 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7376 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7377 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7378 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7379 | *buf = PyMem_New(Py_buffer, cnt); |
| 7380 | if (*buf == NULL) { |
| 7381 | PyMem_Del(*iov); |
| 7382 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7383 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7384 | } |
| 7385 | |
| 7386 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7387 | PyObject *item = PySequence_GetItem(seq, i); |
| 7388 | if (item == NULL) |
| 7389 | goto fail; |
| 7390 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7391 | Py_DECREF(item); |
| 7392 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7393 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7394 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7395 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7396 | blen = (*buf)[i].len; |
| 7397 | (*iov)[i].iov_len = blen; |
| 7398 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7399 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7400 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7401 | |
| 7402 | fail: |
| 7403 | PyMem_Del(*iov); |
| 7404 | for (j = 0; j < i; j++) { |
| 7405 | PyBuffer_Release(&(*buf)[j]); |
| 7406 | } |
| 7407 | PyMem_Del(*buf); |
| 7408 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7409 | } |
| 7410 | |
| 7411 | static void |
| 7412 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7413 | { |
| 7414 | int i; |
| 7415 | PyMem_Del(iov); |
| 7416 | for (i = 0; i < cnt; i++) { |
| 7417 | PyBuffer_Release(&buf[i]); |
| 7418 | } |
| 7419 | PyMem_Del(buf); |
| 7420 | } |
| 7421 | #endif |
| 7422 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7423 | #ifdef HAVE_READV |
| 7424 | PyDoc_STRVAR(posix_readv__doc__, |
| 7425 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7426 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7427 | is an arbitrary sequence of writable buffers.\n\ |
| 7428 | Returns the total number of bytes read."); |
| 7429 | |
| 7430 | static PyObject * |
| 7431 | posix_readv(PyObject *self, PyObject *args) |
| 7432 | { |
| 7433 | int fd, cnt; |
| 7434 | Py_ssize_t n; |
| 7435 | PyObject *seq; |
| 7436 | struct iovec *iov; |
| 7437 | Py_buffer *buf; |
| 7438 | |
| 7439 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7440 | return NULL; |
| 7441 | if (!PySequence_Check(seq)) { |
| 7442 | PyErr_SetString(PyExc_TypeError, |
| 7443 | "readv() arg 2 must be a sequence"); |
| 7444 | return NULL; |
| 7445 | } |
| 7446 | cnt = PySequence_Size(seq); |
| 7447 | |
| 7448 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7449 | return NULL; |
| 7450 | |
| 7451 | Py_BEGIN_ALLOW_THREADS |
| 7452 | n = readv(fd, iov, cnt); |
| 7453 | Py_END_ALLOW_THREADS |
| 7454 | |
| 7455 | iov_cleanup(iov, buf, cnt); |
| 7456 | return PyLong_FromSsize_t(n); |
| 7457 | } |
| 7458 | #endif |
| 7459 | |
| 7460 | #ifdef HAVE_PREAD |
| 7461 | PyDoc_STRVAR(posix_pread__doc__, |
| 7462 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7463 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7464 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7465 | |
| 7466 | static PyObject * |
| 7467 | posix_pread(PyObject *self, PyObject *args) |
| 7468 | { |
| 7469 | int fd, size; |
| 7470 | off_t offset; |
| 7471 | Py_ssize_t n; |
| 7472 | PyObject *buffer; |
| 7473 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7474 | return NULL; |
| 7475 | |
| 7476 | if (size < 0) { |
| 7477 | errno = EINVAL; |
| 7478 | return posix_error(); |
| 7479 | } |
| 7480 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7481 | if (buffer == NULL) |
| 7482 | return NULL; |
| 7483 | if (!_PyVerify_fd(fd)) { |
| 7484 | Py_DECREF(buffer); |
| 7485 | return posix_error(); |
| 7486 | } |
| 7487 | Py_BEGIN_ALLOW_THREADS |
| 7488 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7489 | Py_END_ALLOW_THREADS |
| 7490 | if (n < 0) { |
| 7491 | Py_DECREF(buffer); |
| 7492 | return posix_error(); |
| 7493 | } |
| 7494 | if (n != size) |
| 7495 | _PyBytes_Resize(&buffer, n); |
| 7496 | return buffer; |
| 7497 | } |
| 7498 | #endif |
| 7499 | |
| 7500 | PyDoc_STRVAR(posix_write__doc__, |
| 7501 | "write(fd, string) -> byteswritten\n\n\ |
| 7502 | Write a string to a file descriptor."); |
| 7503 | |
| 7504 | static PyObject * |
| 7505 | posix_write(PyObject *self, PyObject *args) |
| 7506 | { |
| 7507 | Py_buffer pbuf; |
| 7508 | int fd; |
| 7509 | Py_ssize_t size, len; |
| 7510 | |
| 7511 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7512 | return NULL; |
| 7513 | if (!_PyVerify_fd(fd)) { |
| 7514 | PyBuffer_Release(&pbuf); |
| 7515 | return posix_error(); |
| 7516 | } |
| 7517 | len = pbuf.len; |
| 7518 | Py_BEGIN_ALLOW_THREADS |
| 7519 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7520 | if (len > INT_MAX) |
| 7521 | len = INT_MAX; |
| 7522 | size = write(fd, pbuf.buf, (int)len); |
| 7523 | #else |
| 7524 | size = write(fd, pbuf.buf, len); |
| 7525 | #endif |
| 7526 | Py_END_ALLOW_THREADS |
| 7527 | PyBuffer_Release(&pbuf); |
| 7528 | if (size < 0) |
| 7529 | return posix_error(); |
| 7530 | return PyLong_FromSsize_t(size); |
| 7531 | } |
| 7532 | |
| 7533 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7534 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7535 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7536 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7537 | -> byteswritten\n\ |
| 7538 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7539 | |
| 7540 | static PyObject * |
| 7541 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7542 | { |
| 7543 | int in, out; |
| 7544 | Py_ssize_t ret; |
| 7545 | off_t offset; |
| 7546 | |
| 7547 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7548 | #ifndef __APPLE__ |
| 7549 | Py_ssize_t len; |
| 7550 | #endif |
| 7551 | PyObject *headers = NULL, *trailers = NULL; |
| 7552 | Py_buffer *hbuf, *tbuf; |
| 7553 | off_t sbytes; |
| 7554 | struct sf_hdtr sf; |
| 7555 | int flags = 0; |
| 7556 | sf.headers = NULL; |
| 7557 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7558 | static char *keywords[] = {"out", "in", |
| 7559 | "offset", "count", |
| 7560 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7561 | |
| 7562 | #ifdef __APPLE__ |
| 7563 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7564 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7565 | #else |
| 7566 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7567 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7568 | #endif |
| 7569 | &headers, &trailers, &flags)) |
| 7570 | return NULL; |
| 7571 | if (headers != NULL) { |
| 7572 | if (!PySequence_Check(headers)) { |
| 7573 | PyErr_SetString(PyExc_TypeError, |
| 7574 | "sendfile() headers must be a sequence or None"); |
| 7575 | return NULL; |
| 7576 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7577 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7578 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7579 | if (sf.hdr_cnt > 0 && |
| 7580 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7581 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7582 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7583 | #ifdef __APPLE__ |
| 7584 | sbytes += i; |
| 7585 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7586 | } |
| 7587 | } |
| 7588 | if (trailers != NULL) { |
| 7589 | if (!PySequence_Check(trailers)) { |
| 7590 | PyErr_SetString(PyExc_TypeError, |
| 7591 | "sendfile() trailers must be a sequence or None"); |
| 7592 | return NULL; |
| 7593 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7594 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7595 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7596 | if (sf.trl_cnt > 0 && |
| 7597 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7598 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7599 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7600 | #ifdef __APPLE__ |
| 7601 | sbytes += i; |
| 7602 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7603 | } |
| 7604 | } |
| 7605 | |
| 7606 | Py_BEGIN_ALLOW_THREADS |
| 7607 | #ifdef __APPLE__ |
| 7608 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7609 | #else |
| 7610 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7611 | #endif |
| 7612 | Py_END_ALLOW_THREADS |
| 7613 | |
| 7614 | if (sf.headers != NULL) |
| 7615 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7616 | if (sf.trailers != NULL) |
| 7617 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7618 | |
| 7619 | if (ret < 0) { |
| 7620 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7621 | if (sbytes != 0) { |
| 7622 | // some data has been sent |
| 7623 | goto done; |
| 7624 | } |
| 7625 | else { |
| 7626 | // no data has been sent; upper application is supposed |
| 7627 | // to retry on EAGAIN or EBUSY |
| 7628 | return posix_error(); |
| 7629 | } |
| 7630 | } |
| 7631 | return posix_error(); |
| 7632 | } |
| 7633 | goto done; |
| 7634 | |
| 7635 | done: |
| 7636 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7637 | return Py_BuildValue("l", sbytes); |
| 7638 | #else |
| 7639 | return Py_BuildValue("L", sbytes); |
| 7640 | #endif |
| 7641 | |
| 7642 | #else |
| 7643 | Py_ssize_t count; |
| 7644 | PyObject *offobj; |
| 7645 | static char *keywords[] = {"out", "in", |
| 7646 | "offset", "count", NULL}; |
| 7647 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7648 | keywords, &out, &in, &offobj, &count)) |
| 7649 | return NULL; |
| 7650 | #ifdef linux |
| 7651 | if (offobj == Py_None) { |
| 7652 | Py_BEGIN_ALLOW_THREADS |
| 7653 | ret = sendfile(out, in, NULL, count); |
| 7654 | Py_END_ALLOW_THREADS |
| 7655 | if (ret < 0) |
| 7656 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7657 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7658 | } |
| 7659 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7660 | if (!_parse_off_t(offobj, &offset)) |
| 7661 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7662 | Py_BEGIN_ALLOW_THREADS |
| 7663 | ret = sendfile(out, in, &offset, count); |
| 7664 | Py_END_ALLOW_THREADS |
| 7665 | if (ret < 0) |
| 7666 | return posix_error(); |
| 7667 | return Py_BuildValue("n", ret); |
| 7668 | #endif |
| 7669 | } |
| 7670 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7671 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7672 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7673 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7674 | Like stat(), but for an open file descriptor.\n\ |
| 7675 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7676 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7677 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7678 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7679 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7680 | int fd; |
| 7681 | STRUCT_STAT st; |
| 7682 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7683 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7684 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7685 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7686 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7687 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7688 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7689 | Py_BEGIN_ALLOW_THREADS |
| 7690 | res = FSTAT(fd, &st); |
| 7691 | Py_END_ALLOW_THREADS |
| 7692 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7693 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 7694 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7695 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7696 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7697 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7698 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7699 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7700 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7701 | } |
| 7702 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7703 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7704 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7705 | 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] | 7706 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7707 | |
| 7708 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7709 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7710 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7711 | int fd; |
| 7712 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7713 | return NULL; |
| 7714 | if (!_PyVerify_fd(fd)) |
| 7715 | return PyBool_FromLong(0); |
| 7716 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7717 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7718 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7719 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7720 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7721 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7722 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7723 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7724 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7725 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7726 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7727 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7728 | int fds[2]; |
| 7729 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7730 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7731 | if (res != 0) |
| 7732 | return posix_error(); |
| 7733 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7734 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7735 | HANDLE read, write; |
| 7736 | int read_fd, write_fd; |
| 7737 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7738 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7739 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 7740 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7741 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7742 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7743 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7744 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7745 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7746 | #endif /* HAVE_PIPE */ |
| 7747 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7748 | #ifdef HAVE_PIPE2 |
| 7749 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7750 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7751 | Create a pipe with flags set atomically.\n\ |
| 7752 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7753 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7754 | "); |
| 7755 | |
| 7756 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7757 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7758 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7759 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7760 | int fds[2]; |
| 7761 | int res; |
| 7762 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 7763 | flags = _PyLong_AsInt(arg); |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7764 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7765 | return NULL; |
| 7766 | |
| 7767 | res = pipe2(fds, flags); |
| 7768 | if (res != 0) |
| 7769 | return posix_error(); |
| 7770 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7771 | } |
| 7772 | #endif /* HAVE_PIPE2 */ |
| 7773 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7774 | #ifdef HAVE_WRITEV |
| 7775 | PyDoc_STRVAR(posix_writev__doc__, |
| 7776 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7777 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7778 | arbitrary sequence of buffers.\n\ |
| 7779 | Returns the total bytes written."); |
| 7780 | |
| 7781 | static PyObject * |
| 7782 | posix_writev(PyObject *self, PyObject *args) |
| 7783 | { |
| 7784 | int fd, cnt; |
| 7785 | Py_ssize_t res; |
| 7786 | PyObject *seq; |
| 7787 | struct iovec *iov; |
| 7788 | Py_buffer *buf; |
| 7789 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7790 | return NULL; |
| 7791 | if (!PySequence_Check(seq)) { |
| 7792 | PyErr_SetString(PyExc_TypeError, |
| 7793 | "writev() arg 2 must be a sequence"); |
| 7794 | return NULL; |
| 7795 | } |
| 7796 | cnt = PySequence_Size(seq); |
| 7797 | |
| 7798 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7799 | return NULL; |
| 7800 | } |
| 7801 | |
| 7802 | Py_BEGIN_ALLOW_THREADS |
| 7803 | res = writev(fd, iov, cnt); |
| 7804 | Py_END_ALLOW_THREADS |
| 7805 | |
| 7806 | iov_cleanup(iov, buf, cnt); |
| 7807 | return PyLong_FromSsize_t(res); |
| 7808 | } |
| 7809 | #endif |
| 7810 | |
| 7811 | #ifdef HAVE_PWRITE |
| 7812 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7813 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7814 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7815 | offset unchanged."); |
| 7816 | |
| 7817 | static PyObject * |
| 7818 | posix_pwrite(PyObject *self, PyObject *args) |
| 7819 | { |
| 7820 | Py_buffer pbuf; |
| 7821 | int fd; |
| 7822 | off_t offset; |
| 7823 | Py_ssize_t size; |
| 7824 | |
| 7825 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7826 | return NULL; |
| 7827 | |
| 7828 | if (!_PyVerify_fd(fd)) { |
| 7829 | PyBuffer_Release(&pbuf); |
| 7830 | return posix_error(); |
| 7831 | } |
| 7832 | Py_BEGIN_ALLOW_THREADS |
| 7833 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7834 | Py_END_ALLOW_THREADS |
| 7835 | PyBuffer_Release(&pbuf); |
| 7836 | if (size < 0) |
| 7837 | return posix_error(); |
| 7838 | return PyLong_FromSsize_t(size); |
| 7839 | } |
| 7840 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7841 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7842 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7843 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7844 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 7845 | Create a FIFO (a POSIX named pipe).\n\ |
| 7846 | \n\ |
| 7847 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7848 | and path should be relative; path will then be relative to that directory.\n\ |
| 7849 | dir_fd may not be implemented on your platform.\n\ |
| 7850 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7851 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7852 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7853 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7854 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7855 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7856 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7857 | int dir_fd = DEFAULT_DIR_FD; |
| 7858 | int result; |
| 7859 | PyObject *return_value = NULL; |
| 7860 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 7861 | |
| 7862 | memset(&path, 0, sizeof(path)); |
| 7863 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 7864 | path_converter, &path, |
| 7865 | &mode, |
| 7866 | #ifdef HAVE_MKFIFOAT |
| 7867 | dir_fd_converter, &dir_fd |
| 7868 | #else |
| 7869 | dir_fd_unavailable, &dir_fd |
| 7870 | #endif |
| 7871 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7872 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7873 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7874 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7875 | #ifdef HAVE_MKFIFOAT |
| 7876 | if (dir_fd != DEFAULT_DIR_FD) |
| 7877 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 7878 | else |
| 7879 | #endif |
| 7880 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7881 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7882 | |
| 7883 | if (result < 0) { |
| 7884 | return_value = posix_error(); |
| 7885 | goto exit; |
| 7886 | } |
| 7887 | |
| 7888 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7889 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7890 | |
| 7891 | exit: |
| 7892 | path_cleanup(&path); |
| 7893 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7894 | } |
| 7895 | #endif |
| 7896 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7897 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7898 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7899 | "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] | 7900 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7901 | named filename. mode specifies both the permissions to use and the\n\ |
| 7902 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7903 | 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] | 7904 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7905 | os.makedev()), otherwise it is ignored.\n\ |
| 7906 | \n\ |
| 7907 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7908 | and path should be relative; path will then be relative to that directory.\n\ |
| 7909 | dir_fd may not be implemented on your platform.\n\ |
| 7910 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7911 | |
| 7912 | |
| 7913 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7914 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7915 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7916 | path_t path; |
| 7917 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7918 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7919 | int dir_fd = DEFAULT_DIR_FD; |
| 7920 | int result; |
| 7921 | PyObject *return_value = NULL; |
| 7922 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 7923 | |
| 7924 | memset(&path, 0, sizeof(path)); |
| 7925 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 7926 | path_converter, &path, |
| 7927 | &mode, &device, |
| 7928 | #ifdef HAVE_MKNODAT |
| 7929 | dir_fd_converter, &dir_fd |
| 7930 | #else |
| 7931 | dir_fd_unavailable, &dir_fd |
| 7932 | #endif |
| 7933 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7934 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7935 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7936 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7937 | #ifdef HAVE_MKNODAT |
| 7938 | if (dir_fd != DEFAULT_DIR_FD) |
| 7939 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 7940 | else |
| 7941 | #endif |
| 7942 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7943 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7944 | |
| 7945 | if (result < 0) { |
| 7946 | return_value = posix_error(); |
| 7947 | goto exit; |
| 7948 | } |
| 7949 | |
| 7950 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7951 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 7952 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7953 | exit: |
| 7954 | path_cleanup(&path); |
| 7955 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7956 | } |
| 7957 | #endif |
| 7958 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7959 | #ifdef HAVE_DEVICE_MACROS |
| 7960 | PyDoc_STRVAR(posix_major__doc__, |
| 7961 | "major(device) -> major number\n\ |
| 7962 | Extracts a device major number from a raw device number."); |
| 7963 | |
| 7964 | static PyObject * |
| 7965 | posix_major(PyObject *self, PyObject *args) |
| 7966 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7967 | int device; |
| 7968 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7969 | return NULL; |
| 7970 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7971 | } |
| 7972 | |
| 7973 | PyDoc_STRVAR(posix_minor__doc__, |
| 7974 | "minor(device) -> minor number\n\ |
| 7975 | Extracts a device minor number from a raw device number."); |
| 7976 | |
| 7977 | static PyObject * |
| 7978 | posix_minor(PyObject *self, PyObject *args) |
| 7979 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7980 | int device; |
| 7981 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7982 | return NULL; |
| 7983 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7984 | } |
| 7985 | |
| 7986 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7987 | "makedev(major, minor) -> device number\n\ |
| 7988 | Composes a raw device number from the major and minor device numbers."); |
| 7989 | |
| 7990 | static PyObject * |
| 7991 | posix_makedev(PyObject *self, PyObject *args) |
| 7992 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7993 | int major, minor; |
| 7994 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7995 | return NULL; |
| 7996 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7997 | } |
| 7998 | #endif /* device macros */ |
| 7999 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8000 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8001 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8002 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8003 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8004 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8005 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8006 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8007 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8008 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8009 | int fd; |
| 8010 | off_t length; |
| 8011 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8012 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8013 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8014 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8015 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8016 | Py_BEGIN_ALLOW_THREADS |
| 8017 | res = ftruncate(fd, length); |
| 8018 | Py_END_ALLOW_THREADS |
| 8019 | if (res < 0) |
| 8020 | return posix_error(); |
| 8021 | Py_INCREF(Py_None); |
| 8022 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8023 | } |
| 8024 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8025 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8026 | #ifdef HAVE_TRUNCATE |
| 8027 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8028 | "truncate(path, length)\n\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8029 | Truncate the file given by path to length bytes.\n\ |
| 8030 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8031 | If this functionality is unavailable, using it raises an exception."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8032 | |
| 8033 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8034 | posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8035 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8036 | path_t path; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8037 | off_t length; |
| 8038 | int res; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8039 | PyObject *result = NULL; |
| 8040 | static char *keywords[] = {"path", "length", NULL}; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8041 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8042 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8043 | path.function_name = "truncate"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8044 | #ifdef HAVE_FTRUNCATE |
| 8045 | path.allow_fd = 1; |
| 8046 | #endif |
| 8047 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords, |
| 8048 | path_converter, &path, |
| 8049 | _parse_off_t, &length)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8050 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8051 | |
| 8052 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8053 | #ifdef HAVE_FTRUNCATE |
| 8054 | if (path.fd != -1) |
| 8055 | res = ftruncate(path.fd, length); |
| 8056 | else |
| 8057 | #endif |
| 8058 | res = truncate(path.narrow, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8059 | Py_END_ALLOW_THREADS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8060 | if (res < 0) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8061 | result = path_error(&path); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8062 | else { |
| 8063 | Py_INCREF(Py_None); |
| 8064 | result = Py_None; |
| 8065 | } |
| 8066 | path_cleanup(&path); |
| 8067 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8068 | } |
| 8069 | #endif |
| 8070 | |
| 8071 | #ifdef HAVE_POSIX_FALLOCATE |
| 8072 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8073 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8074 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8075 | starting from offset and continuing for len bytes."); |
| 8076 | |
| 8077 | static PyObject * |
| 8078 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8079 | { |
| 8080 | off_t len, offset; |
| 8081 | int res, fd; |
| 8082 | |
| 8083 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8084 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8085 | return NULL; |
| 8086 | |
| 8087 | Py_BEGIN_ALLOW_THREADS |
| 8088 | res = posix_fallocate(fd, offset, len); |
| 8089 | Py_END_ALLOW_THREADS |
| 8090 | if (res != 0) { |
| 8091 | errno = res; |
| 8092 | return posix_error(); |
| 8093 | } |
| 8094 | Py_RETURN_NONE; |
| 8095 | } |
| 8096 | #endif |
| 8097 | |
| 8098 | #ifdef HAVE_POSIX_FADVISE |
| 8099 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8100 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8101 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8102 | the kernel to make optimizations.\n\ |
| 8103 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8104 | offset and continuing for len bytes.\n\ |
| 8105 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8106 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8107 | POSIX_FADV_DONTNEED."); |
| 8108 | |
| 8109 | static PyObject * |
| 8110 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8111 | { |
| 8112 | off_t len, offset; |
| 8113 | int res, fd, advice; |
| 8114 | |
| 8115 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8116 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8117 | return NULL; |
| 8118 | |
| 8119 | Py_BEGIN_ALLOW_THREADS |
| 8120 | res = posix_fadvise(fd, offset, len, advice); |
| 8121 | Py_END_ALLOW_THREADS |
| 8122 | if (res != 0) { |
| 8123 | errno = res; |
| 8124 | return posix_error(); |
| 8125 | } |
| 8126 | Py_RETURN_NONE; |
| 8127 | } |
| 8128 | #endif |
| 8129 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8130 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8131 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8132 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8133 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8134 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8135 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8136 | * get re-set with another call for the same key. */ |
| 8137 | static PyObject *posix_putenv_garbage; |
| 8138 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8139 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8140 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8141 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8142 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8143 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8144 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8145 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8146 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8147 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8148 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8149 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8150 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8151 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8152 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8153 | if (newstr == NULL) { |
| 8154 | PyErr_NoMemory(); |
| 8155 | goto error; |
| 8156 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8157 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8158 | PyErr_Format(PyExc_ValueError, |
| 8159 | "the environment variable is longer than %u characters", |
| 8160 | _MAX_ENV); |
| 8161 | goto error; |
| 8162 | } |
| 8163 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8164 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8165 | if (newenv == NULL) |
| 8166 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8167 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8168 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8169 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8170 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8171 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8172 | PyObject *os1, *os2; |
| 8173 | char *s1, *s2; |
| 8174 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8175 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8176 | if (!PyArg_ParseTuple(args, |
| 8177 | "O&O&:putenv", |
| 8178 | PyUnicode_FSConverter, &os1, |
| 8179 | PyUnicode_FSConverter, &os2)) |
| 8180 | return NULL; |
| 8181 | s1 = PyBytes_AsString(os1); |
| 8182 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8183 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8184 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8185 | if (newstr == NULL) { |
| 8186 | PyErr_NoMemory(); |
| 8187 | goto error; |
| 8188 | } |
| 8189 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8190 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8191 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8192 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8193 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8194 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8195 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8196 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8197 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8198 | * this will cause previous value to be collected. This has to |
| 8199 | * happen after the real putenv() call because the old value |
| 8200 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8201 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8202 | /* really not much we can do; just leak */ |
| 8203 | PyErr_Clear(); |
| 8204 | } |
| 8205 | else { |
| 8206 | Py_DECREF(newstr); |
| 8207 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8208 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8209 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8210 | Py_DECREF(os1); |
| 8211 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8212 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8213 | Py_RETURN_NONE; |
| 8214 | |
| 8215 | error: |
| 8216 | #ifndef MS_WINDOWS |
| 8217 | Py_DECREF(os1); |
| 8218 | Py_DECREF(os2); |
| 8219 | #endif |
| 8220 | Py_XDECREF(newstr); |
| 8221 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8222 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8223 | #endif /* putenv */ |
| 8224 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8225 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8226 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8227 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8228 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8229 | |
| 8230 | static PyObject * |
| 8231 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8232 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8233 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8234 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8235 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8236 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8237 | |
| 8238 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8239 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8240 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8241 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8242 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8243 | #ifdef HAVE_BROKEN_UNSETENV |
| 8244 | unsetenv(PyBytes_AS_STRING(name)); |
| 8245 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8246 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8247 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8248 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8249 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8250 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8251 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8252 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8253 | /* Remove the key from posix_putenv_garbage; |
| 8254 | * this will cause it to be collected. This has to |
| 8255 | * happen after the real unsetenv() call because the |
| 8256 | * old value was still accessible until then. |
| 8257 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8258 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8259 | /* really not much we can do; just leak */ |
| 8260 | PyErr_Clear(); |
| 8261 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8262 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8263 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8264 | } |
| 8265 | #endif /* unsetenv */ |
| 8266 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8267 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8268 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8269 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8270 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8271 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8272 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8273 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8274 | int code; |
| 8275 | char *message; |
| 8276 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8277 | return NULL; |
| 8278 | message = strerror(code); |
| 8279 | if (message == NULL) { |
| 8280 | PyErr_SetString(PyExc_ValueError, |
| 8281 | "strerror() argument out of range"); |
| 8282 | return NULL; |
| 8283 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8284 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8285 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8286 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8287 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8288 | #ifdef HAVE_SYS_WAIT_H |
| 8289 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8290 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8291 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8292 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8293 | 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] | 8294 | |
| 8295 | static PyObject * |
| 8296 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 8297 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8298 | WAIT_TYPE status; |
| 8299 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8300 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8301 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 8302 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8303 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8304 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8305 | } |
| 8306 | #endif /* WCOREDUMP */ |
| 8307 | |
| 8308 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8309 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8310 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8311 | 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] | 8312 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8313 | |
| 8314 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8315 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8316 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8317 | WAIT_TYPE status; |
| 8318 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8319 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8320 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 8321 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8322 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8323 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8324 | } |
| 8325 | #endif /* WIFCONTINUED */ |
| 8326 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8327 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8328 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8329 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8330 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8331 | |
| 8332 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8333 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +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; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8337 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8338 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 8339 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8340 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8341 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8342 | } |
| 8343 | #endif /* WIFSTOPPED */ |
| 8344 | |
| 8345 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8346 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8347 | "WIFSIGNALED(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 terminated by a signal."); |
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_WIFSIGNALED(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:WIFSIGNALED", &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(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8360 | } |
| 8361 | #endif /* WIFSIGNALED */ |
| 8362 | |
| 8363 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8364 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8365 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8366 | 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] | 8367 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8368 | |
| 8369 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8370 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8371 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8372 | WAIT_TYPE status; |
| 8373 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8374 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8375 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 8376 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8377 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8378 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8379 | } |
| 8380 | #endif /* WIFEXITED */ |
| 8381 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8382 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8383 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8384 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8385 | Return the process return code from 'status'."); |
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_WEXITSTATUS(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:WEXITSTATUS", &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 Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8397 | } |
| 8398 | #endif /* WEXITSTATUS */ |
| 8399 | |
| 8400 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8401 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8402 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8403 | 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] | 8404 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8405 | |
| 8406 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8407 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8408 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8409 | WAIT_TYPE status; |
| 8410 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8411 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8412 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8413 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8415 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8416 | } |
| 8417 | #endif /* WTERMSIG */ |
| 8418 | |
| 8419 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8420 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8421 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8422 | Return the signal that stopped the process that provided\n\ |
| 8423 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8424 | |
| 8425 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8426 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8427 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8428 | WAIT_TYPE status; |
| 8429 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8430 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8431 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8432 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8433 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8434 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8435 | } |
| 8436 | #endif /* WSTOPSIG */ |
| 8437 | |
| 8438 | #endif /* HAVE_SYS_WAIT_H */ |
| 8439 | |
| 8440 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8441 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8442 | #ifdef _SCO_DS |
| 8443 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8444 | needed definitions in sys/statvfs.h */ |
| 8445 | #define _SVID3 |
| 8446 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8447 | #include <sys/statvfs.h> |
| 8448 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8449 | static PyObject* |
| 8450 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8451 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8452 | if (v == NULL) |
| 8453 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8454 | |
| 8455 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8456 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8457 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8458 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8459 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8460 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8461 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8462 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8463 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8464 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8465 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8466 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8467 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8468 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8469 | PyStructSequence_SET_ITEM(v, 2, |
| 8470 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8471 | PyStructSequence_SET_ITEM(v, 3, |
| 8472 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8473 | PyStructSequence_SET_ITEM(v, 4, |
| 8474 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8475 | PyStructSequence_SET_ITEM(v, 5, |
| 8476 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8477 | PyStructSequence_SET_ITEM(v, 6, |
| 8478 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8479 | PyStructSequence_SET_ITEM(v, 7, |
| 8480 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8481 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8482 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8483 | #endif |
| 8484 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8485 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8486 | } |
| 8487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8488 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8489 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8490 | Perform an fstatvfs system call on the given fd.\n\ |
| 8491 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8492 | |
| 8493 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8494 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8495 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8496 | int fd, res; |
| 8497 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8498 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8499 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8500 | return NULL; |
| 8501 | Py_BEGIN_ALLOW_THREADS |
| 8502 | res = fstatvfs(fd, &st); |
| 8503 | Py_END_ALLOW_THREADS |
| 8504 | if (res != 0) |
| 8505 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8506 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8507 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8508 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8509 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8510 | |
| 8511 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8512 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8513 | #include <sys/statvfs.h> |
| 8514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8515 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8516 | "statvfs(path)\n\n\ |
| 8517 | Perform a statvfs system call on the given path.\n\ |
| 8518 | \n\ |
| 8519 | path may always be specified as a string.\n\ |
| 8520 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8521 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8522 | |
| 8523 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8524 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8525 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8526 | static char *keywords[] = {"path", NULL}; |
| 8527 | path_t path; |
| 8528 | int result; |
| 8529 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8530 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8531 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8532 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8533 | path.function_name = "statvfs"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8534 | #ifdef HAVE_FSTATVFS |
| 8535 | path.allow_fd = 1; |
| 8536 | #endif |
| 8537 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 8538 | path_converter, &path |
| 8539 | )) |
| 8540 | return NULL; |
| 8541 | |
| 8542 | Py_BEGIN_ALLOW_THREADS |
| 8543 | #ifdef HAVE_FSTATVFS |
| 8544 | if (path.fd != -1) { |
| 8545 | #ifdef __APPLE__ |
| 8546 | /* handle weak-linking on Mac OS X 10.3 */ |
| 8547 | if (fstatvfs == NULL) { |
| 8548 | fd_specified("statvfs", path.fd); |
| 8549 | goto exit; |
| 8550 | } |
| 8551 | #endif |
| 8552 | result = fstatvfs(path.fd, &st); |
| 8553 | } |
| 8554 | else |
| 8555 | #endif |
| 8556 | result = statvfs(path.narrow, &st); |
| 8557 | Py_END_ALLOW_THREADS |
| 8558 | |
| 8559 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8560 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8561 | goto exit; |
| 8562 | } |
| 8563 | |
| 8564 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 8565 | |
| 8566 | exit: |
| 8567 | path_cleanup(&path); |
| 8568 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8569 | } |
| 8570 | #endif /* HAVE_STATVFS */ |
| 8571 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8572 | #ifdef MS_WINDOWS |
| 8573 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8574 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8575 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8576 | |
| 8577 | static PyObject * |
| 8578 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8579 | { |
| 8580 | BOOL retval; |
| 8581 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8582 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8583 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8584 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8585 | return NULL; |
| 8586 | |
| 8587 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8588 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8589 | Py_END_ALLOW_THREADS |
| 8590 | if (retval == 0) |
| 8591 | return PyErr_SetFromWindowsErr(0); |
| 8592 | |
| 8593 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8594 | } |
| 8595 | #endif |
| 8596 | |
| 8597 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8598 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8599 | * It maps strings representing configuration variable names to |
| 8600 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8601 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8602 | * rarely-used constants. There are three separate tables that use |
| 8603 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8604 | * |
| 8605 | * This code is always included, even if none of the interfaces that |
| 8606 | * need it are included. The #if hackery needed to avoid it would be |
| 8607 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8608 | */ |
| 8609 | struct constdef { |
| 8610 | char *name; |
| 8611 | long value; |
| 8612 | }; |
| 8613 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8614 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8615 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8616 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8617 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8618 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8619 | *valuep = PyLong_AS_LONG(arg); |
| 8620 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8621 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8622 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8623 | /* look up the value in the table using a binary search */ |
| 8624 | size_t lo = 0; |
| 8625 | size_t mid; |
| 8626 | size_t hi = tablesize; |
| 8627 | int cmp; |
| 8628 | const char *confname; |
| 8629 | if (!PyUnicode_Check(arg)) { |
| 8630 | PyErr_SetString(PyExc_TypeError, |
| 8631 | "configuration names must be strings or integers"); |
| 8632 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8633 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8634 | confname = _PyUnicode_AsString(arg); |
| 8635 | if (confname == NULL) |
| 8636 | return 0; |
| 8637 | while (lo < hi) { |
| 8638 | mid = (lo + hi) / 2; |
| 8639 | cmp = strcmp(confname, table[mid].name); |
| 8640 | if (cmp < 0) |
| 8641 | hi = mid; |
| 8642 | else if (cmp > 0) |
| 8643 | lo = mid + 1; |
| 8644 | else { |
| 8645 | *valuep = table[mid].value; |
| 8646 | return 1; |
| 8647 | } |
| 8648 | } |
| 8649 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8650 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8651 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8652 | } |
| 8653 | |
| 8654 | |
| 8655 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8656 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8657 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8658 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8659 | #endif |
| 8660 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8661 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8662 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8663 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8664 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8665 | #endif |
| 8666 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8667 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8668 | #endif |
| 8669 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8670 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8671 | #endif |
| 8672 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8673 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8674 | #endif |
| 8675 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8676 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8677 | #endif |
| 8678 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8679 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8680 | #endif |
| 8681 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8682 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8683 | #endif |
| 8684 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8685 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8686 | #endif |
| 8687 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8688 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8689 | #endif |
| 8690 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8691 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8692 | #endif |
| 8693 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8694 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8695 | #endif |
| 8696 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8697 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8698 | #endif |
| 8699 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8700 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8701 | #endif |
| 8702 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8703 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8704 | #endif |
| 8705 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8706 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8707 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8708 | #ifdef _PC_ACL_ENABLED |
| 8709 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8710 | #endif |
| 8711 | #ifdef _PC_MIN_HOLE_SIZE |
| 8712 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8713 | #endif |
| 8714 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8715 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8716 | #endif |
| 8717 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8718 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8719 | #endif |
| 8720 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8721 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8722 | #endif |
| 8723 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8724 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8725 | #endif |
| 8726 | #ifdef _PC_REC_XFER_ALIGN |
| 8727 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8728 | #endif |
| 8729 | #ifdef _PC_SYMLINK_MAX |
| 8730 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8731 | #endif |
| 8732 | #ifdef _PC_XATTR_ENABLED |
| 8733 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8734 | #endif |
| 8735 | #ifdef _PC_XATTR_EXISTS |
| 8736 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8737 | #endif |
| 8738 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8739 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8740 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8741 | }; |
| 8742 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8743 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8744 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8745 | { |
| 8746 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8747 | sizeof(posix_constants_pathconf) |
| 8748 | / sizeof(struct constdef)); |
| 8749 | } |
| 8750 | #endif |
| 8751 | |
| 8752 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8753 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8754 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8755 | 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] | 8756 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8757 | |
| 8758 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8759 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8760 | { |
| 8761 | PyObject *result = NULL; |
| 8762 | int name, fd; |
| 8763 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8764 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8765 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8766 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8767 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8768 | errno = 0; |
| 8769 | limit = fpathconf(fd, name); |
| 8770 | if (limit == -1 && errno != 0) |
| 8771 | posix_error(); |
| 8772 | else |
| 8773 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8774 | } |
| 8775 | return result; |
| 8776 | } |
| 8777 | #endif |
| 8778 | |
| 8779 | |
| 8780 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8781 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8782 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8783 | Return the configuration limit name for the file or directory path.\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8784 | If there is no limit, return -1.\n\ |
| 8785 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8786 | If this functionality is unavailable, using it raises an exception."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8787 | |
| 8788 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8789 | posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8790 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8791 | path_t path; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8792 | PyObject *result = NULL; |
| 8793 | int name; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8794 | static char *keywords[] = {"path", "name", NULL}; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8795 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8796 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8797 | path.function_name = "pathconf"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8798 | #ifdef HAVE_FPATHCONF |
| 8799 | path.allow_fd = 1; |
| 8800 | #endif |
| 8801 | if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords, |
| 8802 | path_converter, &path, |
| 8803 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8804 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8806 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8807 | #ifdef HAVE_FPATHCONF |
| 8808 | if (path.fd != -1) |
| 8809 | limit = fpathconf(path.fd, name); |
| 8810 | else |
| 8811 | #endif |
| 8812 | limit = pathconf(path.narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8813 | if (limit == -1 && errno != 0) { |
| 8814 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8815 | /* could be a path or name problem */ |
| 8816 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8817 | else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8818 | result = path_error(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8819 | } |
| 8820 | else |
| 8821 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8822 | } |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8823 | path_cleanup(&path); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8824 | return result; |
| 8825 | } |
| 8826 | #endif |
| 8827 | |
| 8828 | #ifdef HAVE_CONFSTR |
| 8829 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8830 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8831 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8832 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8833 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8834 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8835 | #endif |
| 8836 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8837 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8838 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8839 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8840 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8841 | #endif |
| 8842 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8843 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8844 | #endif |
| 8845 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8846 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8847 | #endif |
| 8848 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8849 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8850 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8851 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8852 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8853 | #endif |
| 8854 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8855 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8856 | #endif |
| 8857 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8858 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8859 | #endif |
| 8860 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8861 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8862 | #endif |
| 8863 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8864 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8865 | #endif |
| 8866 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8867 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8868 | #endif |
| 8869 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8870 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8871 | #endif |
| 8872 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8873 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8874 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8875 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8876 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8877 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8878 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8879 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8880 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8881 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8882 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8883 | #endif |
| 8884 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8885 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8886 | #endif |
| 8887 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8888 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8889 | #endif |
| 8890 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8891 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8892 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8893 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8894 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8895 | #endif |
| 8896 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8897 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8898 | #endif |
| 8899 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8900 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8901 | #endif |
| 8902 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8903 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8904 | #endif |
| 8905 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8906 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8907 | #endif |
| 8908 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8909 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8910 | #endif |
| 8911 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8912 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8913 | #endif |
| 8914 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8915 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8916 | #endif |
| 8917 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8918 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8919 | #endif |
| 8920 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8921 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8922 | #endif |
| 8923 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8924 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8925 | #endif |
| 8926 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8927 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8928 | #endif |
| 8929 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8930 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8931 | #endif |
| 8932 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8933 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8934 | #endif |
| 8935 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8936 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8937 | #endif |
| 8938 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8939 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8940 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8941 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8942 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8943 | #endif |
| 8944 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8945 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8946 | #endif |
| 8947 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8948 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8949 | #endif |
| 8950 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8951 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8952 | #endif |
| 8953 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8954 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8955 | #endif |
| 8956 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8957 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8958 | #endif |
| 8959 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8960 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8961 | #endif |
| 8962 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8963 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8964 | #endif |
| 8965 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8966 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8967 | #endif |
| 8968 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8969 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8970 | #endif |
| 8971 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8972 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8973 | #endif |
| 8974 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8975 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8976 | #endif |
| 8977 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8978 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8979 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8980 | }; |
| 8981 | |
| 8982 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8983 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8984 | { |
| 8985 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8986 | sizeof(posix_constants_confstr) |
| 8987 | / sizeof(struct constdef)); |
| 8988 | } |
| 8989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8990 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8991 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8992 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8993 | |
| 8994 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8995 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8996 | { |
| 8997 | PyObject *result = NULL; |
| 8998 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8999 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9000 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9001 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9002 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 9003 | return NULL; |
| 9004 | |
| 9005 | errno = 0; |
| 9006 | len = confstr(name, buffer, sizeof(buffer)); |
| 9007 | if (len == 0) { |
| 9008 | if (errno) { |
| 9009 | posix_error(); |
| 9010 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9011 | } |
| 9012 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9013 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9014 | } |
| 9015 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9016 | |
| 9017 | if ((unsigned int)len >= sizeof(buffer)) { |
| 9018 | char *buf = PyMem_Malloc(len); |
| 9019 | if (buf == NULL) |
| 9020 | return PyErr_NoMemory(); |
| 9021 | confstr(name, buf, len); |
| 9022 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9023 | PyMem_Free(buf); |
| 9024 | } |
| 9025 | else |
| 9026 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9027 | return result; |
| 9028 | } |
| 9029 | #endif |
| 9030 | |
| 9031 | |
| 9032 | #ifdef HAVE_SYSCONF |
| 9033 | static struct constdef posix_constants_sysconf[] = { |
| 9034 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9035 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9036 | #endif |
| 9037 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9038 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9039 | #endif |
| 9040 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9041 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9042 | #endif |
| 9043 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9044 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9045 | #endif |
| 9046 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9047 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9048 | #endif |
| 9049 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9050 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9051 | #endif |
| 9052 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9053 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9054 | #endif |
| 9055 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9056 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9057 | #endif |
| 9058 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9059 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9060 | #endif |
| 9061 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9062 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9063 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9064 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9065 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9066 | #endif |
| 9067 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9068 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9069 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9070 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9071 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9072 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9073 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9074 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9075 | #endif |
| 9076 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9078 | #endif |
| 9079 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9080 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9081 | #endif |
| 9082 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9084 | #endif |
| 9085 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9086 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9087 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9088 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9089 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9090 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9091 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9092 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9093 | #endif |
| 9094 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9095 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9096 | #endif |
| 9097 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9098 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9099 | #endif |
| 9100 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9101 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9102 | #endif |
| 9103 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9104 | {"SC_BC_STRING_MAX", _SC_BC_STRING_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_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9107 | {"SC_CAP", _SC_CAP}, |
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_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9110 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9111 | #endif |
| 9112 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9113 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9114 | #endif |
| 9115 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9116 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9117 | #endif |
| 9118 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9119 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9120 | #endif |
| 9121 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9123 | #endif |
| 9124 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9125 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9126 | #endif |
| 9127 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9128 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9129 | #endif |
| 9130 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9131 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9132 | #endif |
| 9133 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9134 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9135 | #endif |
| 9136 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9137 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9138 | #endif |
| 9139 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9140 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9141 | #endif |
| 9142 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9143 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9144 | #endif |
| 9145 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9146 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9147 | #endif |
| 9148 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9149 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9150 | #endif |
| 9151 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9152 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9153 | #endif |
| 9154 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9155 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9156 | #endif |
| 9157 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9158 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9159 | #endif |
| 9160 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9161 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9162 | #endif |
| 9163 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9164 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9165 | #endif |
| 9166 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9167 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9168 | #endif |
| 9169 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9170 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9171 | #endif |
| 9172 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9173 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9174 | #endif |
| 9175 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9176 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9177 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9178 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9179 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9180 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9181 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9182 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9183 | #endif |
| 9184 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9185 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9186 | #endif |
| 9187 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9188 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9189 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9190 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9191 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9192 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9193 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9194 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
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_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9197 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9198 | #endif |
| 9199 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9200 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9201 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9202 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9203 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9204 | #endif |
| 9205 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9206 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9207 | #endif |
| 9208 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9209 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9210 | #endif |
| 9211 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9212 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
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_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9215 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9216 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9217 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9218 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9219 | #endif |
| 9220 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9221 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9222 | #endif |
| 9223 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9224 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9225 | #endif |
| 9226 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9227 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9228 | #endif |
| 9229 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9230 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9231 | #endif |
| 9232 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9233 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9234 | #endif |
| 9235 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9236 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9237 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9238 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9239 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9240 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9241 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9242 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9243 | #endif |
| 9244 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9245 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9246 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9247 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9248 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9249 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9250 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9251 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9252 | #endif |
| 9253 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9254 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9255 | #endif |
| 9256 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9258 | #endif |
| 9259 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9260 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9261 | #endif |
| 9262 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9263 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9264 | #endif |
| 9265 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9266 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9267 | #endif |
| 9268 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9269 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9270 | #endif |
| 9271 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9272 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9273 | #endif |
| 9274 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9275 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9276 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9277 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9279 | #endif |
| 9280 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9281 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9282 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9283 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9284 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9285 | #endif |
| 9286 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9287 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9288 | #endif |
| 9289 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9290 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9291 | #endif |
| 9292 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9293 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9294 | #endif |
| 9295 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9296 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9297 | #endif |
| 9298 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9299 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9300 | #endif |
| 9301 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9302 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9303 | #endif |
| 9304 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9305 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9306 | #endif |
| 9307 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9308 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9309 | #endif |
| 9310 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9311 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9312 | #endif |
| 9313 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9314 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9315 | #endif |
| 9316 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9317 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9318 | #endif |
| 9319 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9320 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9321 | #endif |
| 9322 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9323 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9324 | #endif |
| 9325 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9326 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9327 | #endif |
| 9328 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9329 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9330 | #endif |
| 9331 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9332 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9333 | #endif |
| 9334 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9335 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9336 | #endif |
| 9337 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9338 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9339 | #endif |
| 9340 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9341 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9342 | #endif |
| 9343 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9344 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9345 | #endif |
| 9346 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9347 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9348 | #endif |
| 9349 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9350 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9351 | #endif |
| 9352 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9353 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9354 | #endif |
| 9355 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9356 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9357 | #endif |
| 9358 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9359 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9360 | #endif |
| 9361 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9362 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9363 | #endif |
| 9364 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9365 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9366 | #endif |
| 9367 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9368 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9369 | #endif |
| 9370 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9371 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9372 | #endif |
| 9373 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9375 | #endif |
| 9376 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9377 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9378 | #endif |
| 9379 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9380 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9381 | #endif |
| 9382 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9383 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9384 | #endif |
| 9385 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9386 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9387 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9388 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9389 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9390 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9391 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9392 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9393 | #endif |
| 9394 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9395 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9396 | #endif |
| 9397 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9398 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9399 | #endif |
| 9400 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9401 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9402 | #endif |
| 9403 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9404 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9405 | #endif |
| 9406 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9407 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9408 | #endif |
| 9409 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9410 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9411 | #endif |
| 9412 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9413 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9414 | #endif |
| 9415 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9416 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9417 | #endif |
| 9418 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9419 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9420 | #endif |
| 9421 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9422 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9423 | #endif |
| 9424 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9425 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9426 | #endif |
| 9427 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9428 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9429 | #endif |
| 9430 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9431 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9432 | #endif |
| 9433 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9434 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9435 | #endif |
| 9436 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9437 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9438 | #endif |
| 9439 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9440 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9441 | #endif |
| 9442 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9443 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9444 | #endif |
| 9445 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9446 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9447 | #endif |
| 9448 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9449 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9450 | #endif |
| 9451 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9452 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9453 | #endif |
| 9454 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9455 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9456 | #endif |
| 9457 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9458 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9459 | #endif |
| 9460 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9461 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9462 | #endif |
| 9463 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9464 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9465 | #endif |
| 9466 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9467 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9468 | #endif |
| 9469 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9470 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9471 | #endif |
| 9472 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9473 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9474 | #endif |
| 9475 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9476 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9477 | #endif |
| 9478 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9479 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9480 | #endif |
| 9481 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9482 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9483 | #endif |
| 9484 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9485 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9486 | #endif |
| 9487 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9488 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9489 | #endif |
| 9490 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9491 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9492 | #endif |
| 9493 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9494 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9495 | #endif |
| 9496 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9497 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9498 | #endif |
| 9499 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9500 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9501 | #endif |
| 9502 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9503 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9504 | #endif |
| 9505 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9506 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9507 | #endif |
| 9508 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9509 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9510 | #endif |
| 9511 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9512 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9513 | #endif |
| 9514 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9515 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9516 | #endif |
| 9517 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9518 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9519 | #endif |
| 9520 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9521 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9522 | #endif |
| 9523 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9524 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9525 | #endif |
| 9526 | }; |
| 9527 | |
| 9528 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9529 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9530 | { |
| 9531 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9532 | sizeof(posix_constants_sysconf) |
| 9533 | / sizeof(struct constdef)); |
| 9534 | } |
| 9535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9536 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9537 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9538 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9539 | |
| 9540 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9541 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9542 | { |
| 9543 | PyObject *result = NULL; |
| 9544 | int name; |
| 9545 | |
| 9546 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9547 | int value; |
| 9548 | |
| 9549 | errno = 0; |
| 9550 | value = sysconf(name); |
| 9551 | if (value == -1 && errno != 0) |
| 9552 | posix_error(); |
| 9553 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9554 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9555 | } |
| 9556 | return result; |
| 9557 | } |
| 9558 | #endif |
| 9559 | |
| 9560 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9561 | /* This code is used to ensure that the tables of configuration value names |
| 9562 | * are in sorted order as required by conv_confname(), and also to build the |
| 9563 | * the exported dictionaries that are used to publish information about the |
| 9564 | * names available on the host platform. |
| 9565 | * |
| 9566 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9567 | * when used, even for platforms we're not able to test on. It also makes |
| 9568 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9569 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9570 | |
| 9571 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9572 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9573 | { |
| 9574 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9575 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9576 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9577 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9578 | |
| 9579 | return strcmp(c1->name, c2->name); |
| 9580 | } |
| 9581 | |
| 9582 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9583 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9584 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9585 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9586 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9587 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9588 | |
| 9589 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9590 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9591 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9592 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9593 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9594 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9595 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9596 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9597 | Py_XDECREF(o); |
| 9598 | Py_DECREF(d); |
| 9599 | return -1; |
| 9600 | } |
| 9601 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9602 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9603 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9604 | } |
| 9605 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9606 | /* Return -1 on failure, 0 on success. */ |
| 9607 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9608 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9609 | { |
| 9610 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9611 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9612 | sizeof(posix_constants_pathconf) |
| 9613 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9614 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9615 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9616 | #endif |
| 9617 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9618 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9619 | sizeof(posix_constants_confstr) |
| 9620 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9621 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9622 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9623 | #endif |
| 9624 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9625 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9626 | sizeof(posix_constants_sysconf) |
| 9627 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9628 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9629 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9630 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9631 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9632 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9633 | |
| 9634 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9635 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9636 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9637 | 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] | 9638 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9639 | |
| 9640 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9641 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9642 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9643 | abort(); |
| 9644 | /*NOTREACHED*/ |
| 9645 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9646 | return NULL; |
| 9647 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9648 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9649 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9650 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9651 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9652 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9653 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9654 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9655 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9656 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9657 | application (if any) its extension is associated.\n\ |
| 9658 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9659 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9660 | \n\ |
| 9661 | startfile returns as soon as the associated application is launched.\n\ |
| 9662 | There is no option to wait for the application to close, and no way\n\ |
| 9663 | to retrieve the application's exit status.\n\ |
| 9664 | \n\ |
| 9665 | The filepath is relative to the current directory. If you want to use\n\ |
| 9666 | 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] | 9667 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9668 | |
| 9669 | static PyObject * |
| 9670 | win32_startfile(PyObject *self, PyObject *args) |
| 9671 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9672 | PyObject *ofilepath; |
| 9673 | char *filepath; |
| 9674 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9675 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9676 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9677 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9678 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9679 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9680 | &unipath, &operation)) { |
| 9681 | PyErr_Clear(); |
| 9682 | goto normal; |
| 9683 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9684 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9685 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9686 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9687 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9688 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9689 | PyErr_Clear(); |
| 9690 | operation = NULL; |
| 9691 | goto normal; |
| 9692 | } |
| 9693 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9694 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9695 | wpath = PyUnicode_AsUnicode(unipath); |
| 9696 | if (wpath == NULL) |
| 9697 | goto normal; |
| 9698 | if (uoperation) { |
| 9699 | woperation = PyUnicode_AsUnicode(uoperation); |
| 9700 | if (woperation == NULL) |
| 9701 | goto normal; |
| 9702 | } |
| 9703 | else |
| 9704 | woperation = NULL; |
| 9705 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9706 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9707 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 9708 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9709 | Py_END_ALLOW_THREADS |
| 9710 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9711 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9712 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9713 | win32_error_object("startfile", unipath); |
| 9714 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9715 | } |
| 9716 | Py_INCREF(Py_None); |
| 9717 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9718 | |
| 9719 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9720 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9721 | PyUnicode_FSConverter, &ofilepath, |
| 9722 | &operation)) |
| 9723 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 9724 | if (win32_warn_bytes_api()) { |
| 9725 | Py_DECREF(ofilepath); |
| 9726 | return NULL; |
| 9727 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9728 | filepath = PyBytes_AsString(ofilepath); |
| 9729 | Py_BEGIN_ALLOW_THREADS |
| 9730 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9731 | NULL, NULL, SW_SHOWNORMAL); |
| 9732 | Py_END_ALLOW_THREADS |
| 9733 | if (rc <= (HINSTANCE)32) { |
| 9734 | PyObject *errval = win32_error("startfile", filepath); |
| 9735 | Py_DECREF(ofilepath); |
| 9736 | return errval; |
| 9737 | } |
| 9738 | Py_DECREF(ofilepath); |
| 9739 | Py_INCREF(Py_None); |
| 9740 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9741 | } |
| 9742 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9743 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9744 | #ifdef HAVE_GETLOADAVG |
| 9745 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9746 | "getloadavg() -> (float, float, float)\n\n\ |
| 9747 | Return the number of processes in the system run queue averaged over\n\ |
| 9748 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9749 | was unobtainable"); |
| 9750 | |
| 9751 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9752 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9753 | { |
| 9754 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9755 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9756 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9757 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9758 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9759 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9760 | } |
| 9761 | #endif |
| 9762 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9763 | PyDoc_STRVAR(device_encoding__doc__, |
| 9764 | "device_encoding(fd) -> str\n\n\ |
| 9765 | Return a string describing the encoding of the device\n\ |
| 9766 | if the output is a terminal; else return None."); |
| 9767 | |
| 9768 | static PyObject * |
| 9769 | device_encoding(PyObject *self, PyObject *args) |
| 9770 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9771 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9772 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9773 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9774 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9775 | |
| 9776 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9777 | } |
| 9778 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9779 | #ifdef HAVE_SETRESUID |
| 9780 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9781 | "setresuid(ruid, euid, suid)\n\n\ |
| 9782 | Set the current process's real, effective, and saved user ids."); |
| 9783 | |
| 9784 | static PyObject* |
| 9785 | posix_setresuid (PyObject *self, PyObject *args) |
| 9786 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9787 | /* We assume uid_t is no larger than a long. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9788 | uid_t ruid, euid, suid; |
| 9789 | if (!PyArg_ParseTuple(args, "O&O&O&:setresuid", |
| 9790 | _Py_Uid_Converter, &ruid, |
| 9791 | _Py_Uid_Converter, &euid, |
| 9792 | _Py_Uid_Converter, &suid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9793 | return NULL; |
| 9794 | if (setresuid(ruid, euid, suid) < 0) |
| 9795 | return posix_error(); |
| 9796 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9797 | } |
| 9798 | #endif |
| 9799 | |
| 9800 | #ifdef HAVE_SETRESGID |
| 9801 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9802 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9803 | Set the current process's real, effective, and saved group ids."); |
| 9804 | |
| 9805 | static PyObject* |
| 9806 | posix_setresgid (PyObject *self, PyObject *args) |
| 9807 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9808 | gid_t rgid, egid, sgid; |
| 9809 | if (!PyArg_ParseTuple(args, "O&O&O&:setresgid", |
| 9810 | _Py_Gid_Converter, &rgid, |
| 9811 | _Py_Gid_Converter, &egid, |
| 9812 | _Py_Gid_Converter, &sgid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9813 | return NULL; |
| 9814 | if (setresgid(rgid, egid, sgid) < 0) |
| 9815 | return posix_error(); |
| 9816 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9817 | } |
| 9818 | #endif |
| 9819 | |
| 9820 | #ifdef HAVE_GETRESUID |
| 9821 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9822 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9823 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9824 | |
| 9825 | static PyObject* |
| 9826 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9827 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9828 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9829 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9830 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9831 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 9832 | _PyLong_FromUid(euid), |
| 9833 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9834 | } |
| 9835 | #endif |
| 9836 | |
| 9837 | #ifdef HAVE_GETRESGID |
| 9838 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9839 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9840 | 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] | 9841 | |
| 9842 | static PyObject* |
| 9843 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9844 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9845 | uid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9846 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9847 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9848 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 9849 | _PyLong_FromGid(egid), |
| 9850 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9851 | } |
| 9852 | #endif |
| 9853 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 9854 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9855 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9856 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9857 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 9858 | Return the value of extended attribute attribute on path.\n\ |
| 9859 | \n\ |
| 9860 | path may be either a string or an open file descriptor.\n\ |
| 9861 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 9862 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 9863 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9864 | |
| 9865 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9866 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9867 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9868 | path_t path; |
| 9869 | path_t attribute; |
| 9870 | int follow_symlinks = 1; |
| 9871 | PyObject *buffer = NULL; |
| 9872 | int i; |
| 9873 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9874 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9875 | memset(&path, 0, sizeof(path)); |
| 9876 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9877 | path.function_name = "getxattr"; |
| 9878 | attribute.function_name = "getxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9879 | path.allow_fd = 1; |
| 9880 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 9881 | path_converter, &path, |
| 9882 | path_converter, &attribute, |
| 9883 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9884 | return NULL; |
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 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 9887 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9888 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9889 | for (i = 0; ; i++) { |
| 9890 | void *ptr; |
| 9891 | ssize_t result; |
| 9892 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 9893 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 9894 | if (!buffer_size) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9895 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9896 | goto exit; |
| 9897 | } |
| 9898 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 9899 | if (!buffer) |
| 9900 | goto exit; |
| 9901 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9902 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9903 | Py_BEGIN_ALLOW_THREADS; |
| 9904 | if (path.fd >= 0) |
| 9905 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 9906 | else if (follow_symlinks) |
| 9907 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 9908 | else |
| 9909 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 9910 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9911 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9912 | if (result < 0) { |
| 9913 | Py_DECREF(buffer); |
| 9914 | buffer = NULL; |
| 9915 | if (errno == ERANGE) |
| 9916 | continue; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9917 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9918 | goto exit; |
| 9919 | } |
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 | if (result != buffer_size) { |
| 9922 | /* Can only shrink. */ |
| 9923 | _PyBytes_Resize(&buffer, result); |
| 9924 | } |
| 9925 | break; |
| 9926 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9927 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9928 | exit: |
| 9929 | path_cleanup(&path); |
| 9930 | path_cleanup(&attribute); |
| 9931 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9932 | } |
| 9933 | |
| 9934 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9935 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 9936 | Set extended attribute attribute on path to value.\n\ |
| 9937 | path may be either a string or an open file descriptor.\n\ |
| 9938 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 9939 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 9940 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9941 | |
| 9942 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9943 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9944 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9945 | path_t path; |
| 9946 | path_t attribute; |
| 9947 | Py_buffer value; |
| 9948 | int flags = 0; |
| 9949 | int follow_symlinks = 1; |
| 9950 | int result; |
| 9951 | PyObject *return_value = NULL; |
| 9952 | static char *keywords[] = {"path", "attribute", "value", |
| 9953 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9954 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9955 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9956 | path.function_name = "setxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9957 | path.allow_fd = 1; |
| 9958 | memset(&attribute, 0, sizeof(attribute)); |
| 9959 | memset(&value, 0, sizeof(value)); |
| 9960 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 9961 | keywords, |
| 9962 | path_converter, &path, |
| 9963 | path_converter, &attribute, |
| 9964 | &value, &flags, |
| 9965 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9966 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9967 | |
| 9968 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 9969 | goto exit; |
| 9970 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9971 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9972 | if (path.fd > -1) |
| 9973 | result = fsetxattr(path.fd, attribute.narrow, |
| 9974 | value.buf, value.len, flags); |
| 9975 | else if (follow_symlinks) |
| 9976 | result = setxattr(path.narrow, attribute.narrow, |
| 9977 | value.buf, value.len, flags); |
| 9978 | else |
| 9979 | result = lsetxattr(path.narrow, attribute.narrow, |
| 9980 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9981 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9982 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9983 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9984 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9985 | goto exit; |
| 9986 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9987 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9988 | return_value = Py_None; |
| 9989 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9990 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9991 | exit: |
| 9992 | path_cleanup(&path); |
| 9993 | path_cleanup(&attribute); |
| 9994 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9995 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9996 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9997 | } |
| 9998 | |
| 9999 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10000 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 10001 | Remove extended attribute attribute on path.\n\ |
| 10002 | path may be either a string or an open file descriptor.\n\ |
| 10003 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10004 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 10005 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10006 | |
| 10007 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10008 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10009 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10010 | path_t path; |
| 10011 | path_t attribute; |
| 10012 | int follow_symlinks = 1; |
| 10013 | int result; |
| 10014 | PyObject *return_value = NULL; |
| 10015 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10016 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10017 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10018 | path.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10019 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10020 | attribute.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10021 | path.allow_fd = 1; |
| 10022 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 10023 | keywords, |
| 10024 | path_converter, &path, |
| 10025 | path_converter, &attribute, |
| 10026 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10027 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10028 | |
| 10029 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 10030 | goto exit; |
| 10031 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10032 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10033 | if (path.fd > -1) |
| 10034 | result = fremovexattr(path.fd, attribute.narrow); |
| 10035 | else if (follow_symlinks) |
| 10036 | result = removexattr(path.narrow, attribute.narrow); |
| 10037 | else |
| 10038 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10039 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10040 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10041 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10042 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10043 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10044 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10045 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10046 | return_value = Py_None; |
| 10047 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10048 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10049 | exit: |
| 10050 | path_cleanup(&path); |
| 10051 | path_cleanup(&attribute); |
| 10052 | |
| 10053 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10054 | } |
| 10055 | |
| 10056 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10057 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 10058 | Return a list of extended attributes on path.\n\ |
| 10059 | \n\ |
| 10060 | path may be either None, a string, or an open file descriptor.\n\ |
| 10061 | if path is None, listxattr will examine the current directory.\n\ |
| 10062 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10063 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 10064 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10065 | |
| 10066 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10067 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10068 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10069 | path_t path; |
| 10070 | int follow_symlinks = 1; |
| 10071 | Py_ssize_t i; |
| 10072 | PyObject *result = NULL; |
| 10073 | char *buffer = NULL; |
| 10074 | char *name; |
| 10075 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10076 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10077 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10078 | path.function_name = "listxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10079 | path.allow_fd = 1; |
| 10080 | path.fd = -1; |
| 10081 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 10082 | path_converter, &path, |
| 10083 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10084 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10085 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10086 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 10087 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10088 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10089 | name = path.narrow ? path.narrow : "."; |
| 10090 | for (i = 0; ; i++) { |
| 10091 | char *start, *trace, *end; |
| 10092 | ssize_t length; |
| 10093 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10094 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10095 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10096 | /* ERANGE */ |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10097 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10098 | break; |
| 10099 | } |
| 10100 | buffer = PyMem_MALLOC(buffer_size); |
| 10101 | if (!buffer) { |
| 10102 | PyErr_NoMemory(); |
| 10103 | break; |
| 10104 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10105 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10106 | Py_BEGIN_ALLOW_THREADS; |
| 10107 | if (path.fd > -1) |
| 10108 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10109 | else if (follow_symlinks) |
| 10110 | length = listxattr(name, buffer, buffer_size); |
| 10111 | else |
| 10112 | length = llistxattr(name, buffer, buffer_size); |
| 10113 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10114 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10115 | if (length < 0) { |
| 10116 | if (errno == ERANGE) |
| 10117 | continue; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10118 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10119 | break; |
| 10120 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10121 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10122 | result = PyList_New(0); |
| 10123 | if (!result) { |
| 10124 | goto exit; |
| 10125 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10126 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10127 | end = buffer + length; |
| 10128 | for (trace = start = buffer; trace != end; trace++) { |
| 10129 | if (!*trace) { |
| 10130 | int error; |
| 10131 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10132 | trace - start); |
| 10133 | if (!attribute) { |
| 10134 | Py_DECREF(result); |
| 10135 | result = NULL; |
| 10136 | goto exit; |
| 10137 | } |
| 10138 | error = PyList_Append(result, attribute); |
| 10139 | Py_DECREF(attribute); |
| 10140 | if (error) { |
| 10141 | Py_DECREF(result); |
| 10142 | result = NULL; |
| 10143 | goto exit; |
| 10144 | } |
| 10145 | start = trace + 1; |
| 10146 | } |
| 10147 | } |
| 10148 | break; |
| 10149 | } |
| 10150 | exit: |
| 10151 | path_cleanup(&path); |
| 10152 | if (buffer) |
| 10153 | PyMem_FREE(buffer); |
| 10154 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10155 | } |
| 10156 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10157 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10158 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10159 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10160 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10161 | "urandom(n) -> str\n\n\ |
| 10162 | Return n random bytes suitable for cryptographic use."); |
| 10163 | |
| 10164 | static PyObject * |
| 10165 | posix_urandom(PyObject *self, PyObject *args) |
| 10166 | { |
| 10167 | Py_ssize_t size; |
| 10168 | PyObject *result; |
| 10169 | int ret; |
| 10170 | |
| 10171 | /* Read arguments */ |
| 10172 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10173 | return NULL; |
| 10174 | if (size < 0) |
| 10175 | return PyErr_Format(PyExc_ValueError, |
| 10176 | "negative argument not allowed"); |
| 10177 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10178 | if (result == NULL) |
| 10179 | return NULL; |
| 10180 | |
| 10181 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10182 | PyBytes_GET_SIZE(result)); |
| 10183 | if (ret == -1) { |
| 10184 | Py_DECREF(result); |
| 10185 | return NULL; |
| 10186 | } |
| 10187 | return result; |
| 10188 | } |
| 10189 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10190 | /* Terminal size querying */ |
| 10191 | |
| 10192 | static PyTypeObject TerminalSizeType; |
| 10193 | |
| 10194 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10195 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10196 | |
| 10197 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10198 | {"columns", "width of the terminal window in characters"}, |
| 10199 | {"lines", "height of the terminal window in characters"}, |
| 10200 | {NULL, NULL} |
| 10201 | }; |
| 10202 | |
| 10203 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10204 | "os.terminal_size", |
| 10205 | TerminalSize_docstring, |
| 10206 | TerminalSize_fields, |
| 10207 | 2, |
| 10208 | }; |
| 10209 | |
| 10210 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10211 | PyDoc_STRVAR(termsize__doc__, |
| 10212 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10213 | "\n" \ |
| 10214 | "The optional argument fd (default standard output) specifies\n" \ |
| 10215 | "which file descriptor should be queried.\n" \ |
| 10216 | "\n" \ |
| 10217 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10218 | "is thrown.\n" \ |
| 10219 | "\n" \ |
| 10220 | "This function will only be defined if an implementation is\n" \ |
| 10221 | "available for this system.\n" \ |
| 10222 | "\n" \ |
| 10223 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10224 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10225 | |
| 10226 | static PyObject* |
| 10227 | get_terminal_size(PyObject *self, PyObject *args) |
| 10228 | { |
| 10229 | int columns, lines; |
| 10230 | PyObject *termsize; |
| 10231 | |
| 10232 | int fd = fileno(stdout); |
| 10233 | /* Under some conditions stdout may not be connected and |
| 10234 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10235 | * GUI apps don't have valid standard streams by default. |
| 10236 | * |
| 10237 | * If this happens, and the optional fd argument is not present, |
| 10238 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10239 | */ |
| 10240 | |
| 10241 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10242 | return NULL; |
| 10243 | |
| 10244 | #ifdef TERMSIZE_USE_IOCTL |
| 10245 | { |
| 10246 | struct winsize w; |
| 10247 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10248 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10249 | columns = w.ws_col; |
| 10250 | lines = w.ws_row; |
| 10251 | } |
| 10252 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10253 | |
| 10254 | #ifdef TERMSIZE_USE_CONIO |
| 10255 | { |
| 10256 | DWORD nhandle; |
| 10257 | HANDLE handle; |
| 10258 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10259 | switch (fd) { |
| 10260 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10261 | break; |
| 10262 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10263 | break; |
| 10264 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10265 | break; |
| 10266 | default: |
| 10267 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10268 | } |
| 10269 | handle = GetStdHandle(nhandle); |
| 10270 | if (handle == NULL) |
| 10271 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10272 | if (handle == INVALID_HANDLE_VALUE) |
| 10273 | return PyErr_SetFromWindowsErr(0); |
| 10274 | |
| 10275 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10276 | return PyErr_SetFromWindowsErr(0); |
| 10277 | |
| 10278 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10279 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10280 | } |
| 10281 | #endif /* TERMSIZE_USE_CONIO */ |
| 10282 | |
| 10283 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10284 | if (termsize == NULL) |
| 10285 | return NULL; |
| 10286 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10287 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10288 | if (PyErr_Occurred()) { |
| 10289 | Py_DECREF(termsize); |
| 10290 | return NULL; |
| 10291 | } |
| 10292 | return termsize; |
| 10293 | } |
| 10294 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10295 | |
| 10296 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10297 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10298 | {"access", (PyCFunction)posix_access, |
| 10299 | METH_VARARGS | METH_KEYWORDS, |
| 10300 | posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10301 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10302 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10303 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10304 | {"chdir", (PyCFunction)posix_chdir, |
| 10305 | METH_VARARGS | METH_KEYWORDS, |
| 10306 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10307 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10308 | {"chflags", (PyCFunction)posix_chflags, |
| 10309 | METH_VARARGS | METH_KEYWORDS, |
| 10310 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10311 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10312 | {"chmod", (PyCFunction)posix_chmod, |
| 10313 | METH_VARARGS | METH_KEYWORDS, |
| 10314 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10315 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10316 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10317 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10318 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10319 | {"chown", (PyCFunction)posix_chown, |
| 10320 | METH_VARARGS | METH_KEYWORDS, |
| 10321 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10322 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10323 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10324 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10325 | #endif /* HAVE_LCHMOD */ |
| 10326 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10327 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10328 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10329 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10330 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10331 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10332 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10333 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10334 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10335 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10336 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10337 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10338 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10339 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10340 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10341 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10342 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10343 | METH_NOARGS, posix_getcwd__doc__}, |
| 10344 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10345 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10346 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10347 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 10348 | {"link", (PyCFunction)posix_link, |
| 10349 | METH_VARARGS | METH_KEYWORDS, |
| 10350 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10351 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10352 | {"listdir", (PyCFunction)posix_listdir, |
| 10353 | METH_VARARGS | METH_KEYWORDS, |
| 10354 | posix_listdir__doc__}, |
| 10355 | {"lstat", (PyCFunction)posix_lstat, |
| 10356 | METH_VARARGS | METH_KEYWORDS, |
| 10357 | posix_lstat__doc__}, |
| 10358 | {"mkdir", (PyCFunction)posix_mkdir, |
| 10359 | METH_VARARGS | METH_KEYWORDS, |
| 10360 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10361 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10362 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10363 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10364 | #ifdef HAVE_GETPRIORITY |
| 10365 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10366 | #endif /* HAVE_GETPRIORITY */ |
| 10367 | #ifdef HAVE_SETPRIORITY |
| 10368 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10369 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10370 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10371 | {"readlink", (PyCFunction)posix_readlink, |
| 10372 | METH_VARARGS | METH_KEYWORDS, |
| 10373 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10374 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10375 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10376 | {"readlink", (PyCFunction)win_readlink, |
| 10377 | METH_VARARGS | METH_KEYWORDS, |
| 10378 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10379 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10380 | {"rename", (PyCFunction)posix_rename, |
| 10381 | METH_VARARGS | METH_KEYWORDS, |
| 10382 | posix_rename__doc__}, |
| 10383 | {"replace", (PyCFunction)posix_replace, |
| 10384 | METH_VARARGS | METH_KEYWORDS, |
| 10385 | posix_replace__doc__}, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 10386 | {"rmdir", (PyCFunction)posix_rmdir, |
| 10387 | METH_VARARGS | METH_KEYWORDS, |
| 10388 | posix_rmdir__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10389 | {"stat", (PyCFunction)posix_stat, |
| 10390 | METH_VARARGS | METH_KEYWORDS, |
| 10391 | posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10392 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10393 | #if defined(HAVE_SYMLINK) |
| 10394 | {"symlink", (PyCFunction)posix_symlink, |
| 10395 | METH_VARARGS | METH_KEYWORDS, |
| 10396 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10397 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10398 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10399 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10400 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10401 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10402 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10403 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10404 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10405 | {"unlink", (PyCFunction)posix_unlink, |
| 10406 | METH_VARARGS | METH_KEYWORDS, |
| 10407 | posix_unlink__doc__}, |
| 10408 | {"remove", (PyCFunction)posix_unlink, |
| 10409 | METH_VARARGS | METH_KEYWORDS, |
| 10410 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10411 | {"utime", (PyCFunction)posix_utime, |
| 10412 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10413 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10414 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10415 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10416 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10417 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10418 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10419 | {"execve", (PyCFunction)posix_execve, |
| 10420 | METH_VARARGS | METH_KEYWORDS, |
| 10421 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10422 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10423 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10424 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10425 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10426 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10427 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10428 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10429 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10430 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10431 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10432 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10433 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10434 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10435 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10436 | {"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] | 10437 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10438 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10439 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10440 | #endif |
| 10441 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10442 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10443 | #endif |
| 10444 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10445 | {"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] | 10446 | #endif |
| 10447 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10448 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10449 | #endif |
| 10450 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10451 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10452 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10453 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10454 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10455 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10456 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10457 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10458 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10459 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10460 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10461 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10462 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10463 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10464 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10465 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10466 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 10467 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10468 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10469 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 10470 | #endif /* HAVE_GETEUID */ |
| 10471 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10472 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10473 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10474 | #ifdef HAVE_GETGROUPLIST |
| 10475 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10476 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10477 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10478 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10479 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10480 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10481 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10482 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10483 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10484 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10485 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10486 | #endif /* HAVE_GETPPID */ |
| 10487 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10488 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10489 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10490 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10491 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10492 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10493 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10494 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10495 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10496 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10497 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10498 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10499 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10500 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10501 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10502 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10503 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10504 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10505 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10506 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10507 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10508 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10509 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10510 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10511 | #endif /* HAVE_SETEUID */ |
| 10512 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10513 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10514 | #endif /* HAVE_SETEGID */ |
| 10515 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10516 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10517 | #endif /* HAVE_SETREUID */ |
| 10518 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10519 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10520 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10521 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10522 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10523 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10524 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10525 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10526 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10527 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10528 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10529 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10530 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10531 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10532 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10533 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10534 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10535 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10536 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10537 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10538 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10539 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10540 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10541 | #endif /* HAVE_WAIT3 */ |
| 10542 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10543 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10544 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10545 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10546 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10547 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10548 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10549 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10550 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10551 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10552 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10553 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10554 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10555 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10556 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10557 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10558 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10559 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10560 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10561 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10562 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10563 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10564 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10565 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10566 | {"open", (PyCFunction)posix_open,\ |
| 10567 | METH_VARARGS | METH_KEYWORDS, |
| 10568 | posix_open__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10569 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10570 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10571 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10572 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10573 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10574 | #ifdef HAVE_LOCKF |
| 10575 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10576 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10577 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10578 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10579 | #ifdef HAVE_READV |
| 10580 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10581 | #endif |
| 10582 | #ifdef HAVE_PREAD |
| 10583 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10584 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10585 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10586 | #ifdef HAVE_WRITEV |
| 10587 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10588 | #endif |
| 10589 | #ifdef HAVE_PWRITE |
| 10590 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10591 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10592 | #ifdef HAVE_SENDFILE |
| 10593 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10594 | posix_sendfile__doc__}, |
| 10595 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10596 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10597 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10598 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10599 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10600 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10601 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10602 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10603 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10604 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10605 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 10606 | METH_VARARGS | METH_KEYWORDS, |
| 10607 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10608 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10609 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10610 | {"mknod", (PyCFunction)posix_mknod, |
| 10611 | METH_VARARGS | METH_KEYWORDS, |
| 10612 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10613 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10614 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10615 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10616 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10617 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10618 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10619 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10620 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10621 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10622 | #ifdef HAVE_TRUNCATE |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10623 | {"truncate", (PyCFunction)posix_truncate, |
| 10624 | METH_VARARGS | METH_KEYWORDS, |
| 10625 | posix_truncate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10626 | #endif |
| 10627 | #ifdef HAVE_POSIX_FALLOCATE |
| 10628 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10629 | #endif |
| 10630 | #ifdef HAVE_POSIX_FADVISE |
| 10631 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10632 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10633 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10634 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10635 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10636 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10637 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10638 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10639 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10640 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10641 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10642 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10643 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10644 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10645 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10646 | #ifdef HAVE_SYNC |
| 10647 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10648 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10649 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10650 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10651 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10652 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10653 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10654 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10655 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10656 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10657 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10658 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10659 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10660 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10661 | #endif /* WIFSTOPPED */ |
| 10662 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10663 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10664 | #endif /* WIFSIGNALED */ |
| 10665 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10666 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10667 | #endif /* WIFEXITED */ |
| 10668 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10669 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10670 | #endif /* WEXITSTATUS */ |
| 10671 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10672 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10673 | #endif /* WTERMSIG */ |
| 10674 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10676 | #endif /* WSTOPSIG */ |
| 10677 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10678 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10679 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10680 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10681 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10682 | {"statvfs", (PyCFunction)posix_statvfs, |
| 10683 | METH_VARARGS | METH_KEYWORDS, |
| 10684 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10685 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10686 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10687 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10688 | #endif |
| 10689 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10691 | #endif |
| 10692 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10693 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10694 | #endif |
| 10695 | #ifdef HAVE_PATHCONF |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10696 | {"pathconf", (PyCFunction)posix_pathconf, |
| 10697 | METH_VARARGS | METH_KEYWORDS, |
| 10698 | posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10699 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10700 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10701 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10702 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10703 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10704 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10705 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10706 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10707 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10708 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10709 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 10710 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10711 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10712 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10713 | #endif |
| 10714 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10715 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10716 | #endif |
| 10717 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10718 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10719 | #endif |
| 10720 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10721 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10722 | #endif |
| 10723 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10724 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10725 | {"setxattr", (PyCFunction)posix_setxattr, |
| 10726 | METH_VARARGS | METH_KEYWORDS, |
| 10727 | posix_setxattr__doc__}, |
| 10728 | {"getxattr", (PyCFunction)posix_getxattr, |
| 10729 | METH_VARARGS | METH_KEYWORDS, |
| 10730 | posix_getxattr__doc__}, |
| 10731 | {"removexattr", (PyCFunction)posix_removexattr, |
| 10732 | METH_VARARGS | METH_KEYWORDS, |
| 10733 | posix_removexattr__doc__}, |
| 10734 | {"listxattr", (PyCFunction)posix_listxattr, |
| 10735 | METH_VARARGS | METH_KEYWORDS, |
| 10736 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10737 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10738 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10739 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 10740 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10741 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10742 | }; |
| 10743 | |
| 10744 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10745 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10746 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10747 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10748 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10749 | } |
| 10750 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10751 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10752 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10753 | enable_symlink() |
| 10754 | { |
| 10755 | HANDLE tok; |
| 10756 | TOKEN_PRIVILEGES tok_priv; |
| 10757 | LUID luid; |
| 10758 | int meth_idx = 0; |
| 10759 | |
| 10760 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10761 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10762 | |
| 10763 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10764 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10765 | |
| 10766 | tok_priv.PrivilegeCount = 1; |
| 10767 | tok_priv.Privileges[0].Luid = luid; |
| 10768 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 10769 | |
| 10770 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 10771 | sizeof(TOKEN_PRIVILEGES), |
| 10772 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10773 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10774 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10775 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 10776 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10777 | } |
| 10778 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 10779 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10780 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 10781 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10782 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10783 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10784 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10785 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10786 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10787 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10788 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10789 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10790 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10791 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10792 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10793 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10794 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10795 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10796 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10797 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10798 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10799 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10800 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10801 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10802 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10803 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10804 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10805 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10806 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10807 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10808 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10809 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10810 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10811 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10812 | #endif |
| 10813 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10814 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10815 | #endif |
| 10816 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10817 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10818 | #endif |
| 10819 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10820 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10821 | #endif |
| 10822 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10823 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10824 | #endif |
| 10825 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10826 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10827 | #endif |
| 10828 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10829 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10830 | #endif |
| 10831 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10832 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10833 | #endif |
| 10834 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10835 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10836 | #endif |
| 10837 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10838 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10839 | #endif |
| 10840 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10841 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10842 | #endif |
| 10843 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10844 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10845 | #endif |
| 10846 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10847 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10848 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10849 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10850 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10851 | #endif |
| 10852 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10853 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10854 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10855 | #ifdef O_XATTR |
| 10856 | if (ins(d, "O_XATTR", (long)O_XATTR)) return -1; |
| 10857 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10858 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10859 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10860 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10861 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10862 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10863 | #endif |
| 10864 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10865 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10866 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 10867 | #ifdef O_EXEC |
| 10868 | if (ins(d, "O_EXEC", (long)O_EXEC)) return -1; |
| 10869 | #endif |
| 10870 | #ifdef O_SEARCH |
| 10871 | if (ins(d, "O_SEARCH", (long)O_SEARCH)) return -1; |
| 10872 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 10873 | #ifdef O_PATH |
| 10874 | if (ins(d, "O_PATH", (long)O_PATH)) return -1; |
| 10875 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 10876 | #ifdef O_TTY_INIT |
| 10877 | if (ins(d, "O_TTY_INIT", (long)O_TTY_INIT)) return -1; |
| 10878 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10879 | #ifdef PRIO_PROCESS |
| 10880 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 10881 | #endif |
| 10882 | #ifdef PRIO_PGRP |
| 10883 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 10884 | #endif |
| 10885 | #ifdef PRIO_USER |
| 10886 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 10887 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 10888 | #ifdef O_CLOEXEC |
| 10889 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 10890 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10891 | #ifdef O_ACCMODE |
| 10892 | if (ins(d, "O_ACCMODE", (long)O_ACCMODE)) return -1; |
| 10893 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10894 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10895 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 10896 | #ifdef SEEK_HOLE |
| 10897 | if (ins(d, "SEEK_HOLE", (long)SEEK_HOLE)) return -1; |
| 10898 | #endif |
| 10899 | #ifdef SEEK_DATA |
| 10900 | if (ins(d, "SEEK_DATA", (long)SEEK_DATA)) return -1; |
| 10901 | #endif |
| 10902 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10903 | /* MS Windows */ |
| 10904 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10905 | /* Don't inherit in child processes. */ |
| 10906 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10907 | #endif |
| 10908 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10909 | /* Optimize for short life (keep in memory). */ |
| 10910 | /* MS forgot to define this one with a non-underscore form too. */ |
| 10911 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10912 | #endif |
| 10913 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10914 | /* Automatically delete when last handle is closed. */ |
| 10915 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10916 | #endif |
| 10917 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10918 | /* Optimize for random access. */ |
| 10919 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10920 | #endif |
| 10921 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10922 | /* Optimize for sequential access. */ |
| 10923 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10924 | #endif |
| 10925 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10926 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 10927 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10928 | /* Send a SIGIO signal whenever input or output |
| 10929 | becomes available on file descriptor */ |
| 10930 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 10931 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10932 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10933 | /* Direct disk access. */ |
| 10934 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10935 | #endif |
| 10936 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10937 | /* Must be a directory. */ |
| 10938 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10939 | #endif |
| 10940 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10941 | /* Do not follow links. */ |
| 10942 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10943 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10944 | #ifdef O_NOLINKS |
| 10945 | /* Fails if link count of the named file is greater than 1 */ |
| 10946 | if (ins(d, "O_NOLINKS", (long)O_NOLINKS)) return -1; |
| 10947 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 10948 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10949 | /* Do not update the access time. */ |
| 10950 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 10951 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10952 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10953 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10954 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10955 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10956 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10957 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10958 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10959 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10960 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10961 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10962 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10963 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10964 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10965 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10966 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10967 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10968 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10969 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10970 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10971 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10972 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10973 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10974 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10975 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10976 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10977 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10978 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10979 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10980 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10981 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10982 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10983 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10984 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10985 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10986 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10987 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10988 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10989 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10990 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10991 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10992 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10993 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10994 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10995 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10996 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10997 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10998 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10999 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11000 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11001 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11002 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11003 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11004 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11005 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11006 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11007 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11008 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11009 | #endif /* ST_RDONLY */ |
| 11010 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11011 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11012 | #endif /* ST_NOSUID */ |
| 11013 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11014 | /* FreeBSD sendfile() constants */ |
| 11015 | #ifdef SF_NODISKIO |
| 11016 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11017 | #endif |
| 11018 | #ifdef SF_MNOWAIT |
| 11019 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11020 | #endif |
| 11021 | #ifdef SF_SYNC |
| 11022 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11023 | #endif |
| 11024 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11025 | /* constants for posix_fadvise */ |
| 11026 | #ifdef POSIX_FADV_NORMAL |
| 11027 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11028 | #endif |
| 11029 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11030 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11031 | #endif |
| 11032 | #ifdef POSIX_FADV_RANDOM |
| 11033 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11034 | #endif |
| 11035 | #ifdef POSIX_FADV_NOREUSE |
| 11036 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11037 | #endif |
| 11038 | #ifdef POSIX_FADV_WILLNEED |
| 11039 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11040 | #endif |
| 11041 | #ifdef POSIX_FADV_DONTNEED |
| 11042 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11043 | #endif |
| 11044 | |
| 11045 | /* constants for waitid */ |
| 11046 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11047 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11048 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11049 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11050 | #endif |
| 11051 | #ifdef WEXITED |
| 11052 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11053 | #endif |
| 11054 | #ifdef WNOWAIT |
| 11055 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11056 | #endif |
| 11057 | #ifdef WSTOPPED |
| 11058 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11059 | #endif |
| 11060 | #ifdef CLD_EXITED |
| 11061 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11062 | #endif |
| 11063 | #ifdef CLD_DUMPED |
| 11064 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11065 | #endif |
| 11066 | #ifdef CLD_TRAPPED |
| 11067 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11068 | #endif |
| 11069 | #ifdef CLD_CONTINUED |
| 11070 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11071 | #endif |
| 11072 | |
| 11073 | /* constants for lockf */ |
| 11074 | #ifdef F_LOCK |
| 11075 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11076 | #endif |
| 11077 | #ifdef F_TLOCK |
| 11078 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11079 | #endif |
| 11080 | #ifdef F_ULOCK |
| 11081 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11082 | #endif |
| 11083 | #ifdef F_TEST |
| 11084 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11085 | #endif |
| 11086 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11087 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11088 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11089 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11090 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11091 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11092 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11093 | #endif |
| 11094 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11095 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11096 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11097 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11098 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11099 | #ifdef SCHED_SPORADIC |
| 11100 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11101 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11102 | #ifdef SCHED_BATCH |
| 11103 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11104 | #endif |
| 11105 | #ifdef SCHED_IDLE |
| 11106 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11107 | #endif |
| 11108 | #ifdef SCHED_RESET_ON_FORK |
| 11109 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11110 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11111 | #ifdef SCHED_SYS |
| 11112 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11113 | #endif |
| 11114 | #ifdef SCHED_IA |
| 11115 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11116 | #endif |
| 11117 | #ifdef SCHED_FSS |
| 11118 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11119 | #endif |
| 11120 | #ifdef SCHED_FX |
| 11121 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11122 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11123 | #endif |
| 11124 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11125 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11126 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11127 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11128 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11129 | #endif |
| 11130 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11131 | #ifdef RTLD_LAZY |
| 11132 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11133 | #endif |
| 11134 | #ifdef RTLD_NOW |
| 11135 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11136 | #endif |
| 11137 | #ifdef RTLD_GLOBAL |
| 11138 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11139 | #endif |
| 11140 | #ifdef RTLD_LOCAL |
| 11141 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11142 | #endif |
| 11143 | #ifdef RTLD_NODELETE |
| 11144 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11145 | #endif |
| 11146 | #ifdef RTLD_NOLOAD |
| 11147 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11148 | #endif |
| 11149 | #ifdef RTLD_DEEPBIND |
| 11150 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11151 | #endif |
| 11152 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11153 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11154 | } |
| 11155 | |
| 11156 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11157 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11158 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11159 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11160 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11161 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11162 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11163 | #define MODNAME "posix" |
| 11164 | #endif |
| 11165 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11166 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11167 | PyModuleDef_HEAD_INIT, |
| 11168 | MODNAME, |
| 11169 | posix__doc__, |
| 11170 | -1, |
| 11171 | posix_methods, |
| 11172 | NULL, |
| 11173 | NULL, |
| 11174 | NULL, |
| 11175 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11176 | }; |
| 11177 | |
| 11178 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11179 | static char *have_functions[] = { |
| 11180 | |
| 11181 | #ifdef HAVE_FACCESSAT |
| 11182 | "HAVE_FACCESSAT", |
| 11183 | #endif |
| 11184 | |
| 11185 | #ifdef HAVE_FCHDIR |
| 11186 | "HAVE_FCHDIR", |
| 11187 | #endif |
| 11188 | |
| 11189 | #ifdef HAVE_FCHMOD |
| 11190 | "HAVE_FCHMOD", |
| 11191 | #endif |
| 11192 | |
| 11193 | #ifdef HAVE_FCHMODAT |
| 11194 | "HAVE_FCHMODAT", |
| 11195 | #endif |
| 11196 | |
| 11197 | #ifdef HAVE_FCHOWN |
| 11198 | "HAVE_FCHOWN", |
| 11199 | #endif |
| 11200 | |
| 11201 | #ifdef HAVE_FEXECVE |
| 11202 | "HAVE_FEXECVE", |
| 11203 | #endif |
| 11204 | |
| 11205 | #ifdef HAVE_FDOPENDIR |
| 11206 | "HAVE_FDOPENDIR", |
| 11207 | #endif |
| 11208 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11209 | #ifdef HAVE_FPATHCONF |
| 11210 | "HAVE_FPATHCONF", |
| 11211 | #endif |
| 11212 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11213 | #ifdef HAVE_FSTATAT |
| 11214 | "HAVE_FSTATAT", |
| 11215 | #endif |
| 11216 | |
| 11217 | #ifdef HAVE_FSTATVFS |
| 11218 | "HAVE_FSTATVFS", |
| 11219 | #endif |
| 11220 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11221 | #ifdef HAVE_FTRUNCATE |
| 11222 | "HAVE_FTRUNCATE", |
| 11223 | #endif |
| 11224 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11225 | #ifdef HAVE_FUTIMENS |
| 11226 | "HAVE_FUTIMENS", |
| 11227 | #endif |
| 11228 | |
| 11229 | #ifdef HAVE_FUTIMES |
| 11230 | "HAVE_FUTIMES", |
| 11231 | #endif |
| 11232 | |
| 11233 | #ifdef HAVE_FUTIMESAT |
| 11234 | "HAVE_FUTIMESAT", |
| 11235 | #endif |
| 11236 | |
| 11237 | #ifdef HAVE_LINKAT |
| 11238 | "HAVE_LINKAT", |
| 11239 | #endif |
| 11240 | |
| 11241 | #ifdef HAVE_LCHFLAGS |
| 11242 | "HAVE_LCHFLAGS", |
| 11243 | #endif |
| 11244 | |
| 11245 | #ifdef HAVE_LCHMOD |
| 11246 | "HAVE_LCHMOD", |
| 11247 | #endif |
| 11248 | |
| 11249 | #ifdef HAVE_LCHOWN |
| 11250 | "HAVE_LCHOWN", |
| 11251 | #endif |
| 11252 | |
| 11253 | #ifdef HAVE_LSTAT |
| 11254 | "HAVE_LSTAT", |
| 11255 | #endif |
| 11256 | |
| 11257 | #ifdef HAVE_LUTIMES |
| 11258 | "HAVE_LUTIMES", |
| 11259 | #endif |
| 11260 | |
| 11261 | #ifdef HAVE_MKDIRAT |
| 11262 | "HAVE_MKDIRAT", |
| 11263 | #endif |
| 11264 | |
| 11265 | #ifdef HAVE_MKFIFOAT |
| 11266 | "HAVE_MKFIFOAT", |
| 11267 | #endif |
| 11268 | |
| 11269 | #ifdef HAVE_MKNODAT |
| 11270 | "HAVE_MKNODAT", |
| 11271 | #endif |
| 11272 | |
| 11273 | #ifdef HAVE_OPENAT |
| 11274 | "HAVE_OPENAT", |
| 11275 | #endif |
| 11276 | |
| 11277 | #ifdef HAVE_READLINKAT |
| 11278 | "HAVE_READLINKAT", |
| 11279 | #endif |
| 11280 | |
| 11281 | #ifdef HAVE_RENAMEAT |
| 11282 | "HAVE_RENAMEAT", |
| 11283 | #endif |
| 11284 | |
| 11285 | #ifdef HAVE_SYMLINKAT |
| 11286 | "HAVE_SYMLINKAT", |
| 11287 | #endif |
| 11288 | |
| 11289 | #ifdef HAVE_UNLINKAT |
| 11290 | "HAVE_UNLINKAT", |
| 11291 | #endif |
| 11292 | |
| 11293 | #ifdef HAVE_UTIMENSAT |
| 11294 | "HAVE_UTIMENSAT", |
| 11295 | #endif |
| 11296 | |
| 11297 | #ifdef MS_WINDOWS |
| 11298 | "MS_WINDOWS", |
| 11299 | #endif |
| 11300 | |
| 11301 | NULL |
| 11302 | }; |
| 11303 | |
| 11304 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11305 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11306 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11307 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11308 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11309 | PyObject *list; |
| 11310 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11311 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11312 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11313 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11314 | #endif |
| 11315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11316 | m = PyModule_Create(&posixmodule); |
| 11317 | if (m == NULL) |
| 11318 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11319 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | /* Initialize environ dictionary */ |
| 11321 | v = convertenviron(); |
| 11322 | Py_XINCREF(v); |
| 11323 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11324 | return NULL; |
| 11325 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11326 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11327 | if (all_ins(m)) |
| 11328 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11329 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11330 | if (setup_confname_tables(m)) |
| 11331 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11332 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11333 | Py_INCREF(PyExc_OSError); |
| 11334 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11335 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11336 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11337 | if (posix_putenv_garbage == NULL) |
| 11338 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11339 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11340 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11341 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11342 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11343 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11344 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11345 | #endif |
| 11346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11347 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11348 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11349 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11350 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11351 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11352 | structseq_new = StatResultType.tp_new; |
| 11353 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11354 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11355 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11356 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11357 | #ifdef NEED_TICKS_PER_SECOND |
| 11358 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11359 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11360 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11361 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11362 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11363 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11364 | # endif |
| 11365 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11366 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11367 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11368 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11369 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11370 | SchedParamType.tp_new = sched_param_new; |
| 11371 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11372 | |
| 11373 | /* initialize TerminalSize_info */ |
| 11374 | PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11375 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11376 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11377 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11378 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11379 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11380 | Py_INCREF((PyObject*) &StatResultType); |
| 11381 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11382 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11383 | PyModule_AddObject(m, "statvfs_result", |
| 11384 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11385 | |
| 11386 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11387 | Py_INCREF(&SchedParamType); |
| 11388 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11389 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11390 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 11391 | times_result_desc.name = MODNAME ".times_result"; |
| 11392 | PyStructSequence_InitType(&TimesResultType, ×_result_desc); |
| 11393 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 11394 | |
| 11395 | uname_result_desc.name = MODNAME ".uname_result"; |
| 11396 | PyStructSequence_InitType(&UnameResultType, &uname_result_desc); |
| 11397 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 11398 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11399 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11400 | /* |
| 11401 | * Step 2 of weak-linking support on Mac OS X. |
| 11402 | * |
| 11403 | * The code below removes functions that are not available on the |
| 11404 | * currently active platform. |
| 11405 | * |
| 11406 | * 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] | 11407 | * 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] | 11408 | * OSX 10.4. |
| 11409 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11410 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11411 | if (fstatvfs == NULL) { |
| 11412 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11413 | return NULL; |
| 11414 | } |
| 11415 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11416 | #endif /* HAVE_FSTATVFS */ |
| 11417 | |
| 11418 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11419 | if (statvfs == NULL) { |
| 11420 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11421 | return NULL; |
| 11422 | } |
| 11423 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11424 | #endif /* HAVE_STATVFS */ |
| 11425 | |
| 11426 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11427 | if (lchown == NULL) { |
| 11428 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11429 | return NULL; |
| 11430 | } |
| 11431 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11432 | #endif /* HAVE_LCHOWN */ |
| 11433 | |
| 11434 | |
| 11435 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11436 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 11437 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11438 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 11439 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 11440 | billion = PyLong_FromLong(1000000000); |
| 11441 | if (!billion) |
| 11442 | return NULL; |
| 11443 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11444 | /* suppress "function not used" warnings */ |
| 11445 | { |
| 11446 | int ignored; |
| 11447 | fd_specified("", -1); |
| 11448 | follow_symlinks_specified("", 1); |
| 11449 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 11450 | dir_fd_converter(Py_None, &ignored); |
| 11451 | dir_fd_unavailable(Py_None, &ignored); |
| 11452 | } |
| 11453 | |
| 11454 | /* |
| 11455 | * provide list of locally available functions |
| 11456 | * so os.py can populate support_* lists |
| 11457 | */ |
| 11458 | list = PyList_New(0); |
| 11459 | if (!list) |
| 11460 | return NULL; |
| 11461 | for (trace = have_functions; *trace; trace++) { |
| 11462 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 11463 | if (!unicode) |
| 11464 | return NULL; |
| 11465 | if (PyList_Append(list, unicode)) |
| 11466 | return NULL; |
| 11467 | Py_DECREF(unicode); |
| 11468 | } |
| 11469 | PyModule_AddObject(m, "_have_functions", list); |
| 11470 | |
| 11471 | initialized = 1; |
| 11472 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11474 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11475 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11476 | |
| 11477 | #ifdef __cplusplus |
| 11478 | } |
| 11479 | #endif |