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" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 26 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 27 | #if defined(__VMS) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 28 | # 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] | 29 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 30 | #endif /* defined(__VMS) */ |
| 31 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 36 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 37 | "This module provides access to operating system functionality that is\n\ |
| 38 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 39 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 40 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 42 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 43 | #ifdef HAVE_SYS_UIO_H |
| 44 | #include <sys/uio.h> |
| 45 | #endif |
| 46 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 47 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 48 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | #endif /* HAVE_SYS_TYPES_H */ |
| 50 | |
| 51 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 52 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 53 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 55 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 56 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 57 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 58 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 59 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 60 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 61 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | #ifdef HAVE_FCNTL_H |
| 64 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 65 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 67 | #ifdef HAVE_GRP_H |
| 68 | #include <grp.h> |
| 69 | #endif |
| 70 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYSEXITS_H |
| 72 | #include <sysexits.h> |
| 73 | #endif /* HAVE_SYSEXITS_H */ |
| 74 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SYS_LOADAVG_H |
| 76 | #include <sys/loadavg.h> |
| 77 | #endif |
| 78 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 79 | #ifdef HAVE_LANGINFO_H |
| 80 | #include <langinfo.h> |
| 81 | #endif |
| 82 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 83 | #ifdef HAVE_SYS_SENDFILE_H |
| 84 | #include <sys/sendfile.h> |
| 85 | #endif |
| 86 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 87 | #ifdef HAVE_SCHED_H |
| 88 | #include <sched.h> |
| 89 | #endif |
| 90 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 91 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 92 | #undef HAVE_SCHED_SETAFFINITY |
| 93 | #endif |
| 94 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 95 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) |
| 96 | #define USE_XATTRS |
| 97 | #endif |
| 98 | |
| 99 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 100 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 101 | #endif |
| 102 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 103 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 104 | #ifdef HAVE_SYS_SOCKET_H |
| 105 | #include <sys/socket.h> |
| 106 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 109 | #ifdef HAVE_DLFCN_H |
| 110 | #include <dlfcn.h> |
| 111 | #endif |
| 112 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 113 | #if defined(MS_WINDOWS) |
| 114 | # define TERMSIZE_USE_CONIO |
| 115 | #elif defined(HAVE_SYS_IOCTL_H) |
| 116 | # include <sys/ioctl.h> |
| 117 | # if defined(HAVE_TERMIOS_H) |
| 118 | # include <termios.h> |
| 119 | # endif |
| 120 | # if defined(TIOCGWINSZ) |
| 121 | # define TERMSIZE_USE_IOCTL |
| 122 | # endif |
| 123 | #endif /* MS_WINDOWS */ |
| 124 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 126 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 127 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | #define HAVE_GETCWD 1 |
| 129 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 130 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 131 | #include <process.h> |
| 132 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 133 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #define HAVE_EXECV 1 |
| 135 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 136 | #define HAVE_OPENDIR 1 |
| 137 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 138 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 139 | #define HAVE_WAIT 1 |
| 140 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 141 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 142 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 143 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 144 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 145 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #define HAVE_EXECV 1 |
| 147 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 148 | #define HAVE_SYSTEM 1 |
| 149 | #define HAVE_CWAIT 1 |
| 150 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 151 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 152 | #else |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 153 | #if defined(__VMS) |
| 154 | /* Everything needed is defined in vms/pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 155 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 156 | /* Unix functions that the configure script doesn't check for */ |
| 157 | #define HAVE_EXECV 1 |
| 158 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 159 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 160 | #define HAVE_FORK1 1 |
| 161 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 162 | #define HAVE_GETCWD 1 |
| 163 | #define HAVE_GETEGID 1 |
| 164 | #define HAVE_GETEUID 1 |
| 165 | #define HAVE_GETGID 1 |
| 166 | #define HAVE_GETPPID 1 |
| 167 | #define HAVE_GETUID 1 |
| 168 | #define HAVE_KILL 1 |
| 169 | #define HAVE_OPENDIR 1 |
| 170 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 171 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 172 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 173 | #define HAVE_TTYNAME 1 |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 174 | #endif /* __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif /* _MSC_VER */ |
| 176 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 177 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 178 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 179 | |
| 180 | |
| 181 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 182 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 183 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 184 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 185 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 186 | (default) */ |
| 187 | extern char *ctermid_r(char *); |
| 188 | #endif |
| 189 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 190 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 191 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 192 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 193 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 194 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 196 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 197 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 198 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 199 | #endif |
| 200 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 201 | extern int chdir(char *); |
| 202 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 203 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 204 | extern int chdir(const char *); |
| 205 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 206 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 207 | #ifdef __BORLANDC__ |
| 208 | extern int chmod(const char *, int); |
| 209 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 210 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 211 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 212 | /*#ifdef HAVE_FCHMOD |
| 213 | extern int fchmod(int, mode_t); |
| 214 | #endif*/ |
| 215 | /*#ifdef HAVE_LCHMOD |
| 216 | extern int lchmod(const char *, mode_t); |
| 217 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 218 | extern int chown(const char *, uid_t, gid_t); |
| 219 | extern char *getcwd(char *, int); |
| 220 | extern char *strerror(int); |
| 221 | extern int link(const char *, const char *); |
| 222 | extern int rename(const char *, const char *); |
| 223 | extern int stat(const char *, struct stat *); |
| 224 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 226 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 227 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 229 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 230 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 233 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #ifdef HAVE_UTIME_H |
| 236 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 237 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 239 | #ifdef HAVE_SYS_UTIME_H |
| 240 | #include <sys/utime.h> |
| 241 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 242 | #endif /* HAVE_SYS_UTIME_H */ |
| 243 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | #ifdef HAVE_SYS_TIMES_H |
| 245 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 246 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | |
| 248 | #ifdef HAVE_SYS_PARAM_H |
| 249 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 250 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | |
| 252 | #ifdef HAVE_SYS_UTSNAME_H |
| 253 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 254 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 256 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 258 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 259 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 260 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 261 | #include <direct.h> |
| 262 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 263 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 265 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 266 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 267 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 269 | #endif |
| 270 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 272 | #endif |
| 273 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 275 | #endif |
| 276 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 277 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 278 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 279 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 281 | #endif |
| 282 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 283 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 284 | #endif |
| 285 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 287 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 288 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 289 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 290 | #endif |
| 291 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 292 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 293 | #endif |
| 294 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 295 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 296 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 297 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 298 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 299 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 300 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 301 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 302 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 303 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 304 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 305 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 306 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 307 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 308 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 309 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 310 | #define MAXPATHLEN PATH_MAX |
| 311 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 312 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 313 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 314 | #endif /* MAXPATHLEN */ |
| 315 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 316 | #ifdef UNION_WAIT |
| 317 | /* Emulate some macros on systems that have a union instead of macros */ |
| 318 | |
| 319 | #ifndef WIFEXITED |
| 320 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 321 | #endif |
| 322 | |
| 323 | #ifndef WEXITSTATUS |
| 324 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 325 | #endif |
| 326 | |
| 327 | #ifndef WTERMSIG |
| 328 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 329 | #endif |
| 330 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 331 | #define WAIT_TYPE union wait |
| 332 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 333 | |
| 334 | #else /* !UNION_WAIT */ |
| 335 | #define WAIT_TYPE int |
| 336 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 337 | #endif /* UNION_WAIT */ |
| 338 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 339 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 340 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 341 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 342 | #define USE_CTERMID_R |
| 343 | #endif |
| 344 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 345 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 346 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 347 | #undef FSTAT |
| 348 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 349 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 350 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 351 | # define LSTAT win32_lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 352 | # define FSTAT win32_fstat |
| 353 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 354 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 355 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 356 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 357 | # define FSTAT fstat |
| 358 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 359 | #endif |
| 360 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 361 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 362 | #include <sys/mkdev.h> |
| 363 | #else |
| 364 | #if defined(MAJOR_IN_SYSMACROS) |
| 365 | #include <sys/sysmacros.h> |
| 366 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 367 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 368 | #include <sys/mkdev.h> |
| 369 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 370 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 371 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 372 | |
| 373 | #ifdef MS_WINDOWS |
| 374 | static int |
| 375 | win32_warn_bytes_api() |
| 376 | { |
| 377 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 378 | "The Windows bytes API has been deprecated, " |
| 379 | "use Unicode filenames instead", |
| 380 | 1); |
| 381 | } |
| 382 | #endif |
| 383 | |
| 384 | |
| 385 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 386 | /* |
| 387 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 388 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 389 | * which doesn't play nicely with all the initializer lines in this file that |
| 390 | * look like this: |
| 391 | * int dir_fd = DEFAULT_DIR_FD; |
| 392 | */ |
| 393 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 394 | #else |
| 395 | #define DEFAULT_DIR_FD (-100) |
| 396 | #endif |
| 397 | |
| 398 | static int |
| 399 | _fd_converter(PyObject *o, int *p, int default_value) { |
| 400 | long long_value; |
| 401 | if (o == Py_None) { |
| 402 | *p = default_value; |
| 403 | return 1; |
| 404 | } |
| 405 | if (PyFloat_Check(o)) { |
| 406 | PyErr_SetString(PyExc_TypeError, |
| 407 | "integer argument expected, got float" ); |
| 408 | return 0; |
| 409 | } |
| 410 | long_value = PyLong_AsLong(o); |
| 411 | if (long_value == -1 && PyErr_Occurred()) |
| 412 | return 0; |
| 413 | if (long_value > INT_MAX) { |
| 414 | PyErr_SetString(PyExc_OverflowError, |
| 415 | "signed integer is greater than maximum"); |
| 416 | return 0; |
| 417 | } |
| 418 | if (long_value < INT_MIN) { |
| 419 | PyErr_SetString(PyExc_OverflowError, |
| 420 | "signed integer is less than minimum"); |
| 421 | return 0; |
| 422 | } |
| 423 | *p = (int)long_value; |
| 424 | return 1; |
| 425 | } |
| 426 | |
| 427 | static int |
| 428 | dir_fd_converter(PyObject *o, void *p) { |
| 429 | return _fd_converter(o, (int *)p, DEFAULT_DIR_FD); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | /* |
| 435 | * A PyArg_ParseTuple "converter" function |
| 436 | * that handles filesystem paths in the manner |
| 437 | * preferred by the os module. |
| 438 | * |
| 439 | * path_converter accepts (Unicode) strings and their |
| 440 | * subclasses, and bytes and their subclasses. What |
| 441 | * it does with the argument depends on the platform: |
| 442 | * |
| 443 | * * On Windows, if we get a (Unicode) string we |
| 444 | * extract the wchar_t * and return it; if we get |
| 445 | * bytes we extract the char * and return that. |
| 446 | * |
| 447 | * * On all other platforms, strings are encoded |
| 448 | * to bytes using PyUnicode_FSConverter, then we |
| 449 | * extract the char * from the bytes object and |
| 450 | * return that. |
| 451 | * |
| 452 | * path_converter also optionally accepts signed |
| 453 | * integers (representing open file descriptors) instead |
| 454 | * of path strings. |
| 455 | * |
| 456 | * Input fields: |
| 457 | * path.nullable |
| 458 | * If nonzero, the path is permitted to be None. |
| 459 | * path.allow_fd |
| 460 | * If nonzero, the path is permitted to be a file handle |
| 461 | * (a signed int) instead of a string. |
| 462 | * path.function_name |
| 463 | * If non-NULL, path_converter will use that as the name |
| 464 | * of the function in error messages. |
| 465 | * (If path.argument_name is NULL it omits the function name.) |
| 466 | * path.argument_name |
| 467 | * If non-NULL, path_converter will use that as the name |
| 468 | * of the parameter in error messages. |
| 469 | * (If path.argument_name is NULL it uses "path".) |
| 470 | * |
| 471 | * Output fields: |
| 472 | * path.wide |
| 473 | * Points to the path if it was expressed as Unicode |
| 474 | * and was not encoded. (Only used on Windows.) |
| 475 | * path.narrow |
| 476 | * Points to the path if it was expressed as bytes, |
| 477 | * or it was Unicode and was encoded to bytes. |
| 478 | * path.fd |
| 479 | * Contains a file descriptor if path.accept_fd was true |
| 480 | * and the caller provided a signed integer instead of any |
| 481 | * sort of string. |
| 482 | * |
| 483 | * WARNING: if your "path" parameter is optional, and is |
| 484 | * unspecified, path_converter will never get called. |
| 485 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 486 | * yourself! |
| 487 | * path.length |
| 488 | * The length of the path in characters, if specified as |
| 489 | * a string. |
| 490 | * path.object |
| 491 | * The original object passed in. |
| 492 | * path.cleanup |
| 493 | * For internal use only. May point to a temporary object. |
| 494 | * (Pay no attention to the man behind the curtain.) |
| 495 | * |
| 496 | * At most one of path.wide or path.narrow will be non-NULL. |
| 497 | * If path was None and path.nullable was set, |
| 498 | * or if path was an integer and path.allow_fd was set, |
| 499 | * both path.wide and path.narrow will be NULL |
| 500 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 501 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 502 | * path_converter takes care to not write to the path_t |
| 503 | * unless it's successful. However it must reset the |
| 504 | * "cleanup" field each time it's called. |
| 505 | * |
| 506 | * Use as follows: |
| 507 | * path_t path; |
| 508 | * memset(&path, 0, sizeof(path)); |
| 509 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 510 | * // ... use values from path ... |
| 511 | * path_cleanup(&path); |
| 512 | * |
| 513 | * (Note that if PyArg_Parse fails you don't need to call |
| 514 | * path_cleanup(). However it is safe to do so.) |
| 515 | */ |
| 516 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 517 | const char *function_name; |
| 518 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 519 | int nullable; |
| 520 | int allow_fd; |
| 521 | wchar_t *wide; |
| 522 | char *narrow; |
| 523 | int fd; |
| 524 | Py_ssize_t length; |
| 525 | PyObject *object; |
| 526 | PyObject *cleanup; |
| 527 | } path_t; |
| 528 | |
| 529 | static void |
| 530 | path_cleanup(path_t *path) { |
| 531 | if (path->cleanup) { |
| 532 | Py_DECREF(path->cleanup); |
| 533 | path->cleanup = NULL; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | static int |
| 538 | path_converter(PyObject *o, void *p) { |
| 539 | path_t *path = (path_t *)p; |
| 540 | PyObject *unicode, *bytes; |
| 541 | Py_ssize_t length; |
| 542 | char *narrow; |
| 543 | |
| 544 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 545 | PyErr_Format(exc, "%s%s" fmt, \ |
| 546 | path->function_name ? path->function_name : "", \ |
| 547 | path->function_name ? ": " : "", \ |
| 548 | path->argument_name ? path->argument_name : "path") |
| 549 | |
| 550 | /* Py_CLEANUP_SUPPORTED support */ |
| 551 | if (o == NULL) { |
| 552 | path_cleanup(path); |
| 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | /* ensure it's always safe to call path_cleanup() */ |
| 557 | path->cleanup = NULL; |
| 558 | |
| 559 | if (o == Py_None) { |
| 560 | if (!path->nullable) { |
| 561 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 562 | "can't specify None for %s argument"); |
| 563 | return 0; |
| 564 | } |
| 565 | path->wide = NULL; |
| 566 | path->narrow = NULL; |
| 567 | path->length = 0; |
| 568 | path->object = o; |
| 569 | path->fd = -1; |
| 570 | return 1; |
| 571 | } |
| 572 | |
| 573 | unicode = PyUnicode_FromObject(o); |
| 574 | if (unicode) { |
| 575 | #ifdef MS_WINDOWS |
| 576 | wchar_t *wide; |
| 577 | length = PyUnicode_GET_SIZE(unicode); |
| 578 | if (length > 32767) { |
| 579 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 580 | Py_DECREF(unicode); |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | wide = PyUnicode_AsUnicode(unicode); |
| 585 | if (!wide) { |
| 586 | Py_DECREF(unicode); |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | path->wide = wide; |
| 591 | path->narrow = NULL; |
| 592 | path->length = length; |
| 593 | path->object = o; |
| 594 | path->fd = -1; |
| 595 | path->cleanup = unicode; |
| 596 | return Py_CLEANUP_SUPPORTED; |
| 597 | #else |
| 598 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 599 | Py_DECREF(unicode); |
| 600 | if (!converted) |
| 601 | bytes = NULL; |
| 602 | #endif |
| 603 | } |
| 604 | else { |
| 605 | PyErr_Clear(); |
| 606 | bytes = PyBytes_FromObject(o); |
| 607 | if (!bytes) { |
| 608 | PyErr_Clear(); |
| 609 | if (path->allow_fd) { |
| 610 | int fd; |
| 611 | /* |
| 612 | * note: _fd_converter always permits None. |
| 613 | * but we've already done our None check. |
| 614 | * so o cannot be None at this point. |
| 615 | */ |
| 616 | int result = _fd_converter(o, &fd, -1); |
| 617 | if (result) { |
| 618 | path->wide = NULL; |
| 619 | path->narrow = NULL; |
| 620 | path->length = 0; |
| 621 | path->object = o; |
| 622 | path->fd = fd; |
| 623 | return result; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | if (!bytes) { |
| 630 | if (!PyErr_Occurred()) |
| 631 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | #ifdef MS_WINDOWS |
| 636 | if (win32_warn_bytes_api()) { |
| 637 | Py_DECREF(bytes); |
| 638 | return 0; |
| 639 | } |
| 640 | #endif |
| 641 | |
| 642 | length = PyBytes_GET_SIZE(bytes); |
| 643 | #ifdef MS_WINDOWS |
| 644 | if (length > MAX_PATH) { |
| 645 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 646 | Py_DECREF(bytes); |
| 647 | return 0; |
| 648 | } |
| 649 | #endif |
| 650 | |
| 651 | narrow = PyBytes_AS_STRING(bytes); |
| 652 | if (length != strlen(narrow)) { |
| 653 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); |
| 654 | Py_DECREF(bytes); |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | path->wide = NULL; |
| 659 | path->narrow = narrow; |
| 660 | path->length = length; |
| 661 | path->object = o; |
| 662 | path->fd = -1; |
| 663 | path->cleanup = bytes; |
| 664 | return Py_CLEANUP_SUPPORTED; |
| 665 | } |
| 666 | |
| 667 | static void |
| 668 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 669 | PyErr_Format(PyExc_NotImplementedError, |
| 670 | "%s%s%s unavailable on this platform", |
| 671 | (function_name != NULL) ? function_name : "", |
| 672 | (function_name != NULL) ? ": ": "", |
| 673 | argument_name); |
| 674 | } |
| 675 | |
| 676 | static int |
| 677 | dir_fd_unavailable(PyObject *o, void *p) { |
| 678 | int *dir_fd = (int *)p; |
| 679 | int return_value = _fd_converter(o, dir_fd, DEFAULT_DIR_FD); |
| 680 | if (!return_value) |
| 681 | return 0; |
| 682 | if (*dir_fd == DEFAULT_DIR_FD) |
| 683 | return 1; |
| 684 | argument_unavailable_error(NULL, "dir_fd"); |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | static int |
| 689 | fd_specified(char *function_name, int fd) { |
| 690 | if (fd == -1) |
| 691 | return 0; |
| 692 | |
| 693 | argument_unavailable_error(function_name, "fd"); |
| 694 | return 1; |
| 695 | } |
| 696 | |
| 697 | static int |
| 698 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 699 | if (follow_symlinks) |
| 700 | return 0; |
| 701 | |
| 702 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 703 | return 1; |
| 704 | } |
| 705 | |
| 706 | static int |
| 707 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 708 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 709 | PyErr_Format(PyExc_ValueError, |
| 710 | "%s: can't specify dir_fd without matching path", |
| 711 | function_name); |
| 712 | return 1; |
| 713 | } |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | static int |
| 718 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 719 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 720 | PyErr_Format(PyExc_ValueError, |
| 721 | "%s: can't specify both dir_fd and fd", |
| 722 | function_name); |
| 723 | return 1; |
| 724 | } |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int |
| 729 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 730 | int follow_symlinks) { |
| 731 | if ((fd > 0) && (!follow_symlinks)) { |
| 732 | PyErr_Format(PyExc_ValueError, |
| 733 | "%s: cannot use fd and follow_symlinks together", |
| 734 | function_name); |
| 735 | return 1; |
| 736 | } |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static int |
| 741 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 742 | int follow_symlinks) { |
| 743 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 744 | PyErr_Format(PyExc_ValueError, |
| 745 | "%s: cannot use dir_fd and follow_symlinks together", |
| 746 | function_name); |
| 747 | return 1; |
| 748 | } |
| 749 | return 0; |
| 750 | } |
| 751 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 752 | /* A helper used by a number of POSIX-only functions */ |
| 753 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 754 | static int |
| 755 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 756 | { |
| 757 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 758 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 759 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 760 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 761 | #endif |
| 762 | if (PyErr_Occurred()) |
| 763 | return 0; |
| 764 | return 1; |
| 765 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 766 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 767 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 768 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 769 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 770 | * valid and throw an assertion if it isn't. |
| 771 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 772 | * an assertion can be useful, but it does contradict the POSIX standard |
| 773 | * which for write(2) states: |
| 774 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 775 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 776 | * writing." |
| 777 | * Furthermore, python allows the user to enter any old integer |
| 778 | * as a fd and should merely raise a python exception on error. |
| 779 | * The Microsoft CRT doesn't provide an official way to check for the |
| 780 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 781 | * 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] | 782 | * internal structures involved. |
| 783 | * The structures below must be updated for each version of visual studio |
| 784 | * according to the file internal.h in the CRT source, until MS comes |
| 785 | * up with a less hacky way to do this. |
| 786 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 787 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 788 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 789 | /* The actual size of the structure is determined at runtime. |
| 790 | * Only the first items must be present. |
| 791 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 792 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 793 | intptr_t osfhnd; |
| 794 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 795 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 796 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 797 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 798 | #define IOINFO_L2E 5 |
| 799 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 800 | #define IOINFO_ARRAYS 64 |
| 801 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 802 | #define FOPEN 0x01 |
| 803 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 804 | |
| 805 | /* This function emulates what the windows CRT does to validate file handles */ |
| 806 | int |
| 807 | _PyVerify_fd(int fd) |
| 808 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 809 | const int i1 = fd >> IOINFO_L2E; |
| 810 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 811 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 812 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 813 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 814 | /* Determine the actual size of the ioinfo structure, |
| 815 | * as used by the CRT loaded in memory |
| 816 | */ |
| 817 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 818 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 819 | } |
| 820 | if (sizeof_ioinfo == 0) { |
| 821 | /* This should not happen... */ |
| 822 | goto fail; |
| 823 | } |
| 824 | |
| 825 | /* See that it isn't a special CLEAR fileno */ |
| 826 | if (fd != _NO_CONSOLE_FILENO) { |
| 827 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 828 | * we check pointer validity and other info |
| 829 | */ |
| 830 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 831 | /* finally, check that the file is open */ |
| 832 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 833 | if (info->osfile & FOPEN) { |
| 834 | return 1; |
| 835 | } |
| 836 | } |
| 837 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 838 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 839 | errno = EBADF; |
| 840 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 844 | static int |
| 845 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 846 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 847 | if (!_PyVerify_fd(fd1)) |
| 848 | return 0; |
| 849 | if (fd2 == _NO_CONSOLE_FILENO) |
| 850 | return 0; |
| 851 | if ((unsigned)fd2 < _NHANDLE_) |
| 852 | return 1; |
| 853 | else |
| 854 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 855 | } |
| 856 | #else |
| 857 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 858 | #define _PyVerify_fd_dup2(A, B) (1) |
| 859 | #endif |
| 860 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 861 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 862 | /* The following structure was copied from |
| 863 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 864 | include doesn't seem to be present in the Windows SDK (at least as included |
| 865 | with Visual Studio Express). */ |
| 866 | typedef struct _REPARSE_DATA_BUFFER { |
| 867 | ULONG ReparseTag; |
| 868 | USHORT ReparseDataLength; |
| 869 | USHORT Reserved; |
| 870 | union { |
| 871 | struct { |
| 872 | USHORT SubstituteNameOffset; |
| 873 | USHORT SubstituteNameLength; |
| 874 | USHORT PrintNameOffset; |
| 875 | USHORT PrintNameLength; |
| 876 | ULONG Flags; |
| 877 | WCHAR PathBuffer[1]; |
| 878 | } SymbolicLinkReparseBuffer; |
| 879 | |
| 880 | struct { |
| 881 | USHORT SubstituteNameOffset; |
| 882 | USHORT SubstituteNameLength; |
| 883 | USHORT PrintNameOffset; |
| 884 | USHORT PrintNameLength; |
| 885 | WCHAR PathBuffer[1]; |
| 886 | } MountPointReparseBuffer; |
| 887 | |
| 888 | struct { |
| 889 | UCHAR DataBuffer[1]; |
| 890 | } GenericReparseBuffer; |
| 891 | }; |
| 892 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 893 | |
| 894 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 895 | GenericReparseBuffer) |
| 896 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 897 | |
| 898 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 899 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 900 | { |
| 901 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 902 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 903 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 904 | |
| 905 | if (0 == DeviceIoControl( |
| 906 | reparse_point_handle, |
| 907 | FSCTL_GET_REPARSE_POINT, |
| 908 | NULL, 0, /* in buffer */ |
| 909 | target_buffer, sizeof(target_buffer), |
| 910 | &n_bytes_returned, |
| 911 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 912 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 913 | |
| 914 | if (reparse_tag) |
| 915 | *reparse_tag = rdb->ReparseTag; |
| 916 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 917 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 918 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 919 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 920 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 921 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 922 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 923 | #ifdef WITH_NEXT_FRAMEWORK |
| 924 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 925 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 926 | */ |
| 927 | #include <crt_externs.h> |
| 928 | static char **environ; |
| 929 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 930 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 931 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 933 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 934 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 935 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 936 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 937 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 938 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 939 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 940 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 941 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 942 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 943 | d = PyDict_New(); |
| 944 | if (d == NULL) |
| 945 | return NULL; |
| 946 | #ifdef WITH_NEXT_FRAMEWORK |
| 947 | if (environ == NULL) |
| 948 | environ = *_NSGetEnviron(); |
| 949 | #endif |
| 950 | #ifdef MS_WINDOWS |
| 951 | /* _wenviron must be initialized in this way if the program is started |
| 952 | through main() instead of wmain(). */ |
| 953 | _wgetenv(L""); |
| 954 | if (_wenviron == NULL) |
| 955 | return d; |
| 956 | /* This part ignores errors */ |
| 957 | for (e = _wenviron; *e != NULL; e++) { |
| 958 | PyObject *k; |
| 959 | PyObject *v; |
| 960 | wchar_t *p = wcschr(*e, L'='); |
| 961 | if (p == NULL) |
| 962 | continue; |
| 963 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 964 | if (k == NULL) { |
| 965 | PyErr_Clear(); |
| 966 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 967 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 968 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 969 | if (v == NULL) { |
| 970 | PyErr_Clear(); |
| 971 | Py_DECREF(k); |
| 972 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 973 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 974 | if (PyDict_GetItem(d, k) == NULL) { |
| 975 | if (PyDict_SetItem(d, k, v) != 0) |
| 976 | PyErr_Clear(); |
| 977 | } |
| 978 | Py_DECREF(k); |
| 979 | Py_DECREF(v); |
| 980 | } |
| 981 | #else |
| 982 | if (environ == NULL) |
| 983 | return d; |
| 984 | /* This part ignores errors */ |
| 985 | for (e = environ; *e != NULL; e++) { |
| 986 | PyObject *k; |
| 987 | PyObject *v; |
| 988 | char *p = strchr(*e, '='); |
| 989 | if (p == NULL) |
| 990 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 991 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 992 | if (k == NULL) { |
| 993 | PyErr_Clear(); |
| 994 | continue; |
| 995 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 996 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 997 | if (v == NULL) { |
| 998 | PyErr_Clear(); |
| 999 | Py_DECREF(k); |
| 1000 | continue; |
| 1001 | } |
| 1002 | if (PyDict_GetItem(d, k) == NULL) { |
| 1003 | if (PyDict_SetItem(d, k, v) != 0) |
| 1004 | PyErr_Clear(); |
| 1005 | } |
| 1006 | Py_DECREF(k); |
| 1007 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1008 | } |
| 1009 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1010 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1013 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1014 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1015 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1016 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1017 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1018 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1019 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1020 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1021 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1022 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1023 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1024 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1025 | /* XXX We should pass the function name along in the future. |
| 1026 | (winreg.c also wants to pass the function name.) |
| 1027 | This would however require an additional param to the |
| 1028 | Windows error object, which is non-trivial. |
| 1029 | */ |
| 1030 | errno = GetLastError(); |
| 1031 | if (filename) |
| 1032 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1033 | else |
| 1034 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1035 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1036 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1037 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1038 | win32_error_object(char* function, PyObject* filename) |
| 1039 | { |
| 1040 | /* XXX - see win32_error for comments on 'function' */ |
| 1041 | errno = GetLastError(); |
| 1042 | if (filename) |
| 1043 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1044 | PyExc_WindowsError, |
| 1045 | errno, |
| 1046 | filename); |
| 1047 | else |
| 1048 | return PyErr_SetFromWindowsErr(errno); |
| 1049 | } |
| 1050 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1051 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1052 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1053 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1054 | path_error(path_t *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1055 | { |
| 1056 | #ifdef MS_WINDOWS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1057 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 1058 | 0, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1059 | #else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1060 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1061 | #endif |
| 1062 | } |
| 1063 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1064 | /* POSIX generic methods */ |
| 1065 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1066 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1067 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1068 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1069 | int fd; |
| 1070 | int res; |
| 1071 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1072 | if (fd < 0) |
| 1073 | return NULL; |
| 1074 | if (!_PyVerify_fd(fd)) |
| 1075 | return posix_error(); |
| 1076 | Py_BEGIN_ALLOW_THREADS |
| 1077 | res = (*func)(fd); |
| 1078 | Py_END_ALLOW_THREADS |
| 1079 | if (res < 0) |
| 1080 | return posix_error(); |
| 1081 | Py_INCREF(Py_None); |
| 1082 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1083 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1084 | |
| 1085 | static PyObject * |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1086 | posix_1str(const char *func_name, PyObject *args, char *format, |
| 1087 | int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1088 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1089 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1090 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1091 | memset(&path, 0, sizeof(path)); |
| 1092 | path.function_name = func_name; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1093 | if (!PyArg_ParseTuple(args, format, |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1094 | path_converter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1095 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1096 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1097 | res = (*func)(path.narrow); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1098 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1099 | if (res < 0) { |
| 1100 | path_error(&path); |
| 1101 | path_cleanup(&path); |
| 1102 | return NULL; |
| 1103 | } |
| 1104 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1105 | Py_INCREF(Py_None); |
| 1106 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1109 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1110 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1111 | /* This is a reimplementation of the C library's chdir function, |
| 1112 | but one that produces Win32 errors instead of DOS error codes. |
| 1113 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1114 | it also needs to set "magic" environment variables indicating |
| 1115 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1116 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1117 | win32_chdir(LPCSTR path) |
| 1118 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1119 | char new_path[MAX_PATH+1]; |
| 1120 | int result; |
| 1121 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1122 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1123 | if(!SetCurrentDirectoryA(path)) |
| 1124 | return FALSE; |
| 1125 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 1126 | if (!result) |
| 1127 | return FALSE; |
| 1128 | /* In the ANSI API, there should not be any paths longer |
| 1129 | than MAX_PATH. */ |
| 1130 | assert(result <= MAX_PATH+1); |
| 1131 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1132 | strncmp(new_path, "//", 2) == 0) |
| 1133 | /* UNC path, nothing to do. */ |
| 1134 | return TRUE; |
| 1135 | env[1] = new_path[0]; |
| 1136 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /* The Unicode version differs from the ANSI version |
| 1140 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1141 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1142 | win32_wchdir(LPCWSTR path) |
| 1143 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1144 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 1145 | int result; |
| 1146 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1147 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1148 | if(!SetCurrentDirectoryW(path)) |
| 1149 | return FALSE; |
| 1150 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 1151 | if (!result) |
| 1152 | return FALSE; |
| 1153 | if (result > MAX_PATH+1) { |
| 1154 | new_path = malloc(result * sizeof(wchar_t)); |
| 1155 | if (!new_path) { |
| 1156 | SetLastError(ERROR_OUTOFMEMORY); |
| 1157 | return FALSE; |
| 1158 | } |
| 1159 | result = GetCurrentDirectoryW(result, new_path); |
| 1160 | if (!result) { |
| 1161 | free(new_path); |
| 1162 | return FALSE; |
| 1163 | } |
| 1164 | } |
| 1165 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1166 | wcsncmp(new_path, L"//", 2) == 0) |
| 1167 | /* UNC path, nothing to do. */ |
| 1168 | return TRUE; |
| 1169 | env[1] = new_path[0]; |
| 1170 | result = SetEnvironmentVariableW(env, new_path); |
| 1171 | if (new_path != _new_path) |
| 1172 | free(new_path); |
| 1173 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1174 | } |
| 1175 | #endif |
| 1176 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1177 | #ifdef MS_WINDOWS |
| 1178 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1179 | - time stamps are restricted to second resolution |
| 1180 | - file modification times suffer from forth-and-back conversions between |
| 1181 | UTC and local time |
| 1182 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1183 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1184 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1185 | |
| 1186 | struct win32_stat{ |
| 1187 | int st_dev; |
| 1188 | __int64 st_ino; |
| 1189 | unsigned short st_mode; |
| 1190 | int st_nlink; |
| 1191 | int st_uid; |
| 1192 | int st_gid; |
| 1193 | int st_rdev; |
| 1194 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1195 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1196 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1197 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1198 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1199 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1200 | int st_ctime_nsec; |
| 1201 | }; |
| 1202 | |
| 1203 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1204 | |
| 1205 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1206 | 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] | 1207 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1208 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1209 | /* Cannot simply cast and dereference in_ptr, |
| 1210 | since it might not be aligned properly */ |
| 1211 | __int64 in; |
| 1212 | memcpy(&in, in_ptr, sizeof(in)); |
| 1213 | *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] | 1214 | *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] | 1215 | } |
| 1216 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1217 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1218 | 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] | 1219 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1220 | /* XXX endianness */ |
| 1221 | __int64 out; |
| 1222 | out = time_in + secs_between_epochs; |
| 1223 | out = out * 10000000 + nsec_in / 100; |
| 1224 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1227 | /* Below, we *know* that ugo+r is 0444 */ |
| 1228 | #if _S_IREAD != 0400 |
| 1229 | #error Unsupported C library |
| 1230 | #endif |
| 1231 | static int |
| 1232 | attributes_to_mode(DWORD attr) |
| 1233 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1234 | int m = 0; |
| 1235 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1236 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1237 | else |
| 1238 | m |= _S_IFREG; |
| 1239 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1240 | m |= 0444; |
| 1241 | else |
| 1242 | m |= 0666; |
| 1243 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1247 | 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] | 1248 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1249 | memset(result, 0, sizeof(*result)); |
| 1250 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1251 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1252 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1253 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1254 | 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] | 1255 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1256 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1257 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1258 | /* first clear the S_IFMT bits */ |
| 1259 | result->st_mode ^= (result->st_mode & 0170000); |
| 1260 | /* now set the bits that make this a symlink */ |
| 1261 | result->st_mode |= 0120000; |
| 1262 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1263 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1264 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1267 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1268 | 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] | 1269 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1270 | HANDLE hFindFile; |
| 1271 | WIN32_FIND_DATAA FileData; |
| 1272 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1273 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1274 | return FALSE; |
| 1275 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1276 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1277 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1278 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1279 | info->ftCreationTime = FileData.ftCreationTime; |
| 1280 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1281 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1282 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1283 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1284 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1285 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1286 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1287 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1291 | 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] | 1292 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1293 | HANDLE hFindFile; |
| 1294 | WIN32_FIND_DATAW FileData; |
| 1295 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1296 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1297 | return FALSE; |
| 1298 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1299 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1300 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1301 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1302 | info->ftCreationTime = FileData.ftCreationTime; |
| 1303 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1304 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1305 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1306 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1307 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1308 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1309 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1310 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1313 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1314 | static int has_GetFinalPathNameByHandle = -1; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1315 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1316 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1317 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1318 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1319 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1320 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1321 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1322 | DWORD); |
| 1323 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1324 | /* only recheck */ |
Antoine Pitrou | 06eecea | 2012-10-21 16:33:33 +0200 | [diff] [blame] | 1325 | if (-1 == has_GetFinalPathNameByHandle) |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1326 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1327 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1328 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1329 | "GetFinalPathNameByHandleA"); |
| 1330 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1331 | "GetFinalPathNameByHandleW"); |
| 1332 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1333 | Py_GetFinalPathNameByHandleW; |
| 1334 | } |
| 1335 | return has_GetFinalPathNameByHandle; |
| 1336 | } |
| 1337 | |
| 1338 | static BOOL |
| 1339 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1340 | { |
| 1341 | int buf_size, result_length; |
| 1342 | wchar_t *buf; |
| 1343 | |
| 1344 | /* We have a good handle to the target, use it to determine |
| 1345 | the target path name (then we'll call lstat on it). */ |
| 1346 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1347 | VOLUME_NAME_DOS); |
| 1348 | if(!buf_size) |
| 1349 | return FALSE; |
| 1350 | |
| 1351 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1352 | if (!buf) { |
| 1353 | SetLastError(ERROR_OUTOFMEMORY); |
| 1354 | return FALSE; |
| 1355 | } |
| 1356 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1357 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1358 | buf, buf_size, VOLUME_NAME_DOS); |
| 1359 | |
| 1360 | if(!result_length) { |
| 1361 | free(buf); |
| 1362 | return FALSE; |
| 1363 | } |
| 1364 | |
| 1365 | if(!CloseHandle(hdl)) { |
| 1366 | free(buf); |
| 1367 | return FALSE; |
| 1368 | } |
| 1369 | |
| 1370 | buf[result_length] = 0; |
| 1371 | |
| 1372 | *target_path = buf; |
| 1373 | return TRUE; |
| 1374 | } |
| 1375 | |
| 1376 | static int |
| 1377 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1378 | BOOL traverse); |
| 1379 | static int |
| 1380 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1381 | BOOL traverse) |
| 1382 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1383 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1384 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1385 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1386 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1387 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1388 | const char *dot; |
| 1389 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1390 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1391 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1392 | traverse reparse point. */ |
| 1393 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1396 | hFile = CreateFileA( |
| 1397 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1398 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1399 | 0, /* share mode */ |
| 1400 | NULL, /* security attributes */ |
| 1401 | OPEN_EXISTING, |
| 1402 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1403 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1404 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1405 | the symlink path agin and not the actual final path. */ |
| 1406 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1407 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1408 | NULL); |
| 1409 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1410 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1411 | /* Either the target doesn't exist, or we don't have access to |
| 1412 | get a handle to it. If the former, we need to return an error. |
| 1413 | If the latter, we can use attributes_from_dir. */ |
| 1414 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1415 | return -1; |
| 1416 | /* Could not get attributes on open file. Fall back to |
| 1417 | reading the directory. */ |
| 1418 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1419 | /* Very strange. This should not fail now */ |
| 1420 | return -1; |
| 1421 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1422 | if (traverse) { |
| 1423 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1424 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1425 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1426 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1427 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1428 | } else { |
| 1429 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1430 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1431 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1432 | } |
| 1433 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1434 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1435 | return -1; |
| 1436 | |
| 1437 | /* Close the outer open file handle now that we're about to |
| 1438 | reopen it with different flags. */ |
| 1439 | if (!CloseHandle(hFile)) |
| 1440 | return -1; |
| 1441 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1442 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1443 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1444 | the file without the reparse handling flag set. */ |
| 1445 | hFile2 = CreateFileA( |
| 1446 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1447 | NULL, OPEN_EXISTING, |
| 1448 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1449 | NULL); |
| 1450 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1451 | return -1; |
| 1452 | |
| 1453 | if (!get_target_path(hFile2, &target_path)) |
| 1454 | return -1; |
| 1455 | |
| 1456 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1457 | free(target_path); |
| 1458 | return code; |
| 1459 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1460 | } else |
| 1461 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1462 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1463 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1464 | |
| 1465 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1466 | dot = strrchr(path, '.'); |
| 1467 | if (dot) { |
| 1468 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1469 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1470 | result->st_mode |= 0111; |
| 1471 | } |
| 1472 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1476 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1477 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1478 | { |
| 1479 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1480 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1481 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1482 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1483 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1484 | const wchar_t *dot; |
| 1485 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1486 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1487 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1488 | traverse reparse point. */ |
| 1489 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1492 | hFile = CreateFileW( |
| 1493 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1494 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1495 | 0, /* share mode */ |
| 1496 | NULL, /* security attributes */ |
| 1497 | OPEN_EXISTING, |
| 1498 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1499 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1500 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1501 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1502 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1503 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1504 | NULL); |
| 1505 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1506 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1507 | /* Either the target doesn't exist, or we don't have access to |
| 1508 | get a handle to it. If the former, we need to return an error. |
| 1509 | If the latter, we can use attributes_from_dir. */ |
| 1510 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1511 | return -1; |
| 1512 | /* Could not get attributes on open file. Fall back to |
| 1513 | reading the directory. */ |
| 1514 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1515 | /* Very strange. This should not fail now */ |
| 1516 | return -1; |
| 1517 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1518 | if (traverse) { |
| 1519 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1520 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1521 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1522 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1523 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1524 | } else { |
| 1525 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1526 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1527 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1528 | } |
| 1529 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1530 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1531 | return -1; |
| 1532 | |
| 1533 | /* Close the outer open file handle now that we're about to |
| 1534 | reopen it with different flags. */ |
| 1535 | if (!CloseHandle(hFile)) |
| 1536 | return -1; |
| 1537 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1538 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1539 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1540 | the file without the reparse handling flag set. */ |
| 1541 | hFile2 = CreateFileW( |
| 1542 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1543 | NULL, OPEN_EXISTING, |
| 1544 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1545 | NULL); |
| 1546 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1547 | return -1; |
| 1548 | |
| 1549 | if (!get_target_path(hFile2, &target_path)) |
| 1550 | return -1; |
| 1551 | |
| 1552 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1553 | free(target_path); |
| 1554 | return code; |
| 1555 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1556 | } else |
| 1557 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1558 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1559 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1560 | |
| 1561 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1562 | dot = wcsrchr(path, '.'); |
| 1563 | if (dot) { |
| 1564 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1565 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1566 | result->st_mode |= 0111; |
| 1567 | } |
| 1568 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1572 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1573 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1574 | /* Protocol violation: we explicitly clear errno, instead of |
| 1575 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1576 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1577 | errno = 0; |
| 1578 | return code; |
| 1579 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1580 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1581 | static int |
| 1582 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1583 | { |
| 1584 | /* Protocol violation: we explicitly clear errno, instead of |
| 1585 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1586 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1587 | errno = 0; |
| 1588 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1589 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1590 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1591 | |
| 1592 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1593 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1594 | default does not traverse symlinks and instead returns attributes for |
| 1595 | the symlink. |
| 1596 | |
| 1597 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1598 | win32_stat will first explicitly resolve the symlink target and then will |
| 1599 | call win32_lstat on that result. |
| 1600 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1601 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1602 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1603 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1604 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1605 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1606 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1609 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1610 | 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] | 1611 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1612 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | static int |
| 1616 | win32_stat(const char* path, struct win32_stat *result) |
| 1617 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1618 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1621 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1622 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1623 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1624 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | static int |
| 1628 | win32_fstat(int file_number, struct win32_stat *result) |
| 1629 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1630 | BY_HANDLE_FILE_INFORMATION info; |
| 1631 | HANDLE h; |
| 1632 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1633 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 1634 | if (!_PyVerify_fd(file_number)) |
| 1635 | h = INVALID_HANDLE_VALUE; |
| 1636 | else |
| 1637 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1638 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1639 | /* Protocol violation: we explicitly clear errno, instead of |
| 1640 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1641 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1642 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1643 | if (h == INVALID_HANDLE_VALUE) { |
| 1644 | /* This is really a C library error (invalid file handle). |
| 1645 | We set the Win32 error to the closes one matching. */ |
| 1646 | SetLastError(ERROR_INVALID_HANDLE); |
| 1647 | return -1; |
| 1648 | } |
| 1649 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1650 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1651 | type = GetFileType(h); |
| 1652 | if (type == FILE_TYPE_UNKNOWN) { |
| 1653 | DWORD error = GetLastError(); |
| 1654 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1655 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1656 | } |
| 1657 | /* else: valid but unknown file */ |
| 1658 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1659 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1660 | if (type != FILE_TYPE_DISK) { |
| 1661 | if (type == FILE_TYPE_CHAR) |
| 1662 | result->st_mode = _S_IFCHR; |
| 1663 | else if (type == FILE_TYPE_PIPE) |
| 1664 | result->st_mode = _S_IFIFO; |
| 1665 | return 0; |
| 1666 | } |
| 1667 | |
| 1668 | if (!GetFileInformationByHandle(h, &info)) { |
| 1669 | return -1; |
| 1670 | } |
| 1671 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1672 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1673 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1674 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1675 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | #endif /* MS_WINDOWS */ |
| 1679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1680 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1681 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1682 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1683 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1684 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1685 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1686 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1687 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1688 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1689 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1690 | |
| 1691 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1692 | {"st_mode", "protection bits"}, |
| 1693 | {"st_ino", "inode"}, |
| 1694 | {"st_dev", "device"}, |
| 1695 | {"st_nlink", "number of hard links"}, |
| 1696 | {"st_uid", "user ID of owner"}, |
| 1697 | {"st_gid", "group ID of owner"}, |
| 1698 | {"st_size", "total size, in bytes"}, |
| 1699 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1700 | {NULL, "integer time of last access"}, |
| 1701 | {NULL, "integer time of last modification"}, |
| 1702 | {NULL, "integer time of last change"}, |
| 1703 | {"st_atime", "time of last access"}, |
| 1704 | {"st_mtime", "time of last modification"}, |
| 1705 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1706 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1707 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1708 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1709 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1710 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1711 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1712 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1713 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1714 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1715 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1716 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1717 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1718 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1719 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1720 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1721 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1722 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1723 | #endif |
| 1724 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1725 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1726 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1727 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1728 | }; |
| 1729 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1730 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1731 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1732 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1733 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1734 | #endif |
| 1735 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1736 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1737 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1738 | #else |
| 1739 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1740 | #endif |
| 1741 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1742 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1743 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1744 | #else |
| 1745 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1746 | #endif |
| 1747 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1748 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1749 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1750 | #else |
| 1751 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1752 | #endif |
| 1753 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1754 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1755 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1756 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1757 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1758 | #endif |
| 1759 | |
| 1760 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1761 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1762 | #else |
| 1763 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1764 | #endif |
| 1765 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1766 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1767 | "stat_result", /* name */ |
| 1768 | stat_result__doc__, /* doc */ |
| 1769 | stat_result_fields, |
| 1770 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1771 | }; |
| 1772 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1773 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1774 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1775 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1776 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1777 | 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] | 1778 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1779 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1780 | |
| 1781 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1782 | {"f_bsize", }, |
| 1783 | {"f_frsize", }, |
| 1784 | {"f_blocks", }, |
| 1785 | {"f_bfree", }, |
| 1786 | {"f_bavail", }, |
| 1787 | {"f_files", }, |
| 1788 | {"f_ffree", }, |
| 1789 | {"f_favail", }, |
| 1790 | {"f_flag", }, |
| 1791 | {"f_namemax",}, |
| 1792 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1793 | }; |
| 1794 | |
| 1795 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1796 | "statvfs_result", /* name */ |
| 1797 | statvfs_result__doc__, /* doc */ |
| 1798 | statvfs_result_fields, |
| 1799 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1800 | }; |
| 1801 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1802 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1803 | PyDoc_STRVAR(waitid_result__doc__, |
| 1804 | "waitid_result: Result from waitid.\n\n\ |
| 1805 | This object may be accessed either as a tuple of\n\ |
| 1806 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1807 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1808 | \n\ |
| 1809 | See os.waitid for more information."); |
| 1810 | |
| 1811 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1812 | {"si_pid", }, |
| 1813 | {"si_uid", }, |
| 1814 | {"si_signo", }, |
| 1815 | {"si_status", }, |
| 1816 | {"si_code", }, |
| 1817 | {0} |
| 1818 | }; |
| 1819 | |
| 1820 | static PyStructSequence_Desc waitid_result_desc = { |
| 1821 | "waitid_result", /* name */ |
| 1822 | waitid_result__doc__, /* doc */ |
| 1823 | waitid_result_fields, |
| 1824 | 5 |
| 1825 | }; |
| 1826 | static PyTypeObject WaitidResultType; |
| 1827 | #endif |
| 1828 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1829 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1830 | static PyTypeObject StatResultType; |
| 1831 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1832 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1833 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1834 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1835 | static newfunc structseq_new; |
| 1836 | |
| 1837 | static PyObject * |
| 1838 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1839 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1840 | PyStructSequence *result; |
| 1841 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1842 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1843 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1844 | if (!result) |
| 1845 | return NULL; |
| 1846 | /* If we have been initialized from a tuple, |
| 1847 | st_?time might be set to None. Initialize it |
| 1848 | from the int slots. */ |
| 1849 | for (i = 7; i <= 9; i++) { |
| 1850 | if (result->ob_item[i+3] == Py_None) { |
| 1851 | Py_DECREF(Py_None); |
| 1852 | Py_INCREF(result->ob_item[i]); |
| 1853 | result->ob_item[i+3] = result->ob_item[i]; |
| 1854 | } |
| 1855 | } |
| 1856 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | |
| 1861 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1862 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1863 | |
| 1864 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1865 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1866 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1867 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1868 | future calls return ints. \n\ |
| 1869 | If newval is omitted, return the current setting.\n"); |
| 1870 | |
| 1871 | static PyObject* |
| 1872 | stat_float_times(PyObject* self, PyObject *args) |
| 1873 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1874 | int newval = -1; |
| 1875 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1876 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 1877 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1878 | "stat_float_times() is deprecated", |
| 1879 | 1)) |
| 1880 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1881 | if (newval == -1) |
| 1882 | /* Return old value */ |
| 1883 | return PyBool_FromLong(_stat_float_times); |
| 1884 | _stat_float_times = newval; |
| 1885 | Py_INCREF(Py_None); |
| 1886 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1887 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1888 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1889 | static PyObject *billion = NULL; |
| 1890 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1891 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1892 | 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] | 1893 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1894 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1895 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1896 | PyObject *s_in_ns = NULL; |
| 1897 | PyObject *ns_total = NULL; |
| 1898 | PyObject *float_s = NULL; |
| 1899 | |
| 1900 | if (!(s && ns_fractional)) |
| 1901 | goto exit; |
| 1902 | |
| 1903 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1904 | if (!s_in_ns) |
| 1905 | goto exit; |
| 1906 | |
| 1907 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1908 | if (!ns_total) |
| 1909 | goto exit; |
| 1910 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1911 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1912 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1913 | if (!float_s) |
| 1914 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1915 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1916 | else { |
| 1917 | float_s = s; |
| 1918 | Py_INCREF(float_s); |
| 1919 | } |
| 1920 | |
| 1921 | PyStructSequence_SET_ITEM(v, index, s); |
| 1922 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 1923 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 1924 | s = NULL; |
| 1925 | float_s = NULL; |
| 1926 | ns_total = NULL; |
| 1927 | exit: |
| 1928 | Py_XDECREF(s); |
| 1929 | Py_XDECREF(ns_fractional); |
| 1930 | Py_XDECREF(s_in_ns); |
| 1931 | Py_XDECREF(ns_total); |
| 1932 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1935 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1936 | (used by posix_stat() and posix_fstat()) */ |
| 1937 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1938 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1939 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1940 | unsigned long ansec, mnsec, cnsec; |
| 1941 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1942 | if (v == NULL) |
| 1943 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1944 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1945 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1946 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1947 | PyStructSequence_SET_ITEM(v, 1, |
| 1948 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1949 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1950 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1951 | #endif |
| 1952 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1953 | PyStructSequence_SET_ITEM(v, 2, |
| 1954 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1955 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1956 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1957 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1958 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1959 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1960 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1961 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1962 | PyStructSequence_SET_ITEM(v, 6, |
| 1963 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1964 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1965 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1966 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1967 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1968 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1969 | ansec = st->st_atim.tv_nsec; |
| 1970 | mnsec = st->st_mtim.tv_nsec; |
| 1971 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1972 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1973 | ansec = st->st_atimespec.tv_nsec; |
| 1974 | mnsec = st->st_mtimespec.tv_nsec; |
| 1975 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1976 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1977 | ansec = st->st_atime_nsec; |
| 1978 | mnsec = st->st_mtime_nsec; |
| 1979 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1980 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1981 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1982 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1983 | fill_time(v, 7, st->st_atime, ansec); |
| 1984 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1985 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1986 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1987 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1988 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1989 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1990 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1991 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1992 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1993 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1994 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1995 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1996 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1997 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1998 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1999 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2000 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2001 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2002 | #endif |
| 2003 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2004 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2005 | PyObject *val; |
| 2006 | unsigned long bsec,bnsec; |
| 2007 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2008 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2009 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2010 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2011 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2012 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2013 | if (_stat_float_times) { |
| 2014 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2015 | } else { |
| 2016 | val = PyLong_FromLong((long)bsec); |
| 2017 | } |
| 2018 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2019 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2020 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2021 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2022 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2023 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2024 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2025 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2026 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2027 | if (PyErr_Occurred()) { |
| 2028 | Py_DECREF(v); |
| 2029 | return NULL; |
| 2030 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2032 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2035 | /* POSIX methods */ |
| 2036 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2037 | |
| 2038 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2039 | posix_do_stat(char *function_name, path_t *path, |
| 2040 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2041 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2042 | STRUCT_STAT st; |
| 2043 | int result; |
| 2044 | |
| 2045 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2046 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2047 | return NULL; |
| 2048 | #endif |
| 2049 | |
| 2050 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2051 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2052 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2053 | return NULL; |
| 2054 | |
| 2055 | Py_BEGIN_ALLOW_THREADS |
| 2056 | if (path->fd != -1) |
| 2057 | result = FSTAT(path->fd, &st); |
| 2058 | else |
| 2059 | #ifdef MS_WINDOWS |
| 2060 | if (path->wide) { |
| 2061 | if (follow_symlinks) |
| 2062 | result = win32_stat_w(path->wide, &st); |
| 2063 | else |
| 2064 | result = win32_lstat_w(path->wide, &st); |
| 2065 | } |
| 2066 | else |
| 2067 | #endif |
| 2068 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2069 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2070 | result = LSTAT(path->narrow, &st); |
| 2071 | else |
| 2072 | #endif |
| 2073 | #ifdef HAVE_FSTATAT |
| 2074 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2075 | result = fstatat(dir_fd, path->narrow, &st, |
| 2076 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2077 | else |
| 2078 | #endif |
| 2079 | result = STAT(path->narrow, &st); |
| 2080 | Py_END_ALLOW_THREADS |
| 2081 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2082 | if (result != 0) { |
| 2083 | return path_error(path); |
| 2084 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2085 | |
| 2086 | return _pystat_fromstructstat(&st); |
| 2087 | } |
| 2088 | |
| 2089 | PyDoc_STRVAR(posix_stat__doc__, |
| 2090 | "stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result\n\n\ |
| 2091 | Perform a stat system call on the given path.\n\ |
| 2092 | \n\ |
| 2093 | path may be specified as either a string or as an open file descriptor.\n\ |
| 2094 | \n\ |
| 2095 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2096 | and path should be relative; path will then be relative to that directory.\n\ |
| 2097 | dir_fd may not be supported on your platform; if it is unavailable, using\n\ |
| 2098 | it will raise a NotImplementedError.\n\ |
| 2099 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2100 | link, stat will examine the symbolic link itself instead of the file the\n\ |
| 2101 | link points to.\n\ |
| 2102 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2103 | an open file descriptor."); |
| 2104 | |
| 2105 | static PyObject * |
| 2106 | posix_stat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2107 | { |
| 2108 | static char *keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2109 | path_t path; |
| 2110 | int dir_fd = DEFAULT_DIR_FD; |
| 2111 | int follow_symlinks = 1; |
| 2112 | PyObject *return_value; |
| 2113 | |
| 2114 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2115 | path.function_name = "stat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2116 | path.allow_fd = 1; |
| 2117 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords, |
| 2118 | path_converter, &path, |
| 2119 | #ifdef HAVE_FSTATAT |
| 2120 | dir_fd_converter, &dir_fd, |
| 2121 | #else |
| 2122 | dir_fd_unavailable, &dir_fd, |
| 2123 | #endif |
| 2124 | &follow_symlinks)) |
| 2125 | return NULL; |
| 2126 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2127 | path_cleanup(&path); |
| 2128 | return return_value; |
| 2129 | } |
| 2130 | |
| 2131 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2132 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2133 | Like stat(), but do not follow symbolic links.\n\ |
| 2134 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2135 | |
| 2136 | static PyObject * |
| 2137 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2138 | { |
| 2139 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2140 | path_t path; |
| 2141 | int dir_fd = DEFAULT_DIR_FD; |
| 2142 | int follow_symlinks = 0; |
| 2143 | PyObject *return_value; |
| 2144 | |
| 2145 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2146 | path.function_name = "lstat"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2147 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2148 | path_converter, &path, |
| 2149 | #ifdef HAVE_FSTATAT |
| 2150 | dir_fd_converter, &dir_fd |
| 2151 | #else |
| 2152 | dir_fd_unavailable, &dir_fd |
| 2153 | #endif |
| 2154 | )) |
| 2155 | return NULL; |
| 2156 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2157 | path_cleanup(&path); |
| 2158 | return return_value; |
| 2159 | } |
| 2160 | |
| 2161 | PyDoc_STRVAR(posix_access__doc__, |
| 2162 | "access(path, mode, *, dir_fd=None, effective_ids=False,\ |
| 2163 | follow_symlinks=True)\n\n\ |
| 2164 | Use the real uid/gid to test for access to a path. Returns True if granted,\n\ |
| 2165 | False otherwise.\n\ |
| 2166 | \n\ |
| 2167 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2168 | and path should be relative; path will then be relative to that directory.\n\ |
| 2169 | If effective_ids is True, access will use the effective uid/gid instead of\n\ |
| 2170 | the real uid/gid.\n\ |
| 2171 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2172 | link, access will examine the symbolic link itself instead of the file the\n\ |
| 2173 | link points to.\n\ |
| 2174 | dir_fd, effective_ids, and follow_symlinks may not be implemented\n\ |
| 2175 | on your platform. If they are unavailable, using them will raise a\n\ |
| 2176 | NotImplementedError.\n\ |
| 2177 | \n\ |
| 2178 | Note that most operations will use the effective uid/gid, therefore this\n\ |
| 2179 | routine can be used in a suid/sgid environment to test if the invoking user\n\ |
| 2180 | has the specified access to the path.\n\ |
| 2181 | The mode argument can be F_OK to test existence, or the inclusive-OR\n\ |
| 2182 | of R_OK, W_OK, and X_OK."); |
| 2183 | |
| 2184 | static PyObject * |
| 2185 | posix_access(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2186 | { |
| 2187 | static char *keywords[] = {"path", "mode", "dir_fd", "effective_ids", |
| 2188 | "follow_symlinks", NULL}; |
| 2189 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2190 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2191 | int dir_fd = DEFAULT_DIR_FD; |
| 2192 | int effective_ids = 0; |
| 2193 | int follow_symlinks = 1; |
| 2194 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2195 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2196 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2197 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2198 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2199 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2200 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2201 | |
| 2202 | memset(&path, 0, sizeof(path)); |
| 2203 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", keywords, |
| 2204 | path_converter, &path, &mode, |
| 2205 | #ifdef HAVE_FACCESSAT |
| 2206 | dir_fd_converter, &dir_fd, |
| 2207 | #else |
| 2208 | dir_fd_unavailable, &dir_fd, |
| 2209 | #endif |
| 2210 | &effective_ids, &follow_symlinks)) |
| 2211 | return NULL; |
| 2212 | |
| 2213 | #ifndef HAVE_FACCESSAT |
| 2214 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2215 | goto exit; |
| 2216 | |
| 2217 | if (effective_ids) { |
| 2218 | argument_unavailable_error("access", "effective_ids"); |
| 2219 | goto exit; |
| 2220 | } |
| 2221 | #endif |
| 2222 | |
| 2223 | #ifdef MS_WINDOWS |
| 2224 | Py_BEGIN_ALLOW_THREADS |
| 2225 | if (path.wide != NULL) |
| 2226 | attr = GetFileAttributesW(path.wide); |
| 2227 | else |
| 2228 | attr = GetFileAttributesA(path.narrow); |
| 2229 | Py_END_ALLOW_THREADS |
| 2230 | |
| 2231 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2232 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2233 | * * we didn't get a -1, and |
| 2234 | * * write access wasn't requested, |
| 2235 | * * or the file isn't read-only, |
| 2236 | * * or it's a directory. |
| 2237 | * (Directories cannot be read-only on Windows.) |
| 2238 | */ |
| 2239 | return_value = PyBool_FromLong( |
| 2240 | (attr != 0xFFFFFFFF) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2241 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2242 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2243 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2244 | #else |
| 2245 | |
| 2246 | Py_BEGIN_ALLOW_THREADS |
| 2247 | #ifdef HAVE_FACCESSAT |
| 2248 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2249 | effective_ids || |
| 2250 | !follow_symlinks) { |
| 2251 | int flags = 0; |
| 2252 | if (!follow_symlinks) |
| 2253 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2254 | if (effective_ids) |
| 2255 | flags |= AT_EACCESS; |
| 2256 | result = faccessat(dir_fd, path.narrow, mode, flags); |
| 2257 | } |
| 2258 | else |
| 2259 | #endif |
| 2260 | result = access(path.narrow, mode); |
| 2261 | Py_END_ALLOW_THREADS |
| 2262 | return_value = PyBool_FromLong(!result); |
| 2263 | #endif |
| 2264 | |
| 2265 | #ifndef HAVE_FACCESSAT |
| 2266 | exit: |
| 2267 | #endif |
| 2268 | path_cleanup(&path); |
| 2269 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2272 | #ifndef F_OK |
| 2273 | #define F_OK 0 |
| 2274 | #endif |
| 2275 | #ifndef R_OK |
| 2276 | #define R_OK 4 |
| 2277 | #endif |
| 2278 | #ifndef W_OK |
| 2279 | #define W_OK 2 |
| 2280 | #endif |
| 2281 | #ifndef X_OK |
| 2282 | #define X_OK 1 |
| 2283 | #endif |
| 2284 | |
| 2285 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2286 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2287 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2288 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2289 | |
| 2290 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2291 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2292 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2293 | int id; |
| 2294 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2295 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2296 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 2297 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2298 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2299 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2300 | /* file descriptor 0 only, the default input device (stdin) */ |
| 2301 | if (id == 0) { |
| 2302 | ret = ttyname(); |
| 2303 | } |
| 2304 | else { |
| 2305 | ret = NULL; |
| 2306 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2307 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2308 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2309 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2310 | if (ret == NULL) |
| 2311 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2312 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2313 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2314 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2315 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2316 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2317 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2318 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2319 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2320 | |
| 2321 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2322 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2323 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2324 | char *ret; |
| 2325 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2326 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2327 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2328 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2329 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2330 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2331 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2332 | if (ret == NULL) |
| 2333 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2334 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2335 | } |
| 2336 | #endif |
| 2337 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2338 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2339 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2340 | Change the current working directory to the specified path.\n\ |
| 2341 | \n\ |
| 2342 | path may always be specified as a string.\n\ |
| 2343 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2344 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2345 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2346 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2347 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2348 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2349 | path_t path; |
| 2350 | int result; |
| 2351 | PyObject *return_value = NULL; |
| 2352 | static char *keywords[] = {"path", NULL}; |
| 2353 | |
| 2354 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2355 | path.function_name = "chdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2356 | #ifdef HAVE_FCHDIR |
| 2357 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2358 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2359 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2360 | path_converter, &path |
| 2361 | )) |
| 2362 | return NULL; |
| 2363 | |
| 2364 | Py_BEGIN_ALLOW_THREADS |
| 2365 | #ifdef MS_WINDOWS |
| 2366 | if (path.wide) |
| 2367 | result = win32_wchdir(path.wide); |
| 2368 | else |
| 2369 | result = win32_chdir(path.narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2370 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2371 | #else |
| 2372 | #ifdef HAVE_FCHDIR |
| 2373 | if (path.fd != -1) |
| 2374 | result = fchdir(path.fd); |
| 2375 | else |
| 2376 | #endif |
| 2377 | result = chdir(path.narrow); |
| 2378 | #endif |
| 2379 | Py_END_ALLOW_THREADS |
| 2380 | |
| 2381 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2382 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2383 | goto exit; |
| 2384 | } |
| 2385 | |
| 2386 | return_value = Py_None; |
| 2387 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2388 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2389 | exit: |
| 2390 | path_cleanup(&path); |
| 2391 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2394 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2395 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2396 | "fchdir(fd)\n\n\ |
| 2397 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2398 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2399 | |
| 2400 | static PyObject * |
| 2401 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2402 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2403 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2404 | } |
| 2405 | #endif /* HAVE_FCHDIR */ |
| 2406 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2407 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2408 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2409 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2410 | Change the access permissions of a file.\n\ |
| 2411 | \n\ |
| 2412 | path may always be specified as a string.\n\ |
| 2413 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2414 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2415 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2416 | and path should be relative; path will then be relative to that directory.\n\ |
| 2417 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2418 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2419 | link points to.\n\ |
| 2420 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2421 | an open file descriptor.\n\ |
| 2422 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2423 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2424 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2425 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2426 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2427 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2428 | path_t path; |
| 2429 | int mode; |
| 2430 | int dir_fd = DEFAULT_DIR_FD; |
| 2431 | int follow_symlinks = 1; |
| 2432 | int result; |
| 2433 | PyObject *return_value = NULL; |
| 2434 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2435 | "follow_symlinks", NULL}; |
| 2436 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2437 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2438 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2439 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2440 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2441 | #ifdef HAVE_FCHMODAT |
| 2442 | int fchmodat_nofollow_unsupported = 0; |
| 2443 | #endif |
| 2444 | |
| 2445 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2446 | path.function_name = "chmod"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2447 | #ifdef HAVE_FCHMOD |
| 2448 | path.allow_fd = 1; |
| 2449 | #endif |
| 2450 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2451 | path_converter, &path, |
| 2452 | &mode, |
| 2453 | #ifdef HAVE_FCHMODAT |
| 2454 | dir_fd_converter, &dir_fd, |
| 2455 | #else |
| 2456 | dir_fd_unavailable, &dir_fd, |
| 2457 | #endif |
| 2458 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2459 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2460 | |
| 2461 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2462 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2463 | goto exit; |
| 2464 | #endif |
| 2465 | |
| 2466 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2467 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2468 | if (path.wide) |
| 2469 | attr = GetFileAttributesW(path.wide); |
| 2470 | else |
| 2471 | attr = GetFileAttributesA(path.narrow); |
| 2472 | if (attr == 0xFFFFFFFF) |
| 2473 | result = 0; |
| 2474 | else { |
| 2475 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2476 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2477 | else |
| 2478 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2479 | if (path.wide) |
| 2480 | result = SetFileAttributesW(path.wide, attr); |
| 2481 | else |
| 2482 | result = SetFileAttributesA(path.narrow, attr); |
| 2483 | } |
| 2484 | Py_END_ALLOW_THREADS |
| 2485 | |
| 2486 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 2487 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2488 | goto exit; |
| 2489 | } |
| 2490 | #else /* MS_WINDOWS */ |
| 2491 | Py_BEGIN_ALLOW_THREADS |
| 2492 | #ifdef HAVE_FCHMOD |
| 2493 | if (path.fd != -1) |
| 2494 | result = fchmod(path.fd, mode); |
| 2495 | else |
| 2496 | #endif |
| 2497 | #ifdef HAVE_LCHMOD |
| 2498 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2499 | result = lchmod(path.narrow, mode); |
| 2500 | else |
| 2501 | #endif |
| 2502 | #ifdef HAVE_FCHMODAT |
| 2503 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2504 | /* |
| 2505 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2506 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2507 | * and then says it isn't implemented yet. |
| 2508 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2509 | * |
| 2510 | * Once it is supported, os.chmod will automatically |
| 2511 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2512 | * Until then, we need to be careful what exception we raise. |
| 2513 | */ |
| 2514 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2515 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2516 | /* |
| 2517 | * But wait! We can't throw the exception without allowing threads, |
| 2518 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2519 | */ |
| 2520 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2521 | result && |
| 2522 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2523 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2524 | } |
| 2525 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2526 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2527 | result = chmod(path.narrow, mode); |
| 2528 | Py_END_ALLOW_THREADS |
| 2529 | |
| 2530 | if (result) { |
| 2531 | #ifdef HAVE_FCHMODAT |
| 2532 | if (fchmodat_nofollow_unsupported) { |
| 2533 | if (dir_fd != DEFAULT_DIR_FD) |
| 2534 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2535 | dir_fd, follow_symlinks); |
| 2536 | else |
| 2537 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2538 | } |
| 2539 | else |
| 2540 | #endif |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2541 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2542 | goto exit; |
| 2543 | } |
| 2544 | #endif |
| 2545 | |
| 2546 | Py_INCREF(Py_None); |
| 2547 | return_value = Py_None; |
| 2548 | exit: |
| 2549 | path_cleanup(&path); |
| 2550 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2553 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2554 | #ifdef HAVE_FCHMOD |
| 2555 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2556 | "fchmod(fd, mode)\n\n\ |
| 2557 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2558 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2559 | |
| 2560 | static PyObject * |
| 2561 | posix_fchmod(PyObject *self, PyObject *args) |
| 2562 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2563 | int fd, mode, res; |
| 2564 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2565 | return NULL; |
| 2566 | Py_BEGIN_ALLOW_THREADS |
| 2567 | res = fchmod(fd, mode); |
| 2568 | Py_END_ALLOW_THREADS |
| 2569 | if (res < 0) |
| 2570 | return posix_error(); |
| 2571 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2572 | } |
| 2573 | #endif /* HAVE_FCHMOD */ |
| 2574 | |
| 2575 | #ifdef HAVE_LCHMOD |
| 2576 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2577 | "lchmod(path, mode)\n\n\ |
| 2578 | 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] | 2579 | affects the link itself rather than the target.\n\ |
| 2580 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2581 | |
| 2582 | static PyObject * |
| 2583 | posix_lchmod(PyObject *self, PyObject *args) |
| 2584 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2585 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2586 | int i; |
| 2587 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2588 | memset(&path, 0, sizeof(path)); |
| 2589 | path.function_name = "lchmod"; |
| 2590 | if (!PyArg_ParseTuple(args, "O&i:lchmod", |
| 2591 | path_converter, &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2592 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2593 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2594 | res = lchmod(path.narrow, i); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2595 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2596 | if (res < 0) { |
| 2597 | path_error(&path); |
| 2598 | path_cleanup(&path); |
| 2599 | return NULL; |
| 2600 | } |
| 2601 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2602 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2603 | } |
| 2604 | #endif /* HAVE_LCHMOD */ |
| 2605 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2606 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2607 | #ifdef HAVE_CHFLAGS |
| 2608 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2609 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 2610 | Set file flags.\n\ |
| 2611 | \n\ |
| 2612 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2613 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 2614 | file the link points to.\n\ |
| 2615 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 2616 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2617 | |
| 2618 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2619 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2620 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2621 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2622 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2623 | int follow_symlinks = 1; |
| 2624 | int result; |
| 2625 | PyObject *return_value; |
| 2626 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 2627 | |
| 2628 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2629 | path.function_name = "chflags"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2630 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 2631 | path_converter, &path, |
| 2632 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2633 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2634 | |
| 2635 | #ifndef HAVE_LCHFLAGS |
| 2636 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 2637 | goto exit; |
| 2638 | #endif |
| 2639 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2640 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2641 | #ifdef HAVE_LCHFLAGS |
| 2642 | if (!follow_symlinks) |
| 2643 | result = lchflags(path.narrow, flags); |
| 2644 | else |
| 2645 | #endif |
| 2646 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2647 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2648 | |
| 2649 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2650 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2651 | goto exit; |
| 2652 | } |
| 2653 | |
| 2654 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2655 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2656 | |
| 2657 | exit: |
| 2658 | path_cleanup(&path); |
| 2659 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2660 | } |
| 2661 | #endif /* HAVE_CHFLAGS */ |
| 2662 | |
| 2663 | #ifdef HAVE_LCHFLAGS |
| 2664 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2665 | "lchflags(path, flags)\n\n\ |
| 2666 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2667 | This function will not follow symbolic links.\n\ |
| 2668 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2669 | |
| 2670 | static PyObject * |
| 2671 | posix_lchflags(PyObject *self, PyObject *args) |
| 2672 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2673 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2674 | unsigned long flags; |
| 2675 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2676 | memset(&path, 0, sizeof(path)); |
| 2677 | path.function_name = "lchflags"; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2678 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2679 | path_converter, &path, &flags)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2680 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2681 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2682 | res = lchflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2683 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2684 | if (res < 0) { |
| 2685 | path_error(&path); |
| 2686 | path_cleanup(&path); |
| 2687 | return NULL; |
| 2688 | } |
| 2689 | path_cleanup(&path); |
| 2690 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2691 | } |
| 2692 | #endif /* HAVE_LCHFLAGS */ |
| 2693 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2694 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2695 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2696 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2697 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2698 | |
| 2699 | static PyObject * |
| 2700 | posix_chroot(PyObject *self, PyObject *args) |
| 2701 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2702 | return posix_1str("chroot", args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2703 | } |
| 2704 | #endif |
| 2705 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2706 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2707 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2708 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2709 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2710 | |
| 2711 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2712 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2713 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2714 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2715 | } |
| 2716 | #endif /* HAVE_FSYNC */ |
| 2717 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2718 | #ifdef HAVE_SYNC |
| 2719 | PyDoc_STRVAR(posix_sync__doc__, |
| 2720 | "sync()\n\n\ |
| 2721 | Force write of everything to disk."); |
| 2722 | |
| 2723 | static PyObject * |
| 2724 | posix_sync(PyObject *self, PyObject *noargs) |
| 2725 | { |
| 2726 | Py_BEGIN_ALLOW_THREADS |
| 2727 | sync(); |
| 2728 | Py_END_ALLOW_THREADS |
| 2729 | Py_RETURN_NONE; |
| 2730 | } |
| 2731 | #endif |
| 2732 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2733 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2734 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2735 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2736 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2737 | #endif |
| 2738 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2739 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2740 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2741 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2742 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2743 | |
| 2744 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2745 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2746 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2747 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2748 | } |
| 2749 | #endif /* HAVE_FDATASYNC */ |
| 2750 | |
| 2751 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2752 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2753 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2754 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2755 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2756 | \n\ |
| 2757 | path may always be specified as a string.\n\ |
| 2758 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2759 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2760 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2761 | and path should be relative; path will then be relative to that directory.\n\ |
| 2762 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2763 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 2764 | link points to.\n\ |
| 2765 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2766 | an open file descriptor.\n\ |
| 2767 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2768 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2769 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2770 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2771 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2772 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2773 | path_t path; |
| 2774 | long uid_l, gid_l; |
| 2775 | uid_t uid; |
| 2776 | gid_t gid; |
| 2777 | int dir_fd = DEFAULT_DIR_FD; |
| 2778 | int follow_symlinks = 1; |
| 2779 | int result; |
| 2780 | PyObject *return_value = NULL; |
| 2781 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 2782 | "follow_symlinks", NULL}; |
| 2783 | |
| 2784 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2785 | path.function_name = "chown"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2786 | #ifdef HAVE_FCHOWN |
| 2787 | path.allow_fd = 1; |
| 2788 | #endif |
| 2789 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&ll|$O&p:chown", keywords, |
| 2790 | path_converter, &path, |
| 2791 | &uid_l, &gid_l, |
| 2792 | #ifdef HAVE_FCHOWNAT |
| 2793 | dir_fd_converter, &dir_fd, |
| 2794 | #else |
| 2795 | dir_fd_unavailable, &dir_fd, |
| 2796 | #endif |
| 2797 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2798 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2799 | |
| 2800 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 2801 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 2802 | goto exit; |
| 2803 | #endif |
| 2804 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 2805 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 2806 | goto exit; |
| 2807 | |
| 2808 | #ifdef __APPLE__ |
| 2809 | /* |
| 2810 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 2811 | * (But we still have an lchown symbol because of weak-linking.) |
| 2812 | * It doesn't have fchownat either. So there's no possibility |
| 2813 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2814 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2815 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 2816 | follow_symlinks_specified("chown", follow_symlinks); |
| 2817 | goto exit; |
| 2818 | } |
| 2819 | #endif |
| 2820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2821 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2822 | uid = (uid_t)uid_l; |
| 2823 | gid = (uid_t)gid_l; |
| 2824 | #ifdef HAVE_FCHOWN |
| 2825 | if (path.fd != -1) |
| 2826 | result = fchown(path.fd, uid, gid); |
| 2827 | else |
| 2828 | #endif |
| 2829 | #ifdef HAVE_LCHOWN |
| 2830 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2831 | result = lchown(path.narrow, uid, gid); |
| 2832 | else |
| 2833 | #endif |
| 2834 | #ifdef HAVE_FCHOWNAT |
| 2835 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 2836 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 2837 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2838 | else |
| 2839 | #endif |
| 2840 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2841 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2842 | |
| 2843 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2844 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2845 | goto exit; |
| 2846 | } |
| 2847 | |
| 2848 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2849 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2850 | |
| 2851 | exit: |
| 2852 | path_cleanup(&path); |
| 2853 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2854 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2855 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2856 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2857 | #ifdef HAVE_FCHOWN |
| 2858 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2859 | "fchown(fd, uid, gid)\n\n\ |
| 2860 | 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] | 2861 | 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] | 2862 | |
| 2863 | static PyObject * |
| 2864 | posix_fchown(PyObject *self, PyObject *args) |
| 2865 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2866 | int fd; |
| 2867 | long uid, gid; |
| 2868 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2869 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2870 | return NULL; |
| 2871 | Py_BEGIN_ALLOW_THREADS |
| 2872 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2873 | Py_END_ALLOW_THREADS |
| 2874 | if (res < 0) |
| 2875 | return posix_error(); |
| 2876 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2877 | } |
| 2878 | #endif /* HAVE_FCHOWN */ |
| 2879 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2880 | #ifdef HAVE_LCHOWN |
| 2881 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2882 | "lchown(path, uid, gid)\n\n\ |
| 2883 | 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] | 2884 | This function will not follow symbolic links.\n\ |
| 2885 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2886 | |
| 2887 | static PyObject * |
| 2888 | posix_lchown(PyObject *self, PyObject *args) |
| 2889 | { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2890 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2891 | long uid, gid; |
| 2892 | int res; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2893 | memset(&path, 0, sizeof(path)); |
| 2894 | path.function_name = "lchown"; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2895 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2896 | path_converter, &path, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2897 | &uid, &gid)) |
| 2898 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2899 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2900 | res = lchown(path.narrow, (uid_t) uid, (gid_t) gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2901 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2902 | if (res < 0) { |
| 2903 | path_error(&path); |
| 2904 | path_cleanup(&path); |
| 2905 | return NULL; |
| 2906 | } |
| 2907 | path_cleanup(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2908 | Py_INCREF(Py_None); |
| 2909 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2910 | } |
| 2911 | #endif /* HAVE_LCHOWN */ |
| 2912 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2913 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2914 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2915 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2916 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2917 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2918 | char buf[1026]; |
| 2919 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2920 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2921 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2922 | if (!use_bytes) { |
| 2923 | wchar_t wbuf[1026]; |
| 2924 | wchar_t *wbuf2 = wbuf; |
| 2925 | PyObject *resobj; |
| 2926 | DWORD len; |
| 2927 | Py_BEGIN_ALLOW_THREADS |
| 2928 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2929 | /* If the buffer is large enough, len does not include the |
| 2930 | terminating \0. If the buffer is too small, len includes |
| 2931 | the space needed for the terminator. */ |
| 2932 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2933 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2934 | if (wbuf2) |
| 2935 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2936 | } |
| 2937 | Py_END_ALLOW_THREADS |
| 2938 | if (!wbuf2) { |
| 2939 | PyErr_NoMemory(); |
| 2940 | return NULL; |
| 2941 | } |
| 2942 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 2943 | if (wbuf2 != wbuf) |
| 2944 | free(wbuf2); |
| 2945 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2946 | } |
| 2947 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 2948 | if (wbuf2 != wbuf) |
| 2949 | free(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2950 | return resobj; |
| 2951 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 2952 | |
| 2953 | if (win32_warn_bytes_api()) |
| 2954 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2955 | #endif |
| 2956 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2957 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2958 | res = getcwd(buf, sizeof buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2959 | Py_END_ALLOW_THREADS |
| 2960 | if (res == NULL) |
| 2961 | return posix_error(); |
| 2962 | if (use_bytes) |
| 2963 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2964 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2965 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2966 | |
| 2967 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2968 | "getcwd() -> path\n\n\ |
| 2969 | Return a unicode string representing the current working directory."); |
| 2970 | |
| 2971 | static PyObject * |
| 2972 | posix_getcwd_unicode(PyObject *self) |
| 2973 | { |
| 2974 | return posix_getcwd(0); |
| 2975 | } |
| 2976 | |
| 2977 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2978 | "getcwdb() -> path\n\n\ |
| 2979 | Return a bytes string representing the current working directory."); |
| 2980 | |
| 2981 | static PyObject * |
| 2982 | posix_getcwd_bytes(PyObject *self) |
| 2983 | { |
| 2984 | return posix_getcwd(1); |
| 2985 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2986 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2987 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2988 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 2989 | #define HAVE_LINK 1 |
| 2990 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2991 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2992 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2993 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2994 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 2995 | Create a hard link to a file.\n\ |
| 2996 | \n\ |
| 2997 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 2998 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 2999 | should be relative; the path will then be relative to that directory.\n\ |
| 3000 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3001 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3002 | file the link points to.\n\ |
| 3003 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3004 | platform. If they are unavailable, using them will raise a\n\ |
| 3005 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3006 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3007 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3008 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3009 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3010 | path_t src, dst; |
| 3011 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3012 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3013 | int follow_symlinks = 1; |
| 3014 | PyObject *return_value = NULL; |
| 3015 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3016 | "follow_symlinks", NULL}; |
| 3017 | #ifdef MS_WINDOWS |
| 3018 | BOOL result; |
| 3019 | #else |
| 3020 | int result; |
| 3021 | #endif |
| 3022 | |
| 3023 | memset(&src, 0, sizeof(src)); |
| 3024 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3025 | src.function_name = "link"; |
| 3026 | dst.function_name = "link"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3027 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3028 | path_converter, &src, |
| 3029 | path_converter, &dst, |
| 3030 | dir_fd_converter, &src_dir_fd, |
| 3031 | dir_fd_converter, &dst_dir_fd, |
| 3032 | &follow_symlinks)) |
| 3033 | return NULL; |
| 3034 | |
| 3035 | #ifndef HAVE_LINKAT |
| 3036 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3037 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3038 | goto exit; |
| 3039 | } |
| 3040 | #endif |
| 3041 | |
| 3042 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3043 | PyErr_SetString(PyExc_NotImplementedError, |
| 3044 | "link: src and dst must be the same type"); |
| 3045 | goto exit; |
| 3046 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3047 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3048 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3049 | Py_BEGIN_ALLOW_THREADS |
| 3050 | if (src.wide) |
| 3051 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3052 | else |
| 3053 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3054 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3055 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3056 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 3057 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3058 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3059 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3060 | #else |
| 3061 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3062 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3063 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3064 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3065 | (!follow_symlinks)) |
| 3066 | result = linkat(src_dir_fd, src.narrow, |
| 3067 | dst_dir_fd, dst.narrow, |
| 3068 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3069 | else |
| 3070 | #endif |
| 3071 | result = link(src.narrow, dst.narrow); |
| 3072 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3073 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3074 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3075 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3076 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3077 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3078 | #endif |
| 3079 | |
| 3080 | return_value = Py_None; |
| 3081 | Py_INCREF(Py_None); |
| 3082 | |
| 3083 | exit: |
| 3084 | path_cleanup(&src); |
| 3085 | path_cleanup(&dst); |
| 3086 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3087 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3088 | #endif |
| 3089 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3090 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3091 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3092 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3093 | "listdir(path='.') -> list_of_filenames\n\n\ |
| 3094 | 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] | 3095 | 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] | 3096 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3097 | \n\ |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3098 | path can be specified as either str or bytes. If path is bytes,\n\ |
| 3099 | the filenames returned will also be bytes; in all other circumstances\n\ |
| 3100 | the filenames returned will be str.\n\ |
| 3101 | On some platforms, path may also be specified as an open file descriptor;\n\ |
| 3102 | the file descriptor must refer to a directory.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3103 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3104 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3105 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3106 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3107 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3108 | path_t path; |
| 3109 | PyObject *list = NULL; |
| 3110 | static char *keywords[] = {"path", NULL}; |
| 3111 | int fd = -1; |
| 3112 | |
| 3113 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3114 | PyObject *v; |
| 3115 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3116 | BOOL result; |
| 3117 | WIN32_FIND_DATA FileData; |
| 3118 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 3119 | char *bufptr = namebuf; |
| 3120 | /* only claim to have space for MAX_PATH */ |
| 3121 | Py_ssize_t len = sizeof(namebuf)-5; |
| 3122 | PyObject *po = NULL; |
| 3123 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3124 | #else |
| 3125 | PyObject *v; |
| 3126 | DIR *dirp = NULL; |
| 3127 | struct dirent *ep; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3128 | int return_str; /* if false, return bytes */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3129 | #endif |
| 3130 | |
| 3131 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3132 | path.function_name = "listdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3133 | path.nullable = 1; |
| 3134 | #ifdef HAVE_FDOPENDIR |
| 3135 | path.allow_fd = 1; |
| 3136 | path.fd = -1; |
| 3137 | #endif |
| 3138 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3139 | path_converter, &path |
| 3140 | )) |
| 3141 | return NULL; |
| 3142 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3143 | /* XXX Should redo this putting the (now four) versions of opendir |
| 3144 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3145 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3146 | if (!path.narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3147 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3148 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3149 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3150 | if (!path.wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3151 | po_wchars = L"."; |
| 3152 | len = 1; |
| 3153 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3154 | po_wchars = path.wide; |
| 3155 | len = wcslen(path.wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3156 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3157 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3158 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 3159 | if (!wnamebuf) { |
| 3160 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3161 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3162 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3163 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3164 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3165 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3166 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 3167 | wnamebuf[len++] = L'\\'; |
| 3168 | wcscpy(wnamebuf + len, L"*.*"); |
| 3169 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3170 | if ((list = PyList_New(0)) == NULL) { |
| 3171 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3172 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3173 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3174 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3175 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3176 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3177 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3178 | if (error == ERROR_FILE_NOT_FOUND) |
| 3179 | goto exit; |
| 3180 | Py_DECREF(list); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3181 | list = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3182 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3183 | } |
| 3184 | do { |
| 3185 | /* Skip over . and .. */ |
| 3186 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3187 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3188 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3189 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3190 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3191 | Py_DECREF(list); |
| 3192 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3193 | break; |
| 3194 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3195 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3196 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3197 | Py_DECREF(list); |
| 3198 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3199 | break; |
| 3200 | } |
| 3201 | Py_DECREF(v); |
| 3202 | } |
| 3203 | Py_BEGIN_ALLOW_THREADS |
| 3204 | result = FindNextFileW(hFindFile, &wFileData); |
| 3205 | Py_END_ALLOW_THREADS |
| 3206 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3207 | it got to the end of the directory. */ |
| 3208 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3209 | Py_DECREF(list); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3210 | list = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3211 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3212 | } |
| 3213 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3214 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3215 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3216 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3217 | strcpy(namebuf, path.narrow); |
| 3218 | len = path.length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3219 | if (len > 0) { |
| 3220 | char ch = namebuf[len-1]; |
| 3221 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 3222 | namebuf[len++] = '/'; |
| 3223 | strcpy(namebuf + len, "*.*"); |
| 3224 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3225 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3226 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3227 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3228 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3229 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3230 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3231 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3232 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3233 | int error = GetLastError(); |
| 3234 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3235 | goto exit; |
| 3236 | Py_DECREF(list); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3237 | list = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3238 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3239 | } |
| 3240 | do { |
| 3241 | /* Skip over . and .. */ |
| 3242 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3243 | strcmp(FileData.cFileName, "..") != 0) { |
| 3244 | v = PyBytes_FromString(FileData.cFileName); |
| 3245 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3246 | Py_DECREF(list); |
| 3247 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3248 | break; |
| 3249 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3250 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3251 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3252 | Py_DECREF(list); |
| 3253 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3254 | break; |
| 3255 | } |
| 3256 | Py_DECREF(v); |
| 3257 | } |
| 3258 | Py_BEGIN_ALLOW_THREADS |
| 3259 | result = FindNextFile(hFindFile, &FileData); |
| 3260 | Py_END_ALLOW_THREADS |
| 3261 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3262 | it got to the end of the directory. */ |
| 3263 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3264 | Py_DECREF(list); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3265 | list = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3266 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3267 | } |
| 3268 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3269 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3270 | exit: |
| 3271 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3272 | if (FindClose(hFindFile) == FALSE) { |
| 3273 | if (list != NULL) { |
| 3274 | Py_DECREF(list); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3275 | list = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3276 | } |
| 3277 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3278 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3279 | if (wnamebuf) |
| 3280 | free(wnamebuf); |
| 3281 | path_cleanup(&path); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3282 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3283 | return list; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3284 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3285 | #else |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 3286 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3287 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3288 | #ifdef HAVE_FDOPENDIR |
| 3289 | if (path.fd != -1) { |
| 3290 | /* closedir() closes the FD, so we duplicate it */ |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3291 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3292 | fd = dup(path.fd); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3293 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3294 | |
| 3295 | if (fd == -1) { |
| 3296 | list = posix_error(); |
| 3297 | goto exit; |
| 3298 | } |
| 3299 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3300 | return_str = 1; |
| 3301 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3302 | Py_BEGIN_ALLOW_THREADS |
| 3303 | dirp = fdopendir(fd); |
| 3304 | Py_END_ALLOW_THREADS |
| 3305 | } |
| 3306 | else |
| 3307 | #endif |
| 3308 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3309 | char *name; |
| 3310 | if (path.narrow) { |
| 3311 | name = path.narrow; |
| 3312 | /* only return bytes if they specified a bytes object */ |
| 3313 | return_str = !(PyBytes_Check(path.object)); |
| 3314 | } |
| 3315 | else { |
| 3316 | name = "."; |
| 3317 | return_str = 1; |
| 3318 | } |
| 3319 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3320 | Py_BEGIN_ALLOW_THREADS |
| 3321 | dirp = opendir(name); |
| 3322 | Py_END_ALLOW_THREADS |
| 3323 | } |
| 3324 | |
| 3325 | if (dirp == NULL) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3326 | list = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3327 | goto exit; |
| 3328 | } |
| 3329 | if ((list = PyList_New(0)) == NULL) { |
| 3330 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3331 | } |
| 3332 | for (;;) { |
| 3333 | errno = 0; |
| 3334 | Py_BEGIN_ALLOW_THREADS |
| 3335 | ep = readdir(dirp); |
| 3336 | Py_END_ALLOW_THREADS |
| 3337 | if (ep == NULL) { |
| 3338 | if (errno == 0) { |
| 3339 | break; |
| 3340 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3341 | Py_DECREF(list); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3342 | list = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3343 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3344 | } |
| 3345 | } |
| 3346 | if (ep->d_name[0] == '.' && |
| 3347 | (NAMLEN(ep) == 1 || |
| 3348 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3349 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3350 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3351 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3352 | else |
| 3353 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3354 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3355 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3356 | break; |
| 3357 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3358 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3359 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3360 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3361 | break; |
| 3362 | } |
| 3363 | Py_DECREF(v); |
| 3364 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3365 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3366 | exit: |
| 3367 | if (dirp != NULL) { |
| 3368 | Py_BEGIN_ALLOW_THREADS |
| 3369 | if (fd > -1) |
| 3370 | rewinddir(dirp); |
| 3371 | closedir(dirp); |
| 3372 | Py_END_ALLOW_THREADS |
| 3373 | } |
| 3374 | |
| 3375 | path_cleanup(&path); |
| 3376 | |
| 3377 | return list; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3378 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 3379 | #endif /* which OS */ |
| 3380 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3381 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3382 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3383 | /* A helper function for abspath on win32 */ |
| 3384 | static PyObject * |
| 3385 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3386 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3387 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3388 | char outbuf[MAX_PATH*2]; |
| 3389 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3390 | PyObject *po; |
| 3391 | |
| 3392 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3393 | { |
| 3394 | wchar_t *wpath; |
| 3395 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 3396 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3397 | DWORD result; |
| 3398 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3399 | |
| 3400 | wpath = PyUnicode_AsUnicode(po); |
| 3401 | if (wpath == NULL) |
| 3402 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3403 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3404 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3405 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3406 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3407 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3408 | if (!woutbufp) |
| 3409 | return PyErr_NoMemory(); |
| 3410 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3411 | } |
| 3412 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3413 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3414 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3415 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3416 | if (woutbufp != woutbuf) |
| 3417 | free(woutbufp); |
| 3418 | return v; |
| 3419 | } |
| 3420 | /* Drop the argument parsing error as narrow strings |
| 3421 | are also valid. */ |
| 3422 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3423 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3424 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3425 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3426 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3427 | if (win32_warn_bytes_api()) |
| 3428 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3429 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3430 | outbuf, &temp)) { |
| 3431 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3432 | return NULL; |
| 3433 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3434 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3435 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3436 | Py_FileSystemDefaultEncoding, NULL); |
| 3437 | } |
| 3438 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3439 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3440 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3441 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3442 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3443 | /* A helper function for samepath on windows */ |
| 3444 | static PyObject * |
| 3445 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3446 | { |
| 3447 | HANDLE hFile; |
| 3448 | int buf_size; |
| 3449 | wchar_t *target_path; |
| 3450 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3451 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3452 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3453 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3454 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3455 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3456 | path = PyUnicode_AsUnicode(po); |
| 3457 | if (path == NULL) |
| 3458 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3459 | |
| 3460 | if(!check_GetFinalPathNameByHandle()) { |
| 3461 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3462 | NotImplementedError. */ |
| 3463 | return PyErr_Format(PyExc_NotImplementedError, |
| 3464 | "GetFinalPathNameByHandle not available on this platform"); |
| 3465 | } |
| 3466 | |
| 3467 | hFile = CreateFileW( |
| 3468 | path, |
| 3469 | 0, /* desired access */ |
| 3470 | 0, /* share mode */ |
| 3471 | NULL, /* security attributes */ |
| 3472 | OPEN_EXISTING, |
| 3473 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3474 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3475 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3476 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3477 | if(hFile == INVALID_HANDLE_VALUE) |
| 3478 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3479 | |
| 3480 | /* We have a good handle to the target, use it to determine the |
| 3481 | target path name. */ |
| 3482 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3483 | |
| 3484 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3485 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3486 | |
| 3487 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3488 | if(!target_path) |
| 3489 | return PyErr_NoMemory(); |
| 3490 | |
| 3491 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3492 | buf_size, VOLUME_NAME_DOS); |
| 3493 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3494 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3495 | |
| 3496 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3497 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3498 | |
| 3499 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3500 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3501 | free(target_path); |
| 3502 | return result; |
| 3503 | |
| 3504 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3505 | |
| 3506 | static PyObject * |
| 3507 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3508 | { |
| 3509 | HANDLE hFile; |
| 3510 | BY_HANDLE_FILE_INFORMATION info; |
| 3511 | int fd; |
| 3512 | |
| 3513 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3514 | return NULL; |
| 3515 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3516 | if (!_PyVerify_fd(fd)) |
| 3517 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3518 | |
| 3519 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3520 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3521 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3522 | |
| 3523 | if (!GetFileInformationByHandle(hFile, &info)) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3524 | return PyErr_SetFromWindowsErr(0); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3525 | |
| 3526 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3527 | info.nFileIndexHigh, |
| 3528 | info.nFileIndexLow); |
| 3529 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3530 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3531 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3532 | "Return true if the pathname refers to an existing directory."); |
| 3533 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3534 | static PyObject * |
| 3535 | posix__isdir(PyObject *self, PyObject *args) |
| 3536 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3537 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3538 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3539 | DWORD attributes; |
| 3540 | |
| 3541 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3542 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3543 | if (wpath == NULL) |
| 3544 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3545 | |
| 3546 | attributes = GetFileAttributesW(wpath); |
| 3547 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3548 | Py_RETURN_FALSE; |
| 3549 | goto check; |
| 3550 | } |
| 3551 | /* Drop the argument parsing error as narrow strings |
| 3552 | are also valid. */ |
| 3553 | PyErr_Clear(); |
| 3554 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3555 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3556 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3557 | if (win32_warn_bytes_api()) |
| 3558 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3559 | attributes = GetFileAttributesA(path); |
| 3560 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3561 | Py_RETURN_FALSE; |
| 3562 | |
| 3563 | check: |
| 3564 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3565 | Py_RETURN_TRUE; |
| 3566 | else |
| 3567 | Py_RETURN_FALSE; |
| 3568 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3569 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3571 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3572 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3573 | Create a directory.\n\ |
| 3574 | \n\ |
| 3575 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3576 | and path should be relative; path will then be relative to that directory.\n\ |
| 3577 | dir_fd may not be implemented on your platform.\n\ |
| 3578 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 3579 | \n\ |
| 3580 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3581 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3582 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3583 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3584 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3585 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3586 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3587 | int dir_fd = DEFAULT_DIR_FD; |
| 3588 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 3589 | PyObject *return_value = NULL; |
| 3590 | int result; |
| 3591 | |
| 3592 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3593 | path.function_name = "mkdir"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3594 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 3595 | path_converter, &path, &mode, |
| 3596 | #ifdef HAVE_MKDIRAT |
| 3597 | dir_fd_converter, &dir_fd |
| 3598 | #else |
| 3599 | dir_fd_unavailable, &dir_fd |
| 3600 | #endif |
| 3601 | )) |
| 3602 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3603 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3604 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3605 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3606 | if (path.wide) |
| 3607 | result = CreateDirectoryW(path.wide, NULL); |
| 3608 | else |
| 3609 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3610 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3611 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3612 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3613 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3614 | goto exit; |
| 3615 | } |
| 3616 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3617 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3618 | #if HAVE_MKDIRAT |
| 3619 | if (dir_fd != DEFAULT_DIR_FD) |
| 3620 | result = mkdirat(dir_fd, path.narrow, mode); |
| 3621 | else |
| 3622 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3623 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3624 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3625 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3626 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3627 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3628 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3629 | if (result < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3630 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3631 | goto exit; |
| 3632 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3633 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3634 | return_value = Py_None; |
| 3635 | Py_INCREF(Py_None); |
| 3636 | exit: |
| 3637 | path_cleanup(&path); |
| 3638 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3641 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3642 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3643 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3644 | #include <sys/resource.h> |
| 3645 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3646 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3647 | |
| 3648 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3649 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3650 | "nice(inc) -> new_priority\n\n\ |
| 3651 | 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] | 3652 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3653 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3654 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3655 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3656 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3657 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3658 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3659 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3660 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3661 | /* There are two flavours of 'nice': one that returns the new |
| 3662 | priority (as required by almost all standards out there) and the |
| 3663 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3664 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3665 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3666 | If we are of the nice family that returns the new priority, we |
| 3667 | need to clear errno before the call, and check if errno is filled |
| 3668 | before calling posix_error() on a returnvalue of -1, because the |
| 3669 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3670 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3671 | errno = 0; |
| 3672 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3673 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3674 | if (value == 0) |
| 3675 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3676 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3677 | if (value == -1 && errno != 0) |
| 3678 | /* either nice() or getpriority() returned an error */ |
| 3679 | return posix_error(); |
| 3680 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3681 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3682 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3683 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3684 | |
| 3685 | #ifdef HAVE_GETPRIORITY |
| 3686 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3687 | "getpriority(which, who) -> current_priority\n\n\ |
| 3688 | Get program scheduling priority."); |
| 3689 | |
| 3690 | static PyObject * |
| 3691 | posix_getpriority(PyObject *self, PyObject *args) |
| 3692 | { |
| 3693 | int which, who, retval; |
| 3694 | |
| 3695 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3696 | return NULL; |
| 3697 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3698 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3699 | if (errno != 0) |
| 3700 | return posix_error(); |
| 3701 | return PyLong_FromLong((long)retval); |
| 3702 | } |
| 3703 | #endif /* HAVE_GETPRIORITY */ |
| 3704 | |
| 3705 | |
| 3706 | #ifdef HAVE_SETPRIORITY |
| 3707 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3708 | "setpriority(which, who, prio) -> None\n\n\ |
| 3709 | Set program scheduling priority."); |
| 3710 | |
| 3711 | static PyObject * |
| 3712 | posix_setpriority(PyObject *self, PyObject *args) |
| 3713 | { |
| 3714 | int which, who, prio, retval; |
| 3715 | |
| 3716 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3717 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3718 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3719 | if (retval == -1) |
| 3720 | return posix_error(); |
| 3721 | Py_RETURN_NONE; |
| 3722 | } |
| 3723 | #endif /* HAVE_SETPRIORITY */ |
| 3724 | |
| 3725 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3726 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3727 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3728 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3729 | char *function_name = is_replace ? "replace" : "rename"; |
| 3730 | path_t src; |
| 3731 | path_t dst; |
| 3732 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3733 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3734 | int dir_fd_specified; |
| 3735 | PyObject *return_value = NULL; |
| 3736 | char format[24]; |
| 3737 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 3738 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3739 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3740 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3741 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3742 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3743 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3744 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3745 | |
| 3746 | memset(&src, 0, sizeof(src)); |
| 3747 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3748 | src.function_name = function_name; |
| 3749 | dst.function_name = function_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3750 | strcpy(format, "O&O&|$O&O&:"); |
| 3751 | strcat(format, function_name); |
| 3752 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 3753 | path_converter, &src, |
| 3754 | path_converter, &dst, |
| 3755 | dir_fd_converter, &src_dir_fd, |
| 3756 | dir_fd_converter, &dst_dir_fd)) |
| 3757 | return NULL; |
| 3758 | |
| 3759 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 3760 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 3761 | #ifndef HAVE_RENAMEAT |
| 3762 | if (dir_fd_specified) { |
| 3763 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 3764 | goto exit; |
| 3765 | } |
| 3766 | #endif |
| 3767 | |
| 3768 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3769 | PyErr_Format(PyExc_ValueError, |
| 3770 | "%s: src and dst must be the same type", function_name); |
| 3771 | goto exit; |
| 3772 | } |
| 3773 | |
| 3774 | #ifdef MS_WINDOWS |
| 3775 | Py_BEGIN_ALLOW_THREADS |
| 3776 | if (src.wide) |
| 3777 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 3778 | else |
| 3779 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 3780 | Py_END_ALLOW_THREADS |
| 3781 | |
| 3782 | if (!result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 3783 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3784 | goto exit; |
| 3785 | } |
| 3786 | |
| 3787 | #else |
| 3788 | Py_BEGIN_ALLOW_THREADS |
| 3789 | #ifdef HAVE_RENAMEAT |
| 3790 | if (dir_fd_specified) |
| 3791 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 3792 | else |
| 3793 | #endif |
| 3794 | result = rename(src.narrow, dst.narrow); |
| 3795 | Py_END_ALLOW_THREADS |
| 3796 | |
| 3797 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3798 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3799 | goto exit; |
| 3800 | } |
| 3801 | #endif |
| 3802 | |
| 3803 | Py_INCREF(Py_None); |
| 3804 | return_value = Py_None; |
| 3805 | exit: |
| 3806 | path_cleanup(&src); |
| 3807 | path_cleanup(&dst); |
| 3808 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3811 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3812 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 3813 | Rename a file or directory.\n\ |
| 3814 | \n\ |
| 3815 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3816 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3817 | should be relative; the path will then be relative to that directory.\n\ |
| 3818 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 3819 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3820 | |
| 3821 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3822 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3823 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3824 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3828 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 3829 | Rename a file or directory, overwriting the destination.\n\ |
| 3830 | \n\ |
| 3831 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3832 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3833 | should be relative; the path will then be relative to that directory.\n\ |
| 3834 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 3835 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3836 | |
| 3837 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3838 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3839 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3840 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3841 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3843 | PyDoc_STRVAR(posix_rmdir__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3844 | "rmdir(path, *, dir_fd=None)\n\n\ |
| 3845 | Remove a directory.\n\ |
| 3846 | \n\ |
| 3847 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3848 | and path should be relative; path will then be relative to that directory.\n\ |
| 3849 | dir_fd may not be implemented on your platform.\n\ |
| 3850 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3851 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3852 | static PyObject * |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3853 | posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3854 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3855 | path_t path; |
| 3856 | int dir_fd = DEFAULT_DIR_FD; |
| 3857 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 3858 | int result; |
| 3859 | PyObject *return_value = NULL; |
| 3860 | |
| 3861 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3862 | path.function_name = "rmdir"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3863 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords, |
| 3864 | path_converter, &path, |
| 3865 | #ifdef HAVE_UNLINKAT |
| 3866 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3867 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3868 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3869 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3870 | )) |
| 3871 | return NULL; |
| 3872 | |
| 3873 | Py_BEGIN_ALLOW_THREADS |
| 3874 | #ifdef MS_WINDOWS |
| 3875 | if (path.wide) |
| 3876 | result = RemoveDirectoryW(path.wide); |
| 3877 | else |
| 3878 | result = RemoveDirectoryA(path.narrow); |
| 3879 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 3880 | #else |
| 3881 | #ifdef HAVE_UNLINKAT |
| 3882 | if (dir_fd != DEFAULT_DIR_FD) |
| 3883 | result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR); |
| 3884 | else |
| 3885 | #endif |
| 3886 | result = rmdir(path.narrow); |
| 3887 | #endif |
| 3888 | Py_END_ALLOW_THREADS |
| 3889 | |
| 3890 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3891 | return_value = path_error(&path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3892 | goto exit; |
| 3893 | } |
| 3894 | |
| 3895 | return_value = Py_None; |
| 3896 | Py_INCREF(Py_None); |
| 3897 | |
| 3898 | exit: |
| 3899 | path_cleanup(&path); |
| 3900 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3903 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3904 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3905 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3906 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3907 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3908 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3909 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3910 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3911 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3912 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3913 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3914 | wchar_t *command; |
| 3915 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3916 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3917 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3918 | Py_BEGIN_ALLOW_THREADS |
| 3919 | sts = _wsystem(command); |
| 3920 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3921 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3922 | PyObject *command_obj; |
| 3923 | char *command; |
| 3924 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3925 | PyUnicode_FSConverter, &command_obj)) |
| 3926 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3927 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3928 | command = PyBytes_AsString(command_obj); |
| 3929 | Py_BEGIN_ALLOW_THREADS |
| 3930 | sts = system(command); |
| 3931 | Py_END_ALLOW_THREADS |
| 3932 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3933 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3934 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3935 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3936 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3937 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3938 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3939 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3940 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3941 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3942 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3943 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3944 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3945 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3946 | int i; |
| 3947 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3948 | return NULL; |
| 3949 | i = (int)umask(i); |
| 3950 | if (i < 0) |
| 3951 | return posix_error(); |
| 3952 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3955 | #ifdef MS_WINDOWS |
| 3956 | |
| 3957 | /* override the default DeleteFileW behavior so that directory |
| 3958 | symlinks can be removed with this function, the same as with |
| 3959 | Unix symlinks */ |
| 3960 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3961 | { |
| 3962 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3963 | WIN32_FIND_DATAW find_data; |
| 3964 | HANDLE find_data_handle; |
| 3965 | int is_directory = 0; |
| 3966 | int is_link = 0; |
| 3967 | |
| 3968 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3969 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3970 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3971 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3972 | it is a symlink */ |
| 3973 | if(is_directory && |
| 3974 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3975 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3976 | |
| 3977 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3978 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3979 | FindClose(find_data_handle); |
| 3980 | } |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | if (is_directory && is_link) |
| 3985 | return RemoveDirectoryW(lpFileName); |
| 3986 | |
| 3987 | return DeleteFileW(lpFileName); |
| 3988 | } |
| 3989 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3990 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3991 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3992 | "unlink(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3993 | Remove a file (same as remove()).\n\ |
| 3994 | \n\ |
| 3995 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3996 | and path should be relative; path will then be relative to that directory.\n\ |
| 3997 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 3998 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3999 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4000 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4001 | "remove(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4002 | Remove a file (same as unlink()).\n\ |
| 4003 | \n\ |
| 4004 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4005 | and path should be relative; path will then be relative to that directory.\n\ |
| 4006 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4007 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4008 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4009 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4010 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4011 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4012 | path_t path; |
| 4013 | int dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4014 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4015 | int result; |
| 4016 | PyObject *return_value = NULL; |
| 4017 | |
| 4018 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4019 | path.function_name = "unlink"; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4020 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4021 | path_converter, &path, |
| 4022 | #ifdef HAVE_UNLINKAT |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4023 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4024 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4025 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4026 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4027 | )) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4028 | return NULL; |
| 4029 | |
| 4030 | Py_BEGIN_ALLOW_THREADS |
| 4031 | #ifdef MS_WINDOWS |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4032 | if (path.wide) |
| 4033 | result = Py_DeleteFileW(path.wide); |
| 4034 | else |
| 4035 | result = DeleteFileA(path.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4036 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4037 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4038 | #ifdef HAVE_UNLINKAT |
| 4039 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4040 | result = unlinkat(dir_fd, path.narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4041 | else |
| 4042 | #endif /* HAVE_UNLINKAT */ |
| 4043 | result = unlink(path.narrow); |
| 4044 | #endif |
| 4045 | Py_END_ALLOW_THREADS |
| 4046 | |
| 4047 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4048 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4049 | goto exit; |
| 4050 | } |
| 4051 | |
| 4052 | return_value = Py_None; |
| 4053 | Py_INCREF(Py_None); |
| 4054 | |
| 4055 | exit: |
| 4056 | path_cleanup(&path); |
| 4057 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4060 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4061 | PyDoc_STRVAR(posix_uname__doc__, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4062 | "uname() -> uname_result\n\n\ |
| 4063 | Return an object identifying the current operating system.\n\ |
| 4064 | The object behaves like a named tuple with the following fields:\n\ |
| 4065 | (sysname, nodename, release, version, machine)"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4066 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4067 | static PyStructSequence_Field uname_result_fields[] = { |
| 4068 | {"sysname", "operating system name"}, |
| 4069 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4070 | {"release", "operating system release"}, |
| 4071 | {"version", "operating system version"}, |
| 4072 | {"machine", "hardware identifier"}, |
| 4073 | {NULL} |
| 4074 | }; |
| 4075 | |
| 4076 | PyDoc_STRVAR(uname_result__doc__, |
| 4077 | "uname_result: Result from os.uname().\n\n\ |
| 4078 | This object may be accessed either as a tuple of\n\ |
| 4079 | (sysname, nodename, release, version, machine),\n\ |
| 4080 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4081 | \n\ |
| 4082 | See os.uname for more information."); |
| 4083 | |
| 4084 | static PyStructSequence_Desc uname_result_desc = { |
| 4085 | "uname_result", /* name */ |
| 4086 | uname_result__doc__, /* doc */ |
| 4087 | uname_result_fields, |
| 4088 | 5 |
| 4089 | }; |
| 4090 | |
| 4091 | static PyTypeObject UnameResultType; |
| 4092 | |
| 4093 | |
| 4094 | #ifdef HAVE_UNAME |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4095 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4096 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4097 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4098 | struct utsname u; |
| 4099 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4100 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4101 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4102 | Py_BEGIN_ALLOW_THREADS |
| 4103 | res = uname(&u); |
| 4104 | Py_END_ALLOW_THREADS |
| 4105 | if (res < 0) |
| 4106 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4107 | |
| 4108 | value = PyStructSequence_New(&UnameResultType); |
| 4109 | if (value == NULL) |
| 4110 | return NULL; |
| 4111 | |
| 4112 | #define SET(i, field) \ |
| 4113 | { \ |
| 4114 | PyObject *o = PyUnicode_DecodeASCII(field, strlen(field), NULL); \ |
| 4115 | if (!o) { \ |
| 4116 | Py_DECREF(value); \ |
| 4117 | return NULL; \ |
| 4118 | } \ |
| 4119 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4120 | } \ |
| 4121 | |
| 4122 | SET(0, u.sysname); |
| 4123 | SET(1, u.nodename); |
| 4124 | SET(2, u.release); |
| 4125 | SET(3, u.version); |
| 4126 | SET(4, u.machine); |
| 4127 | |
| 4128 | #undef SET |
| 4129 | |
| 4130 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4131 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4132 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4133 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4134 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4135 | PyDoc_STRVAR(posix_utime__doc__, |
| 4136 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4137 | Set the access and modified time of path.\n\ |
| 4138 | \n\ |
| 4139 | path may always be specified as a string.\n\ |
| 4140 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4141 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4142 | \n\ |
| 4143 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4144 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4145 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4146 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4147 | since the epoch.\n\ |
| 4148 | If both times and ns are None, utime uses the current time.\n\ |
| 4149 | Specifying tuples for both times and ns is an error.\n\ |
| 4150 | \n\ |
| 4151 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4152 | and path should be relative; path will then be relative to that directory.\n\ |
| 4153 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4154 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4155 | link points to.\n\ |
| 4156 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4157 | as an open file descriptor.\n\ |
| 4158 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4159 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4160 | |
| 4161 | typedef struct { |
| 4162 | int now; |
| 4163 | time_t atime_s; |
| 4164 | long atime_ns; |
| 4165 | time_t mtime_s; |
| 4166 | long mtime_ns; |
| 4167 | } utime_t; |
| 4168 | |
| 4169 | /* |
| 4170 | * these macros assume that "utime" is a pointer to a utime_t |
| 4171 | * they also intentionally leak the declaration of a pointer named "time" |
| 4172 | */ |
| 4173 | #define UTIME_TO_TIMESPEC \ |
| 4174 | struct timespec ts[2]; \ |
| 4175 | struct timespec *time; \ |
| 4176 | if (utime->now) \ |
| 4177 | time = NULL; \ |
| 4178 | else { \ |
| 4179 | ts[0].tv_sec = utime->atime_s; \ |
| 4180 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4181 | ts[1].tv_sec = utime->mtime_s; \ |
| 4182 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4183 | time = ts; \ |
| 4184 | } \ |
| 4185 | |
| 4186 | #define UTIME_TO_TIMEVAL \ |
| 4187 | struct timeval tv[2]; \ |
| 4188 | struct timeval *time; \ |
| 4189 | if (utime->now) \ |
| 4190 | time = NULL; \ |
| 4191 | else { \ |
| 4192 | tv[0].tv_sec = utime->atime_s; \ |
| 4193 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4194 | tv[1].tv_sec = utime->mtime_s; \ |
| 4195 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4196 | time = tv; \ |
| 4197 | } \ |
| 4198 | |
| 4199 | #define UTIME_TO_UTIMBUF \ |
| 4200 | struct utimbuf u[2]; \ |
| 4201 | struct utimbuf *time; \ |
| 4202 | if (utime->now) \ |
| 4203 | time = NULL; \ |
| 4204 | else { \ |
| 4205 | u.actime = utime->atime_s; \ |
| 4206 | u.modtime = utime->mtime_s; \ |
| 4207 | time = u; \ |
| 4208 | } |
| 4209 | |
| 4210 | #define UTIME_TO_TIME_T \ |
| 4211 | time_t timet[2]; \ |
| 4212 | struct timet time; \ |
| 4213 | if (utime->now) \ |
| 4214 | time = NULL; \ |
| 4215 | else { \ |
| 4216 | timet[0] = utime->atime_s; \ |
| 4217 | timet[1] = utime->mtime_s; \ |
| 4218 | time = &timet; \ |
| 4219 | } \ |
| 4220 | |
| 4221 | |
| 4222 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4223 | |
| 4224 | #if UTIME_HAVE_DIR_FD |
| 4225 | |
| 4226 | static int |
| 4227 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4228 | { |
| 4229 | #ifdef HAVE_UTIMENSAT |
| 4230 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4231 | UTIME_TO_TIMESPEC; |
| 4232 | return utimensat(dir_fd, path, time, flags); |
| 4233 | #elif defined(HAVE_FUTIMESAT) |
| 4234 | UTIME_TO_TIMEVAL; |
| 4235 | /* |
| 4236 | * follow_symlinks will never be false here; |
| 4237 | * we only allow !follow_symlinks and dir_fd together |
| 4238 | * if we have utimensat() |
| 4239 | */ |
| 4240 | assert(follow_symlinks); |
| 4241 | return futimesat(dir_fd, path, time); |
| 4242 | #endif |
| 4243 | } |
| 4244 | |
| 4245 | #endif |
| 4246 | |
| 4247 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4248 | |
| 4249 | #if UTIME_HAVE_FD |
| 4250 | |
| 4251 | static int |
| 4252 | utime_fd(utime_t *utime, int fd) |
| 4253 | { |
| 4254 | #ifdef HAVE_FUTIMENS |
| 4255 | UTIME_TO_TIMESPEC; |
| 4256 | return futimens(fd, time); |
| 4257 | #else |
| 4258 | UTIME_TO_TIMEVAL; |
| 4259 | return futimes(fd, time); |
| 4260 | #endif |
| 4261 | } |
| 4262 | |
| 4263 | #endif |
| 4264 | |
| 4265 | |
| 4266 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4267 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4268 | |
| 4269 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4270 | |
| 4271 | static int |
| 4272 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4273 | { |
| 4274 | #ifdef HAVE_UTIMENSAT |
| 4275 | UTIME_TO_TIMESPEC; |
| 4276 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4277 | #else |
| 4278 | UTIME_TO_TIMEVAL; |
| 4279 | return lutimes(path, time); |
| 4280 | #endif |
| 4281 | } |
| 4282 | |
| 4283 | #endif |
| 4284 | |
| 4285 | #ifndef MS_WINDOWS |
| 4286 | |
| 4287 | static int |
| 4288 | utime_default(utime_t *utime, char *path) |
| 4289 | { |
| 4290 | #ifdef HAVE_UTIMENSAT |
| 4291 | UTIME_TO_TIMESPEC; |
| 4292 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4293 | #elif defined(HAVE_UTIMES) |
| 4294 | UTIME_TO_TIMEVAL; |
| 4295 | return utimes(path, time); |
| 4296 | #elif defined(HAVE_UTIME_H) |
| 4297 | UTIME_TO_UTIMBUF; |
| 4298 | return utime(path, time); |
| 4299 | #else |
| 4300 | UTIME_TO_TIME_T; |
| 4301 | return utime(path, time); |
| 4302 | #endif |
| 4303 | } |
| 4304 | |
| 4305 | #endif |
| 4306 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4307 | static int |
| 4308 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4309 | { |
| 4310 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4311 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4312 | divmod = PyNumber_Divmod(py_long, billion); |
| 4313 | if (!divmod) |
| 4314 | goto exit; |
| 4315 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4316 | if ((*s == -1) && PyErr_Occurred()) |
| 4317 | goto exit; |
| 4318 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4319 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4320 | goto exit; |
| 4321 | |
| 4322 | result = 1; |
| 4323 | exit: |
| 4324 | Py_XDECREF(divmod); |
| 4325 | return result; |
| 4326 | } |
| 4327 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4328 | static PyObject * |
| 4329 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4330 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4331 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4332 | PyObject *times = NULL; |
| 4333 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4334 | int dir_fd = DEFAULT_DIR_FD; |
| 4335 | int follow_symlinks = 1; |
| 4336 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4337 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4338 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4339 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4340 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4341 | #ifdef MS_WINDOWS |
| 4342 | HANDLE hFile; |
| 4343 | FILETIME atime, mtime; |
| 4344 | #else |
| 4345 | int result; |
| 4346 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4347 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4348 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4349 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4350 | memset(&path, 0, sizeof(path)); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4351 | path.function_name = "utime"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4352 | #if UTIME_HAVE_FD |
| 4353 | path.allow_fd = 1; |
| 4354 | #endif |
| 4355 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4356 | "O&|O$OO&p:utime", keywords, |
| 4357 | path_converter, &path, |
| 4358 | ×, &ns, |
| 4359 | #if UTIME_HAVE_DIR_FD |
| 4360 | dir_fd_converter, &dir_fd, |
| 4361 | #else |
| 4362 | dir_fd_unavailable, &dir_fd, |
| 4363 | #endif |
| 4364 | &follow_symlinks |
| 4365 | )) |
| 4366 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4367 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4368 | if (times && (times != Py_None) && ns) { |
| 4369 | PyErr_SetString(PyExc_ValueError, |
| 4370 | "utime: you may specify either 'times'" |
| 4371 | " or 'ns' but not both"); |
| 4372 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | if (times && (times != Py_None)) { |
| 4376 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4377 | PyErr_SetString(PyExc_TypeError, |
| 4378 | "utime: 'times' must be either" |
| 4379 | " a tuple of two ints or None"); |
| 4380 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4381 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4382 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4383 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4384 | &utime.atime_s, &utime.atime_ns) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4385 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4386 | &utime.mtime_s, &utime.mtime_ns) == -1) { |
| 4387 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4388 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4389 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4390 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4391 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4392 | PyErr_SetString(PyExc_TypeError, |
| 4393 | "utime: 'ns' must be a tuple of two ints"); |
| 4394 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4395 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4396 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4397 | 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] | 4398 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4399 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4400 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4401 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4402 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4403 | } |
| 4404 | else { |
| 4405 | /* times and ns are both None/unspecified. use "now". */ |
| 4406 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4407 | } |
| 4408 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4409 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4410 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4411 | goto exit; |
| 4412 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4413 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4414 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4415 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4416 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4417 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4418 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4419 | #if !defined(HAVE_UTIMENSAT) |
| 4420 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4421 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4422 | "utime: cannot use dir_fd and follow_symlinks " |
| 4423 | "together on this platform"); |
| 4424 | goto exit; |
| 4425 | } |
| 4426 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4427 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4428 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4429 | Py_BEGIN_ALLOW_THREADS |
| 4430 | if (path.wide) |
| 4431 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4432 | NULL, OPEN_EXISTING, |
| 4433 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4434 | else |
| 4435 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4436 | NULL, OPEN_EXISTING, |
| 4437 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4438 | Py_END_ALLOW_THREADS |
| 4439 | if (hFile == INVALID_HANDLE_VALUE) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4440 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4441 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4442 | } |
| 4443 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4444 | if (utime.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4445 | SYSTEMTIME now; |
| 4446 | GetSystemTime(&now); |
| 4447 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 4448 | !SystemTimeToFileTime(&now, &atime)) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4449 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4450 | goto exit; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4451 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4452 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4453 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4454 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4455 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4456 | } |
| 4457 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4458 | /* Avoid putting the file name into the error here, |
| 4459 | as that may confuse the user into believing that |
| 4460 | something is wrong with the file, when it also |
| 4461 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4462 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4463 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4464 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4465 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4466 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4467 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4468 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4469 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4470 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4471 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4472 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4473 | |
| 4474 | #if UTIME_HAVE_DIR_FD |
| 4475 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4476 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4477 | else |
| 4478 | #endif |
| 4479 | |
| 4480 | #if UTIME_HAVE_FD |
| 4481 | if (path.fd != -1) |
| 4482 | result = utime_fd(&utime, path.fd); |
| 4483 | else |
| 4484 | #endif |
| 4485 | |
| 4486 | result = utime_default(&utime, path.narrow); |
| 4487 | |
| 4488 | Py_END_ALLOW_THREADS |
| 4489 | |
| 4490 | if (result < 0) { |
| 4491 | /* see previous comment about not putting filename in error here */ |
| 4492 | return_value = posix_error(); |
| 4493 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4494 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4495 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4496 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4497 | |
| 4498 | Py_INCREF(Py_None); |
| 4499 | return_value = Py_None; |
| 4500 | |
| 4501 | exit: |
| 4502 | path_cleanup(&path); |
| 4503 | #ifdef MS_WINDOWS |
| 4504 | if (hFile != INVALID_HANDLE_VALUE) |
| 4505 | CloseHandle(hFile); |
| 4506 | #endif |
| 4507 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4510 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4511 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4512 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4513 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4514 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4515 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4516 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4517 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4518 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4519 | int sts; |
| 4520 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 4521 | return NULL; |
| 4522 | _exit(sts); |
| 4523 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4526 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4527 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4528 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4529 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4530 | Py_ssize_t i; |
| 4531 | for (i = 0; i < count; i++) |
| 4532 | PyMem_Free(array[i]); |
| 4533 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4534 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4535 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4536 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4537 | int fsconvert_strdup(PyObject *o, char**out) |
| 4538 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4539 | PyObject *bytes; |
| 4540 | Py_ssize_t size; |
| 4541 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4542 | return 0; |
| 4543 | size = PyBytes_GET_SIZE(bytes); |
| 4544 | *out = PyMem_Malloc(size+1); |
| 4545 | if (!*out) |
| 4546 | return 0; |
| 4547 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4548 | Py_DECREF(bytes); |
| 4549 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4550 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4551 | #endif |
| 4552 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4553 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4554 | static char** |
| 4555 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4556 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4557 | char **envlist; |
| 4558 | Py_ssize_t i, pos, envc; |
| 4559 | PyObject *keys=NULL, *vals=NULL; |
| 4560 | PyObject *key, *val, *key2, *val2; |
| 4561 | char *p, *k, *v; |
| 4562 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4563 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4564 | i = PyMapping_Size(env); |
| 4565 | if (i < 0) |
| 4566 | return NULL; |
| 4567 | envlist = PyMem_NEW(char *, i + 1); |
| 4568 | if (envlist == NULL) { |
| 4569 | PyErr_NoMemory(); |
| 4570 | return NULL; |
| 4571 | } |
| 4572 | envc = 0; |
| 4573 | keys = PyMapping_Keys(env); |
| 4574 | vals = PyMapping_Values(env); |
| 4575 | if (!keys || !vals) |
| 4576 | goto error; |
| 4577 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4578 | PyErr_Format(PyExc_TypeError, |
| 4579 | "env.keys() or env.values() is not a list"); |
| 4580 | goto error; |
| 4581 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4582 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4583 | for (pos = 0; pos < i; pos++) { |
| 4584 | key = PyList_GetItem(keys, pos); |
| 4585 | val = PyList_GetItem(vals, pos); |
| 4586 | if (!key || !val) |
| 4587 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4588 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4589 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4590 | goto error; |
| 4591 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4592 | Py_DECREF(key2); |
| 4593 | goto error; |
| 4594 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4595 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4596 | k = PyBytes_AsString(key2); |
| 4597 | v = PyBytes_AsString(val2); |
| 4598 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4599 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4600 | p = PyMem_NEW(char, len); |
| 4601 | if (p == NULL) { |
| 4602 | PyErr_NoMemory(); |
| 4603 | Py_DECREF(key2); |
| 4604 | Py_DECREF(val2); |
| 4605 | goto error; |
| 4606 | } |
| 4607 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4608 | envlist[envc++] = p; |
| 4609 | Py_DECREF(key2); |
| 4610 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4611 | } |
| 4612 | Py_DECREF(vals); |
| 4613 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4614 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4615 | envlist[envc] = 0; |
| 4616 | *envc_ptr = envc; |
| 4617 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4618 | |
| 4619 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4620 | Py_XDECREF(keys); |
| 4621 | Py_XDECREF(vals); |
| 4622 | while (--envc >= 0) |
| 4623 | PyMem_DEL(envlist[envc]); |
| 4624 | PyMem_DEL(envlist); |
| 4625 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4626 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4627 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4628 | static char** |
| 4629 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4630 | { |
| 4631 | int i; |
| 4632 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4633 | if (argvlist == NULL) { |
| 4634 | PyErr_NoMemory(); |
| 4635 | return NULL; |
| 4636 | } |
| 4637 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4638 | PyObject* item = PySequence_ITEM(argv, i); |
| 4639 | if (item == NULL) |
| 4640 | goto fail; |
| 4641 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4642 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4643 | goto fail; |
| 4644 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4645 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4646 | } |
| 4647 | argvlist[*argc] = NULL; |
| 4648 | return argvlist; |
| 4649 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4650 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4651 | free_string_array(argvlist, *argc); |
| 4652 | return NULL; |
| 4653 | } |
| 4654 | #endif |
| 4655 | |
| 4656 | #ifdef HAVE_EXECV |
| 4657 | PyDoc_STRVAR(posix_execv__doc__, |
| 4658 | "execv(path, args)\n\n\ |
| 4659 | Execute an executable path with arguments, replacing current process.\n\ |
| 4660 | \n\ |
| 4661 | path: path of executable file\n\ |
| 4662 | args: tuple or list of strings"); |
| 4663 | |
| 4664 | static PyObject * |
| 4665 | posix_execv(PyObject *self, PyObject *args) |
| 4666 | { |
| 4667 | PyObject *opath; |
| 4668 | char *path; |
| 4669 | PyObject *argv; |
| 4670 | char **argvlist; |
| 4671 | Py_ssize_t argc; |
| 4672 | |
| 4673 | /* execv has two arguments: (path, argv), where |
| 4674 | argv is a list or tuple of strings. */ |
| 4675 | |
| 4676 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4677 | PyUnicode_FSConverter, |
| 4678 | &opath, &argv)) |
| 4679 | return NULL; |
| 4680 | path = PyBytes_AsString(opath); |
| 4681 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4682 | PyErr_SetString(PyExc_TypeError, |
| 4683 | "execv() arg 2 must be a tuple or list"); |
| 4684 | Py_DECREF(opath); |
| 4685 | return NULL; |
| 4686 | } |
| 4687 | argc = PySequence_Size(argv); |
| 4688 | if (argc < 1) { |
| 4689 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4690 | Py_DECREF(opath); |
| 4691 | return NULL; |
| 4692 | } |
| 4693 | |
| 4694 | argvlist = parse_arglist(argv, &argc); |
| 4695 | if (argvlist == NULL) { |
| 4696 | Py_DECREF(opath); |
| 4697 | return NULL; |
| 4698 | } |
| 4699 | |
| 4700 | execv(path, argvlist); |
| 4701 | |
| 4702 | /* If we get here it's definitely an error */ |
| 4703 | |
| 4704 | free_string_array(argvlist, argc); |
| 4705 | Py_DECREF(opath); |
| 4706 | return posix_error(); |
| 4707 | } |
| 4708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4709 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4710 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4711 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4712 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4713 | path: path of executable file\n\ |
| 4714 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4715 | env: dictionary of strings mapping to strings\n\ |
| 4716 | \n\ |
| 4717 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 4718 | execve will execute the program the file descriptor is open to.\n\ |
| 4719 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4721 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4722 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4723 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4724 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4725 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4726 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4727 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4728 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4729 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4730 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4731 | /* execve has three arguments: (path, argv, env), where |
| 4732 | argv is a list or tuple of strings and env is a dictionary |
| 4733 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4734 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4735 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4736 | path.function_name = "execve"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4737 | #ifdef HAVE_FEXECVE |
| 4738 | path.allow_fd = 1; |
| 4739 | #endif |
| 4740 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 4741 | path_converter, &path, |
| 4742 | &argv, &env |
| 4743 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4744 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4745 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4746 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4747 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4748 | "execve: argv must be a tuple or list"); |
| 4749 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4750 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4751 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4752 | if (!PyMapping_Check(env)) { |
| 4753 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4754 | "execve: environment must be a mapping object"); |
| 4755 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4756 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4757 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4758 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4759 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4760 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4761 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4762 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4763 | envlist = parse_envlist(env, &envc); |
| 4764 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4765 | goto fail; |
| 4766 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4767 | #ifdef HAVE_FEXECVE |
| 4768 | if (path.fd > -1) |
| 4769 | fexecve(path.fd, argvlist, envlist); |
| 4770 | else |
| 4771 | #endif |
| 4772 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4773 | |
| 4774 | /* If we get here it's definitely an error */ |
| 4775 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 4776 | path_error(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4777 | |
| 4778 | while (--envc >= 0) |
| 4779 | PyMem_DEL(envlist[envc]); |
| 4780 | PyMem_DEL(envlist); |
| 4781 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4782 | if (argvlist) |
| 4783 | free_string_array(argvlist, argc); |
| 4784 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4785 | return NULL; |
| 4786 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4787 | #endif /* HAVE_EXECV */ |
| 4788 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4789 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4790 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4791 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4792 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4793 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4794 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4795 | mode: mode of process creation\n\ |
| 4796 | path: path of executable file\n\ |
| 4797 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4798 | |
| 4799 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4800 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4801 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4802 | PyObject *opath; |
| 4803 | char *path; |
| 4804 | PyObject *argv; |
| 4805 | char **argvlist; |
| 4806 | int mode, i; |
| 4807 | Py_ssize_t argc; |
| 4808 | Py_intptr_t spawnval; |
| 4809 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4810 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4811 | /* spawnv has three arguments: (mode, path, argv), where |
| 4812 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4813 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4814 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4815 | PyUnicode_FSConverter, |
| 4816 | &opath, &argv)) |
| 4817 | return NULL; |
| 4818 | path = PyBytes_AsString(opath); |
| 4819 | if (PyList_Check(argv)) { |
| 4820 | argc = PyList_Size(argv); |
| 4821 | getitem = PyList_GetItem; |
| 4822 | } |
| 4823 | else if (PyTuple_Check(argv)) { |
| 4824 | argc = PyTuple_Size(argv); |
| 4825 | getitem = PyTuple_GetItem; |
| 4826 | } |
| 4827 | else { |
| 4828 | PyErr_SetString(PyExc_TypeError, |
| 4829 | "spawnv() arg 2 must be a tuple or list"); |
| 4830 | Py_DECREF(opath); |
| 4831 | return NULL; |
| 4832 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4833 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4834 | argvlist = PyMem_NEW(char *, argc+1); |
| 4835 | if (argvlist == NULL) { |
| 4836 | Py_DECREF(opath); |
| 4837 | return PyErr_NoMemory(); |
| 4838 | } |
| 4839 | for (i = 0; i < argc; i++) { |
| 4840 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4841 | &argvlist[i])) { |
| 4842 | free_string_array(argvlist, i); |
| 4843 | PyErr_SetString( |
| 4844 | PyExc_TypeError, |
| 4845 | "spawnv() arg 2 must contain only strings"); |
| 4846 | Py_DECREF(opath); |
| 4847 | return NULL; |
| 4848 | } |
| 4849 | } |
| 4850 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4851 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4852 | if (mode == _OLD_P_OVERLAY) |
| 4853 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4854 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4855 | Py_BEGIN_ALLOW_THREADS |
| 4856 | spawnval = _spawnv(mode, path, argvlist); |
| 4857 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4858 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4859 | free_string_array(argvlist, argc); |
| 4860 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4861 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4862 | if (spawnval == -1) |
| 4863 | return posix_error(); |
| 4864 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4865 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4866 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4867 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4868 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4869 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4873 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4874 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4875 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4876 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4877 | mode: mode of process creation\n\ |
| 4878 | path: path of executable file\n\ |
| 4879 | args: tuple or list of arguments\n\ |
| 4880 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4881 | |
| 4882 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4883 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4884 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4885 | PyObject *opath; |
| 4886 | char *path; |
| 4887 | PyObject *argv, *env; |
| 4888 | char **argvlist; |
| 4889 | char **envlist; |
| 4890 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4891 | int mode; |
| 4892 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4893 | Py_intptr_t spawnval; |
| 4894 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4895 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4896 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4897 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4898 | argv is a list or tuple of strings and env is a dictionary |
| 4899 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4900 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4901 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4902 | PyUnicode_FSConverter, |
| 4903 | &opath, &argv, &env)) |
| 4904 | return NULL; |
| 4905 | path = PyBytes_AsString(opath); |
| 4906 | if (PyList_Check(argv)) { |
| 4907 | argc = PyList_Size(argv); |
| 4908 | getitem = PyList_GetItem; |
| 4909 | } |
| 4910 | else if (PyTuple_Check(argv)) { |
| 4911 | argc = PyTuple_Size(argv); |
| 4912 | getitem = PyTuple_GetItem; |
| 4913 | } |
| 4914 | else { |
| 4915 | PyErr_SetString(PyExc_TypeError, |
| 4916 | "spawnve() arg 2 must be a tuple or list"); |
| 4917 | goto fail_0; |
| 4918 | } |
| 4919 | if (!PyMapping_Check(env)) { |
| 4920 | PyErr_SetString(PyExc_TypeError, |
| 4921 | "spawnve() arg 3 must be a mapping object"); |
| 4922 | goto fail_0; |
| 4923 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4924 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4925 | argvlist = PyMem_NEW(char *, argc+1); |
| 4926 | if (argvlist == NULL) { |
| 4927 | PyErr_NoMemory(); |
| 4928 | goto fail_0; |
| 4929 | } |
| 4930 | for (i = 0; i < argc; i++) { |
| 4931 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4932 | &argvlist[i])) |
| 4933 | { |
| 4934 | lastarg = i; |
| 4935 | goto fail_1; |
| 4936 | } |
| 4937 | } |
| 4938 | lastarg = argc; |
| 4939 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4940 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4941 | envlist = parse_envlist(env, &envc); |
| 4942 | if (envlist == NULL) |
| 4943 | goto fail_1; |
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 | if (mode == _OLD_P_OVERLAY) |
| 4946 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4948 | Py_BEGIN_ALLOW_THREADS |
| 4949 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4950 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4951 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4952 | if (spawnval == -1) |
| 4953 | (void) posix_error(); |
| 4954 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4955 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4956 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4957 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4958 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4959 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4960 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4961 | while (--envc >= 0) |
| 4962 | PyMem_DEL(envlist[envc]); |
| 4963 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4964 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4965 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4966 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4967 | Py_DECREF(opath); |
| 4968 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4969 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4970 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4971 | #endif /* HAVE_SPAWNV */ |
| 4972 | |
| 4973 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4974 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4975 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4976 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4977 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4978 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4979 | 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] | 4980 | |
| 4981 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4982 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4983 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4984 | pid_t pid; |
| 4985 | int result = 0; |
| 4986 | _PyImport_AcquireLock(); |
| 4987 | pid = fork1(); |
| 4988 | if (pid == 0) { |
| 4989 | /* child: this clobbers and resets the import lock. */ |
| 4990 | PyOS_AfterFork(); |
| 4991 | } else { |
| 4992 | /* parent: release the import lock. */ |
| 4993 | result = _PyImport_ReleaseLock(); |
| 4994 | } |
| 4995 | if (pid == -1) |
| 4996 | return posix_error(); |
| 4997 | if (result < 0) { |
| 4998 | /* Don't clobber the OSError if the fork failed. */ |
| 4999 | PyErr_SetString(PyExc_RuntimeError, |
| 5000 | "not holding the import lock"); |
| 5001 | return NULL; |
| 5002 | } |
| 5003 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5004 | } |
| 5005 | #endif |
| 5006 | |
| 5007 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5008 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5009 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5010 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5011 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5012 | 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] | 5013 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5014 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5015 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5016 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5017 | pid_t pid; |
| 5018 | int result = 0; |
| 5019 | _PyImport_AcquireLock(); |
| 5020 | pid = fork(); |
| 5021 | if (pid == 0) { |
| 5022 | /* child: this clobbers and resets the import lock. */ |
| 5023 | PyOS_AfterFork(); |
| 5024 | } else { |
| 5025 | /* parent: release the import lock. */ |
| 5026 | result = _PyImport_ReleaseLock(); |
| 5027 | } |
| 5028 | if (pid == -1) |
| 5029 | return posix_error(); |
| 5030 | if (result < 0) { |
| 5031 | /* Don't clobber the OSError if the fork failed. */ |
| 5032 | PyErr_SetString(PyExc_RuntimeError, |
| 5033 | "not holding the import lock"); |
| 5034 | return NULL; |
| 5035 | } |
| 5036 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5037 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5038 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5039 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5040 | #ifdef HAVE_SCHED_H |
| 5041 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5042 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5043 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5044 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5045 | "sched_get_priority_max(policy)\n\n\ |
| 5046 | Get the maximum scheduling priority for *policy*."); |
| 5047 | |
| 5048 | static PyObject * |
| 5049 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5050 | { |
| 5051 | int policy, max; |
| 5052 | |
| 5053 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5054 | return NULL; |
| 5055 | max = sched_get_priority_max(policy); |
| 5056 | if (max < 0) |
| 5057 | return posix_error(); |
| 5058 | return PyLong_FromLong(max); |
| 5059 | } |
| 5060 | |
| 5061 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5062 | "sched_get_priority_min(policy)\n\n\ |
| 5063 | Get the minimum scheduling priority for *policy*."); |
| 5064 | |
| 5065 | static PyObject * |
| 5066 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5067 | { |
| 5068 | int policy, min; |
| 5069 | |
| 5070 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5071 | return NULL; |
| 5072 | min = sched_get_priority_min(policy); |
| 5073 | if (min < 0) |
| 5074 | return posix_error(); |
| 5075 | return PyLong_FromLong(min); |
| 5076 | } |
| 5077 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5078 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5079 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5080 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5081 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5082 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5083 | "sched_getscheduler(pid)\n\n\ |
| 5084 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5085 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5086 | |
| 5087 | static PyObject * |
| 5088 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5089 | { |
| 5090 | pid_t pid; |
| 5091 | int policy; |
| 5092 | |
| 5093 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5094 | return NULL; |
| 5095 | policy = sched_getscheduler(pid); |
| 5096 | if (policy < 0) |
| 5097 | return posix_error(); |
| 5098 | return PyLong_FromLong(policy); |
| 5099 | } |
| 5100 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5101 | #endif |
| 5102 | |
| 5103 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5104 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5105 | static PyObject * |
| 5106 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5107 | { |
| 5108 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5109 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5110 | |
| 5111 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5112 | return NULL; |
| 5113 | res = PyStructSequence_New(type); |
| 5114 | if (!res) |
| 5115 | return NULL; |
| 5116 | Py_INCREF(priority); |
| 5117 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5118 | return res; |
| 5119 | } |
| 5120 | |
| 5121 | PyDoc_STRVAR(sched_param__doc__, |
| 5122 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5123 | Current has only one field: sched_priority"); |
| 5124 | |
| 5125 | static PyStructSequence_Field sched_param_fields[] = { |
| 5126 | {"sched_priority", "the scheduling priority"}, |
| 5127 | {0} |
| 5128 | }; |
| 5129 | |
| 5130 | static PyStructSequence_Desc sched_param_desc = { |
| 5131 | "sched_param", /* name */ |
| 5132 | sched_param__doc__, /* doc */ |
| 5133 | sched_param_fields, |
| 5134 | 1 |
| 5135 | }; |
| 5136 | |
| 5137 | static int |
| 5138 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5139 | { |
| 5140 | long priority; |
| 5141 | |
| 5142 | if (Py_TYPE(param) != &SchedParamType) { |
| 5143 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5144 | return 0; |
| 5145 | } |
| 5146 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5147 | if (priority == -1 && PyErr_Occurred()) |
| 5148 | return 0; |
| 5149 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5150 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5151 | return 0; |
| 5152 | } |
| 5153 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5154 | return 1; |
| 5155 | } |
| 5156 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5157 | #endif |
| 5158 | |
| 5159 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5160 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5161 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5162 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5163 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5164 | If *pid* is 0, the calling process is changed.\n\ |
| 5165 | *param* is an instance of sched_param."); |
| 5166 | |
| 5167 | static PyObject * |
| 5168 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5169 | { |
| 5170 | pid_t pid; |
| 5171 | int policy; |
| 5172 | struct sched_param param; |
| 5173 | |
| 5174 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5175 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5176 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5177 | |
| 5178 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5179 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5180 | ** scheduling policy under Solaris/Illumos, and others. |
| 5181 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5182 | */ |
| 5183 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5184 | return posix_error(); |
| 5185 | Py_RETURN_NONE; |
| 5186 | } |
| 5187 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5188 | #endif |
| 5189 | |
| 5190 | #ifdef HAVE_SCHED_SETPARAM |
| 5191 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5192 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5193 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5194 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5195 | sched_param class. A PID of 0 means the calling process."); |
| 5196 | |
| 5197 | static PyObject * |
| 5198 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5199 | { |
| 5200 | pid_t pid; |
| 5201 | struct sched_param param; |
| 5202 | PyObject *res, *priority; |
| 5203 | |
| 5204 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5205 | return NULL; |
| 5206 | if (sched_getparam(pid, ¶m)) |
| 5207 | return posix_error(); |
| 5208 | res = PyStructSequence_New(&SchedParamType); |
| 5209 | if (!res) |
| 5210 | return NULL; |
| 5211 | priority = PyLong_FromLong(param.sched_priority); |
| 5212 | if (!priority) { |
| 5213 | Py_DECREF(res); |
| 5214 | return NULL; |
| 5215 | } |
| 5216 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5217 | return res; |
| 5218 | } |
| 5219 | |
| 5220 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5221 | "sched_setparam(pid, param)\n\n\ |
| 5222 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5223 | A PID of 0 means the calling process."); |
| 5224 | |
| 5225 | static PyObject * |
| 5226 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5227 | { |
| 5228 | pid_t pid; |
| 5229 | struct sched_param param; |
| 5230 | |
| 5231 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5232 | &pid, &convert_sched_param, ¶m)) |
| 5233 | return NULL; |
| 5234 | if (sched_setparam(pid, ¶m)) |
| 5235 | return posix_error(); |
| 5236 | Py_RETURN_NONE; |
| 5237 | } |
| 5238 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5239 | #endif |
| 5240 | |
| 5241 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5242 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5243 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5244 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5245 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5246 | |
| 5247 | static PyObject * |
| 5248 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5249 | { |
| 5250 | pid_t pid; |
| 5251 | struct timespec interval; |
| 5252 | |
| 5253 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5254 | return NULL; |
| 5255 | if (sched_rr_get_interval(pid, &interval)) |
| 5256 | return posix_error(); |
| 5257 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5258 | } |
| 5259 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5260 | #endif |
| 5261 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5262 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5263 | "sched_yield()\n\n\ |
| 5264 | Voluntarily relinquish the CPU."); |
| 5265 | |
| 5266 | static PyObject * |
| 5267 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5268 | { |
| 5269 | if (sched_yield()) |
| 5270 | return posix_error(); |
| 5271 | Py_RETURN_NONE; |
| 5272 | } |
| 5273 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5274 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5275 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5276 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5277 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5278 | |
| 5279 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5280 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5281 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5282 | |
| 5283 | static PyObject * |
| 5284 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5285 | { |
| 5286 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5287 | int ncpus; |
| 5288 | size_t setsize; |
| 5289 | cpu_set_t *mask = NULL; |
| 5290 | PyObject *iterable, *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5291 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5292 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity", |
| 5293 | &pid, &iterable)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5294 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5295 | |
| 5296 | iterator = PyObject_GetIter(iterable); |
| 5297 | if (iterator == NULL) |
| 5298 | return NULL; |
| 5299 | |
| 5300 | ncpus = NCPUS_START; |
| 5301 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5302 | mask = CPU_ALLOC(ncpus); |
| 5303 | if (mask == NULL) { |
| 5304 | PyErr_NoMemory(); |
| 5305 | goto error; |
| 5306 | } |
| 5307 | CPU_ZERO_S(setsize, mask); |
| 5308 | |
| 5309 | while ((item = PyIter_Next(iterator))) { |
| 5310 | long cpu; |
| 5311 | if (!PyLong_Check(item)) { |
| 5312 | PyErr_Format(PyExc_TypeError, |
| 5313 | "expected an iterator of ints, " |
| 5314 | "but iterator yielded %R", |
| 5315 | Py_TYPE(item)); |
| 5316 | Py_DECREF(item); |
| 5317 | goto error; |
| 5318 | } |
| 5319 | cpu = PyLong_AsLong(item); |
| 5320 | Py_DECREF(item); |
| 5321 | if (cpu < 0) { |
| 5322 | if (!PyErr_Occurred()) |
| 5323 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5324 | goto error; |
| 5325 | } |
| 5326 | if (cpu > INT_MAX - 1) { |
| 5327 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5328 | goto error; |
| 5329 | } |
| 5330 | if (cpu >= ncpus) { |
| 5331 | /* Grow CPU mask to fit the CPU number */ |
| 5332 | int newncpus = ncpus; |
| 5333 | cpu_set_t *newmask; |
| 5334 | size_t newsetsize; |
| 5335 | while (newncpus <= cpu) { |
| 5336 | if (newncpus > INT_MAX / 2) |
| 5337 | newncpus = cpu + 1; |
| 5338 | else |
| 5339 | newncpus = newncpus * 2; |
| 5340 | } |
| 5341 | newmask = CPU_ALLOC(newncpus); |
| 5342 | if (newmask == NULL) { |
| 5343 | PyErr_NoMemory(); |
| 5344 | goto error; |
| 5345 | } |
| 5346 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5347 | CPU_ZERO_S(newsetsize, newmask); |
| 5348 | memcpy(newmask, mask, setsize); |
| 5349 | CPU_FREE(mask); |
| 5350 | setsize = newsetsize; |
| 5351 | mask = newmask; |
| 5352 | ncpus = newncpus; |
| 5353 | } |
| 5354 | CPU_SET_S(cpu, setsize, mask); |
| 5355 | } |
| 5356 | Py_CLEAR(iterator); |
| 5357 | |
| 5358 | if (sched_setaffinity(pid, setsize, mask)) { |
| 5359 | posix_error(); |
| 5360 | goto error; |
| 5361 | } |
| 5362 | CPU_FREE(mask); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5363 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5364 | |
| 5365 | error: |
| 5366 | if (mask) |
| 5367 | CPU_FREE(mask); |
| 5368 | Py_XDECREF(iterator); |
| 5369 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5373 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5374 | Return the affinity of the process with PID *pid*.\n\ |
| 5375 | The returned cpu_set will be of size *ncpus*."); |
| 5376 | |
| 5377 | static PyObject * |
| 5378 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5379 | { |
| 5380 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5381 | int cpu, ncpus, count; |
| 5382 | size_t setsize; |
| 5383 | cpu_set_t *mask = NULL; |
| 5384 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5385 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5386 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity", |
| 5387 | &pid)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5388 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5389 | |
| 5390 | ncpus = NCPUS_START; |
| 5391 | while (1) { |
| 5392 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5393 | mask = CPU_ALLOC(ncpus); |
| 5394 | if (mask == NULL) |
| 5395 | return PyErr_NoMemory(); |
| 5396 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5397 | break; |
| 5398 | CPU_FREE(mask); |
| 5399 | if (errno != EINVAL) |
| 5400 | return posix_error(); |
| 5401 | if (ncpus > INT_MAX / 2) { |
| 5402 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5403 | "a large enough CPU set"); |
| 5404 | return NULL; |
| 5405 | } |
| 5406 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5407 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5408 | |
| 5409 | res = PySet_New(NULL); |
| 5410 | if (res == NULL) |
| 5411 | goto error; |
| 5412 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5413 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5414 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5415 | --count; |
| 5416 | if (cpu_num == NULL) |
| 5417 | goto error; |
| 5418 | if (PySet_Add(res, cpu_num)) { |
| 5419 | Py_DECREF(cpu_num); |
| 5420 | goto error; |
| 5421 | } |
| 5422 | Py_DECREF(cpu_num); |
| 5423 | } |
| 5424 | } |
| 5425 | CPU_FREE(mask); |
| 5426 | return res; |
| 5427 | |
| 5428 | error: |
| 5429 | if (mask) |
| 5430 | CPU_FREE(mask); |
| 5431 | Py_XDECREF(res); |
| 5432 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5433 | } |
| 5434 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5435 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5436 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5437 | #endif /* HAVE_SCHED_H */ |
| 5438 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5439 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5440 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5441 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5442 | #define DEV_PTY_FILE "/dev/ptc" |
| 5443 | #define HAVE_DEV_PTMX |
| 5444 | #else |
| 5445 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5446 | #endif |
| 5447 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5448 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5449 | #ifdef HAVE_PTY_H |
| 5450 | #include <pty.h> |
| 5451 | #else |
| 5452 | #ifdef HAVE_LIBUTIL_H |
| 5453 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5454 | #else |
| 5455 | #ifdef HAVE_UTIL_H |
| 5456 | #include <util.h> |
| 5457 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5458 | #endif /* HAVE_LIBUTIL_H */ |
| 5459 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5460 | #ifdef HAVE_STROPTS_H |
| 5461 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5462 | #endif |
| 5463 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5464 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5465 | #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] | 5466 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5467 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5468 | 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] | 5469 | |
| 5470 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5471 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5473 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5474 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5475 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5476 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5477 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5478 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5479 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5480 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5481 | #endif |
| 5482 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5483 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5484 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5485 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5486 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5487 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5488 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5489 | if (slave_name == NULL) |
| 5490 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5491 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5492 | slave_fd = open(slave_name, O_RDWR); |
| 5493 | if (slave_fd < 0) |
| 5494 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5495 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5496 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5497 | if (master_fd < 0) |
| 5498 | return posix_error(); |
| 5499 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5500 | /* change permission of slave */ |
| 5501 | if (grantpt(master_fd) < 0) { |
| 5502 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5503 | return posix_error(); |
| 5504 | } |
| 5505 | /* unlock slave */ |
| 5506 | if (unlockpt(master_fd) < 0) { |
| 5507 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5508 | return posix_error(); |
| 5509 | } |
| 5510 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5511 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5512 | if (slave_name == NULL) |
| 5513 | return posix_error(); |
| 5514 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5515 | if (slave_fd < 0) |
| 5516 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5517 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5518 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5519 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5520 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5521 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5522 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5523 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5524 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5525 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5526 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5527 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5528 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5529 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5530 | |
| 5531 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5532 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5533 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5534 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5535 | 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] | 5536 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5537 | |
| 5538 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5539 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5540 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5541 | int master_fd = -1, result = 0; |
| 5542 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5543 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5544 | _PyImport_AcquireLock(); |
| 5545 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5546 | if (pid == 0) { |
| 5547 | /* child: this clobbers and resets the import lock. */ |
| 5548 | PyOS_AfterFork(); |
| 5549 | } else { |
| 5550 | /* parent: release the import lock. */ |
| 5551 | result = _PyImport_ReleaseLock(); |
| 5552 | } |
| 5553 | if (pid == -1) |
| 5554 | return posix_error(); |
| 5555 | if (result < 0) { |
| 5556 | /* Don't clobber the OSError if the fork failed. */ |
| 5557 | PyErr_SetString(PyExc_RuntimeError, |
| 5558 | "not holding the import lock"); |
| 5559 | return NULL; |
| 5560 | } |
| 5561 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5562 | } |
| 5563 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5564 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5565 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5566 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5567 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5568 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5569 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5570 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5571 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5572 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5573 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5574 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5575 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5576 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5577 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5578 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5579 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5580 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5581 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5582 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5583 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5584 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5585 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5586 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5587 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5588 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5589 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5590 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5591 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5592 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5593 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5594 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5595 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5596 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5597 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5598 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5599 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5600 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5601 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5602 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5603 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5604 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5605 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5606 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5607 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5608 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5609 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5610 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5611 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5612 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5615 | #ifdef HAVE_GETGROUPLIST |
| 5616 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5617 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5618 | Returns a list of groups to which a user belongs.\n\n\ |
| 5619 | user: username to lookup\n\ |
| 5620 | group: base group id of the user"); |
| 5621 | |
| 5622 | static PyObject * |
| 5623 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5624 | { |
| 5625 | #ifdef NGROUPS_MAX |
| 5626 | #define MAX_GROUPS NGROUPS_MAX |
| 5627 | #else |
| 5628 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5629 | #define MAX_GROUPS 64 |
| 5630 | #endif |
| 5631 | |
| 5632 | const char *user; |
| 5633 | int i, ngroups; |
| 5634 | PyObject *list; |
| 5635 | #ifdef __APPLE__ |
| 5636 | int *groups, basegid; |
| 5637 | #else |
| 5638 | gid_t *groups, basegid; |
| 5639 | #endif |
| 5640 | ngroups = MAX_GROUPS; |
| 5641 | |
| 5642 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5643 | return NULL; |
| 5644 | |
| 5645 | #ifdef __APPLE__ |
| 5646 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5647 | #else |
| 5648 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5649 | #endif |
| 5650 | if (groups == NULL) |
| 5651 | return PyErr_NoMemory(); |
| 5652 | |
| 5653 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5654 | PyMem_Del(groups); |
| 5655 | return posix_error(); |
| 5656 | } |
| 5657 | |
| 5658 | list = PyList_New(ngroups); |
| 5659 | if (list == NULL) { |
| 5660 | PyMem_Del(groups); |
| 5661 | return NULL; |
| 5662 | } |
| 5663 | |
| 5664 | for (i = 0; i < ngroups; i++) { |
| 5665 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5666 | if (o == NULL) { |
| 5667 | Py_DECREF(list); |
| 5668 | PyMem_Del(groups); |
| 5669 | return NULL; |
| 5670 | } |
| 5671 | PyList_SET_ITEM(list, i, o); |
| 5672 | } |
| 5673 | |
| 5674 | PyMem_Del(groups); |
| 5675 | |
| 5676 | return list; |
| 5677 | } |
| 5678 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5679 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5680 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5681 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5682 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5683 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5684 | |
| 5685 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5686 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5687 | { |
| 5688 | PyObject *result = NULL; |
| 5689 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5690 | #ifdef NGROUPS_MAX |
| 5691 | #define MAX_GROUPS NGROUPS_MAX |
| 5692 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5693 | /* 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] | 5694 | #define MAX_GROUPS 64 |
| 5695 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5696 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5697 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5698 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5699 | * This is a helper variable to store the intermediate result when |
| 5700 | * that happens. |
| 5701 | * |
| 5702 | * To keep the code readable the OSX behaviour is unconditional, |
| 5703 | * according to the POSIX spec this should be safe on all unix-y |
| 5704 | * systems. |
| 5705 | */ |
| 5706 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5707 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5708 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5709 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5710 | if (n < 0) { |
| 5711 | if (errno == EINVAL) { |
| 5712 | n = getgroups(0, NULL); |
| 5713 | if (n == -1) { |
| 5714 | return posix_error(); |
| 5715 | } |
| 5716 | if (n == 0) { |
| 5717 | /* Avoid malloc(0) */ |
| 5718 | alt_grouplist = grouplist; |
| 5719 | } else { |
| 5720 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5721 | if (alt_grouplist == NULL) { |
| 5722 | errno = EINVAL; |
| 5723 | return posix_error(); |
| 5724 | } |
| 5725 | n = getgroups(n, alt_grouplist); |
| 5726 | if (n == -1) { |
| 5727 | PyMem_Free(alt_grouplist); |
| 5728 | return posix_error(); |
| 5729 | } |
| 5730 | } |
| 5731 | } else { |
| 5732 | return posix_error(); |
| 5733 | } |
| 5734 | } |
| 5735 | result = PyList_New(n); |
| 5736 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5737 | int i; |
| 5738 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5739 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5740 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5741 | Py_DECREF(result); |
| 5742 | result = NULL; |
| 5743 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5744 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5745 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5746 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5747 | } |
| 5748 | |
| 5749 | if (alt_grouplist != grouplist) { |
| 5750 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5751 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5752 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5753 | return result; |
| 5754 | } |
| 5755 | #endif |
| 5756 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5757 | #ifdef HAVE_INITGROUPS |
| 5758 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5759 | "initgroups(username, gid) -> None\n\n\ |
| 5760 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5761 | the groups of which the specified username is a member, plus the specified\n\ |
| 5762 | group id."); |
| 5763 | |
| 5764 | static PyObject * |
| 5765 | posix_initgroups(PyObject *self, PyObject *args) |
| 5766 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5767 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5768 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5769 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5770 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5771 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5772 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5773 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5774 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5775 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5776 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5777 | res = initgroups(username, (gid_t) gid); |
| 5778 | Py_DECREF(oname); |
| 5779 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5780 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5781 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5782 | Py_INCREF(Py_None); |
| 5783 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5784 | } |
| 5785 | #endif |
| 5786 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5787 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5788 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5789 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5790 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5791 | |
| 5792 | static PyObject * |
| 5793 | posix_getpgid(PyObject *self, PyObject *args) |
| 5794 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5795 | pid_t pid, pgid; |
| 5796 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5797 | return NULL; |
| 5798 | pgid = getpgid(pid); |
| 5799 | if (pgid < 0) |
| 5800 | return posix_error(); |
| 5801 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5802 | } |
| 5803 | #endif /* HAVE_GETPGID */ |
| 5804 | |
| 5805 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5806 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5807 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5808 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5809 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5810 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5811 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5812 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5813 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5814 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5815 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5816 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5817 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5818 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5819 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5820 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5821 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5822 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5823 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5824 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5825 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5826 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5827 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5828 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5829 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5830 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5831 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5832 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5833 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5834 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5835 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5836 | return posix_error(); |
| 5837 | Py_INCREF(Py_None); |
| 5838 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5839 | } |
| 5840 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5841 | #endif /* HAVE_SETPGRP */ |
| 5842 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5843 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5844 | |
| 5845 | #ifdef MS_WINDOWS |
| 5846 | #include <tlhelp32.h> |
| 5847 | |
| 5848 | static PyObject* |
| 5849 | win32_getppid() |
| 5850 | { |
| 5851 | HANDLE snapshot; |
| 5852 | pid_t mypid; |
| 5853 | PyObject* result = NULL; |
| 5854 | BOOL have_record; |
| 5855 | PROCESSENTRY32 pe; |
| 5856 | |
| 5857 | mypid = getpid(); /* This function never fails */ |
| 5858 | |
| 5859 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 5860 | if (snapshot == INVALID_HANDLE_VALUE) |
| 5861 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5862 | |
| 5863 | pe.dwSize = sizeof(pe); |
| 5864 | have_record = Process32First(snapshot, &pe); |
| 5865 | while (have_record) { |
| 5866 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 5867 | /* We could cache the ulong value in a static variable. */ |
| 5868 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 5869 | break; |
| 5870 | } |
| 5871 | |
| 5872 | have_record = Process32Next(snapshot, &pe); |
| 5873 | } |
| 5874 | |
| 5875 | /* If our loop exits and our pid was not found (result will be NULL) |
| 5876 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 5877 | * error anyway, so let's raise it. */ |
| 5878 | if (!result) |
| 5879 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5880 | |
| 5881 | CloseHandle(snapshot); |
| 5882 | |
| 5883 | return result; |
| 5884 | } |
| 5885 | #endif /*MS_WINDOWS*/ |
| 5886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5887 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5888 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5889 | Return the parent's process id. If the parent process has already exited,\n\ |
| 5890 | Windows machines will still return its id; others systems will return the id\n\ |
| 5891 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5892 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5893 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5894 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5895 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5896 | #ifdef MS_WINDOWS |
| 5897 | return win32_getppid(); |
| 5898 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5899 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5900 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5901 | } |
| 5902 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5903 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5904 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5905 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5906 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5907 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5908 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5909 | |
| 5910 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5911 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5912 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5913 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5914 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5915 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 5916 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5917 | |
| 5918 | if (GetUserNameW(user_name, &num_chars)) { |
| 5919 | /* num_chars is the number of unicode chars plus null terminator */ |
| 5920 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5921 | } |
| 5922 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5923 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5924 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5925 | char *name; |
| 5926 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5927 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5928 | errno = 0; |
| 5929 | name = getlogin(); |
| 5930 | if (name == NULL) { |
| 5931 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5932 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5933 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5934 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5935 | } |
| 5936 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5937 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5938 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5939 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5940 | return result; |
| 5941 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5942 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5943 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5944 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5945 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5946 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5947 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5948 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5949 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5950 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5951 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5952 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5953 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5954 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5955 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5956 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5957 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5958 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5959 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5960 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5961 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5962 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5963 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5964 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5965 | pid_t pid; |
| 5966 | int sig; |
| 5967 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 5968 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5969 | if (kill(pid, sig) == -1) |
| 5970 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5971 | Py_INCREF(Py_None); |
| 5972 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5973 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5974 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5975 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5976 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5977 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5978 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5979 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5980 | |
| 5981 | static PyObject * |
| 5982 | posix_killpg(PyObject *self, PyObject *args) |
| 5983 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5984 | int sig; |
| 5985 | pid_t pgid; |
| 5986 | /* XXX some man pages make the `pgid` parameter an int, others |
| 5987 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 5988 | take the same type. Moreover, pid_t is always at least as wide as |
| 5989 | int (else compilation of this module fails), which is safe. */ |
| 5990 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5991 | return NULL; |
| 5992 | if (killpg(pgid, sig) == -1) |
| 5993 | return posix_error(); |
| 5994 | Py_INCREF(Py_None); |
| 5995 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5996 | } |
| 5997 | #endif |
| 5998 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5999 | #ifdef MS_WINDOWS |
| 6000 | PyDoc_STRVAR(win32_kill__doc__, |
| 6001 | "kill(pid, sig)\n\n\ |
| 6002 | Kill a process with a signal."); |
| 6003 | |
| 6004 | static PyObject * |
| 6005 | win32_kill(PyObject *self, PyObject *args) |
| 6006 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6007 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6008 | DWORD pid, sig, err; |
| 6009 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6010 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6011 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 6012 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6013 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6014 | /* Console processes which share a common console can be sent CTRL+C or |
| 6015 | CTRL+BREAK events, provided they handle said events. */ |
| 6016 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 6017 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 6018 | err = GetLastError(); |
| 6019 | PyErr_SetFromWindowsErr(err); |
| 6020 | } |
| 6021 | else |
| 6022 | Py_RETURN_NONE; |
| 6023 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6024 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6025 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6026 | attempt to open and terminate the process. */ |
| 6027 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 6028 | if (handle == NULL) { |
| 6029 | err = GetLastError(); |
| 6030 | return PyErr_SetFromWindowsErr(err); |
| 6031 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6032 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6033 | if (TerminateProcess(handle, sig) == 0) { |
| 6034 | err = GetLastError(); |
| 6035 | result = PyErr_SetFromWindowsErr(err); |
| 6036 | } else { |
| 6037 | Py_INCREF(Py_None); |
| 6038 | result = Py_None; |
| 6039 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6040 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6041 | CloseHandle(handle); |
| 6042 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6043 | } |
| 6044 | #endif /* MS_WINDOWS */ |
| 6045 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6046 | #ifdef HAVE_PLOCK |
| 6047 | |
| 6048 | #ifdef HAVE_SYS_LOCK_H |
| 6049 | #include <sys/lock.h> |
| 6050 | #endif |
| 6051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6052 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6053 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6054 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6055 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6056 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6057 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6058 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6059 | int op; |
| 6060 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6061 | return NULL; |
| 6062 | if (plock(op) == -1) |
| 6063 | return posix_error(); |
| 6064 | Py_INCREF(Py_None); |
| 6065 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6066 | } |
| 6067 | #endif |
| 6068 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6069 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6070 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6071 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6072 | Set the current process's user id."); |
| 6073 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6074 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6075 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6076 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6077 | long uid_arg; |
| 6078 | uid_t uid; |
| 6079 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 6080 | return NULL; |
| 6081 | uid = uid_arg; |
| 6082 | if (uid != uid_arg) { |
| 6083 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6084 | return NULL; |
| 6085 | } |
| 6086 | if (setuid(uid) < 0) |
| 6087 | return posix_error(); |
| 6088 | Py_INCREF(Py_None); |
| 6089 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6090 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6091 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6092 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6093 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6094 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6095 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6096 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6097 | Set the current process's effective user id."); |
| 6098 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6099 | static PyObject * |
| 6100 | posix_seteuid (PyObject *self, PyObject *args) |
| 6101 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6102 | long euid_arg; |
| 6103 | uid_t euid; |
| 6104 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 6105 | return NULL; |
| 6106 | euid = euid_arg; |
| 6107 | if (euid != euid_arg) { |
| 6108 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6109 | return NULL; |
| 6110 | } |
| 6111 | if (seteuid(euid) < 0) { |
| 6112 | return posix_error(); |
| 6113 | } else { |
| 6114 | Py_INCREF(Py_None); |
| 6115 | return Py_None; |
| 6116 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6117 | } |
| 6118 | #endif /* HAVE_SETEUID */ |
| 6119 | |
| 6120 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6121 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6122 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6123 | Set the current process's effective group id."); |
| 6124 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6125 | static PyObject * |
| 6126 | posix_setegid (PyObject *self, PyObject *args) |
| 6127 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6128 | long egid_arg; |
| 6129 | gid_t egid; |
| 6130 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 6131 | return NULL; |
| 6132 | egid = egid_arg; |
| 6133 | if (egid != egid_arg) { |
| 6134 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6135 | return NULL; |
| 6136 | } |
| 6137 | if (setegid(egid) < 0) { |
| 6138 | return posix_error(); |
| 6139 | } else { |
| 6140 | Py_INCREF(Py_None); |
| 6141 | return Py_None; |
| 6142 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6143 | } |
| 6144 | #endif /* HAVE_SETEGID */ |
| 6145 | |
| 6146 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6147 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6148 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6149 | Set the current process's real and effective user ids."); |
| 6150 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6151 | static PyObject * |
| 6152 | posix_setreuid (PyObject *self, PyObject *args) |
| 6153 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6154 | long ruid_arg, euid_arg; |
| 6155 | uid_t ruid, euid; |
| 6156 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 6157 | return NULL; |
| 6158 | if (ruid_arg == -1) |
| 6159 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 6160 | else |
| 6161 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 6162 | if (euid_arg == -1) |
| 6163 | euid = (uid_t)-1; |
| 6164 | else |
| 6165 | euid = euid_arg; |
| 6166 | if ((euid_arg != -1 && euid != euid_arg) || |
| 6167 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 6168 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6169 | return NULL; |
| 6170 | } |
| 6171 | if (setreuid(ruid, euid) < 0) { |
| 6172 | return posix_error(); |
| 6173 | } else { |
| 6174 | Py_INCREF(Py_None); |
| 6175 | return Py_None; |
| 6176 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6177 | } |
| 6178 | #endif /* HAVE_SETREUID */ |
| 6179 | |
| 6180 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6181 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6182 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6183 | Set the current process's real and effective group ids."); |
| 6184 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6185 | static PyObject * |
| 6186 | posix_setregid (PyObject *self, PyObject *args) |
| 6187 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6188 | long rgid_arg, egid_arg; |
| 6189 | gid_t rgid, egid; |
| 6190 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 6191 | return NULL; |
| 6192 | if (rgid_arg == -1) |
| 6193 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 6194 | else |
| 6195 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6196 | if (egid_arg == -1) |
| 6197 | egid = (gid_t)-1; |
| 6198 | else |
| 6199 | egid = egid_arg; |
| 6200 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6201 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6202 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6203 | return NULL; |
| 6204 | } |
| 6205 | if (setregid(rgid, egid) < 0) { |
| 6206 | return posix_error(); |
| 6207 | } else { |
| 6208 | Py_INCREF(Py_None); |
| 6209 | return Py_None; |
| 6210 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6211 | } |
| 6212 | #endif /* HAVE_SETREGID */ |
| 6213 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6214 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6215 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6216 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6217 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6218 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6219 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6220 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6221 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6222 | long gid_arg; |
| 6223 | gid_t gid; |
| 6224 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6225 | return NULL; |
| 6226 | gid = gid_arg; |
| 6227 | if (gid != gid_arg) { |
| 6228 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6229 | return NULL; |
| 6230 | } |
| 6231 | if (setgid(gid) < 0) |
| 6232 | return posix_error(); |
| 6233 | Py_INCREF(Py_None); |
| 6234 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6235 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6236 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6237 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6238 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6239 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6240 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6241 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6242 | |
| 6243 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6244 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6245 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6246 | int i, len; |
| 6247 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6248 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6249 | if (!PySequence_Check(groups)) { |
| 6250 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6251 | return NULL; |
| 6252 | } |
| 6253 | len = PySequence_Size(groups); |
| 6254 | if (len > MAX_GROUPS) { |
| 6255 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6256 | return NULL; |
| 6257 | } |
| 6258 | for(i = 0; i < len; i++) { |
| 6259 | PyObject *elem; |
| 6260 | elem = PySequence_GetItem(groups, i); |
| 6261 | if (!elem) |
| 6262 | return NULL; |
| 6263 | if (!PyLong_Check(elem)) { |
| 6264 | PyErr_SetString(PyExc_TypeError, |
| 6265 | "groups must be integers"); |
| 6266 | Py_DECREF(elem); |
| 6267 | return NULL; |
| 6268 | } else { |
| 6269 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6270 | if (PyErr_Occurred()) { |
| 6271 | PyErr_SetString(PyExc_TypeError, |
| 6272 | "group id too big"); |
| 6273 | Py_DECREF(elem); |
| 6274 | return NULL; |
| 6275 | } |
| 6276 | grouplist[i] = x; |
| 6277 | /* read back the value to see if it fitted in gid_t */ |
| 6278 | if (grouplist[i] != x) { |
| 6279 | PyErr_SetString(PyExc_TypeError, |
| 6280 | "group id too big"); |
| 6281 | Py_DECREF(elem); |
| 6282 | return NULL; |
| 6283 | } |
| 6284 | } |
| 6285 | Py_DECREF(elem); |
| 6286 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6287 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6288 | if (setgroups(len, grouplist) < 0) |
| 6289 | return posix_error(); |
| 6290 | Py_INCREF(Py_None); |
| 6291 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6292 | } |
| 6293 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6294 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6295 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6296 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6297 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6298 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6299 | PyObject *result; |
| 6300 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6301 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6302 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6303 | if (pid == -1) |
| 6304 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6305 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6306 | if (struct_rusage == NULL) { |
| 6307 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6308 | if (m == NULL) |
| 6309 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6310 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6311 | Py_DECREF(m); |
| 6312 | if (struct_rusage == NULL) |
| 6313 | return NULL; |
| 6314 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6316 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6317 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6318 | if (!result) |
| 6319 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6320 | |
| 6321 | #ifndef doubletime |
| 6322 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6323 | #endif |
| 6324 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6325 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6326 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6327 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6328 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6329 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6330 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6331 | SET_INT(result, 2, ru->ru_maxrss); |
| 6332 | SET_INT(result, 3, ru->ru_ixrss); |
| 6333 | SET_INT(result, 4, ru->ru_idrss); |
| 6334 | SET_INT(result, 5, ru->ru_isrss); |
| 6335 | SET_INT(result, 6, ru->ru_minflt); |
| 6336 | SET_INT(result, 7, ru->ru_majflt); |
| 6337 | SET_INT(result, 8, ru->ru_nswap); |
| 6338 | SET_INT(result, 9, ru->ru_inblock); |
| 6339 | SET_INT(result, 10, ru->ru_oublock); |
| 6340 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6341 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6342 | SET_INT(result, 13, ru->ru_nsignals); |
| 6343 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6344 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6345 | #undef SET_INT |
| 6346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6347 | if (PyErr_Occurred()) { |
| 6348 | Py_DECREF(result); |
| 6349 | return NULL; |
| 6350 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6351 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6352 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6353 | } |
| 6354 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6355 | |
| 6356 | #ifdef HAVE_WAIT3 |
| 6357 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6358 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6359 | Wait for completion of a child process."); |
| 6360 | |
| 6361 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6362 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6363 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6364 | pid_t pid; |
| 6365 | int options; |
| 6366 | struct rusage ru; |
| 6367 | WAIT_TYPE status; |
| 6368 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6369 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6370 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6371 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6372 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6373 | Py_BEGIN_ALLOW_THREADS |
| 6374 | pid = wait3(&status, options, &ru); |
| 6375 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6376 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6377 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6378 | } |
| 6379 | #endif /* HAVE_WAIT3 */ |
| 6380 | |
| 6381 | #ifdef HAVE_WAIT4 |
| 6382 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6383 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6384 | Wait for completion of a given child process."); |
| 6385 | |
| 6386 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6387 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6388 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6389 | pid_t pid; |
| 6390 | int options; |
| 6391 | struct rusage ru; |
| 6392 | WAIT_TYPE status; |
| 6393 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6394 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6395 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6396 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6397 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6398 | Py_BEGIN_ALLOW_THREADS |
| 6399 | pid = wait4(pid, &status, options, &ru); |
| 6400 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6401 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6402 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6403 | } |
| 6404 | #endif /* HAVE_WAIT4 */ |
| 6405 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6406 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6407 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6408 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6409 | Wait for the completion of one or more child processes.\n\n\ |
| 6410 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6411 | id specifies the pid to wait on.\n\ |
| 6412 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6413 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6414 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6415 | no children in a waitable state."); |
| 6416 | |
| 6417 | static PyObject * |
| 6418 | posix_waitid(PyObject *self, PyObject *args) |
| 6419 | { |
| 6420 | PyObject *result; |
| 6421 | idtype_t idtype; |
| 6422 | id_t id; |
| 6423 | int options, res; |
| 6424 | siginfo_t si; |
| 6425 | si.si_pid = 0; |
| 6426 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6427 | return NULL; |
| 6428 | Py_BEGIN_ALLOW_THREADS |
| 6429 | res = waitid(idtype, id, &si, options); |
| 6430 | Py_END_ALLOW_THREADS |
| 6431 | if (res == -1) |
| 6432 | return posix_error(); |
| 6433 | |
| 6434 | if (si.si_pid == 0) |
| 6435 | Py_RETURN_NONE; |
| 6436 | |
| 6437 | result = PyStructSequence_New(&WaitidResultType); |
| 6438 | if (!result) |
| 6439 | return NULL; |
| 6440 | |
| 6441 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6442 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6443 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6444 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6445 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6446 | if (PyErr_Occurred()) { |
| 6447 | Py_DECREF(result); |
| 6448 | return NULL; |
| 6449 | } |
| 6450 | |
| 6451 | return result; |
| 6452 | } |
| 6453 | #endif |
| 6454 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6455 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6456 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6457 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6458 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6459 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6460 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6461 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6462 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6463 | pid_t pid; |
| 6464 | int options; |
| 6465 | WAIT_TYPE status; |
| 6466 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6467 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6468 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6469 | return NULL; |
| 6470 | Py_BEGIN_ALLOW_THREADS |
| 6471 | pid = waitpid(pid, &status, options); |
| 6472 | Py_END_ALLOW_THREADS |
| 6473 | if (pid == -1) |
| 6474 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6475 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6476 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6477 | } |
| 6478 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6479 | #elif defined(HAVE_CWAIT) |
| 6480 | |
| 6481 | /* 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] | 6482 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6483 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6484 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6485 | |
| 6486 | static PyObject * |
| 6487 | posix_waitpid(PyObject *self, PyObject *args) |
| 6488 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6489 | Py_intptr_t pid; |
| 6490 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6491 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6492 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6493 | return NULL; |
| 6494 | Py_BEGIN_ALLOW_THREADS |
| 6495 | pid = _cwait(&status, pid, options); |
| 6496 | Py_END_ALLOW_THREADS |
| 6497 | if (pid == -1) |
| 6498 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6499 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6500 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6501 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6502 | } |
| 6503 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6504 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6505 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6506 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6507 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6508 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6509 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6510 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6511 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6512 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6513 | pid_t pid; |
| 6514 | WAIT_TYPE status; |
| 6515 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6516 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6517 | Py_BEGIN_ALLOW_THREADS |
| 6518 | pid = wait(&status); |
| 6519 | Py_END_ALLOW_THREADS |
| 6520 | if (pid == -1) |
| 6521 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6522 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6523 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6524 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6525 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6526 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6527 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6528 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 6529 | PyDoc_STRVAR(readlink__doc__, |
| 6530 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 6531 | Return a string representing the path to which the symbolic link points.\n\ |
| 6532 | \n\ |
| 6533 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 6534 | and path should be relative; path will then be relative to that directory.\n\ |
| 6535 | dir_fd may not be implemented on your platform.\n\ |
| 6536 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6537 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6538 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6539 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6540 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6541 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6542 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6543 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6544 | path_t path; |
| 6545 | int dir_fd = DEFAULT_DIR_FD; |
| 6546 | char buffer[MAXPATHLEN]; |
| 6547 | ssize_t length; |
| 6548 | PyObject *return_value = NULL; |
| 6549 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6550 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6551 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6552 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6553 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 6554 | path_converter, &path, |
| 6555 | #ifdef HAVE_READLINKAT |
| 6556 | dir_fd_converter, &dir_fd |
| 6557 | #else |
| 6558 | dir_fd_unavailable, &dir_fd |
| 6559 | #endif |
| 6560 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6561 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6562 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6563 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6564 | #ifdef HAVE_READLINKAT |
| 6565 | if (dir_fd != DEFAULT_DIR_FD) |
| 6566 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6567 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6568 | #endif |
| 6569 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 6570 | Py_END_ALLOW_THREADS |
| 6571 | |
| 6572 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6573 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6574 | goto exit; |
| 6575 | } |
| 6576 | |
| 6577 | if (PyUnicode_Check(path.object)) |
| 6578 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 6579 | else |
| 6580 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 6581 | exit: |
| 6582 | path_cleanup(&path); |
| 6583 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6584 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6585 | |
| 6586 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6587 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6588 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6589 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6590 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6591 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6592 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 6593 | Create a symbolic link pointing to src named dst.\n\n\ |
| 6594 | target_is_directory is required on Windows if the target is to be\n\ |
| 6595 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 6596 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 6597 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 6598 | \n\ |
| 6599 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 6600 | and path should be relative; path will then be relative to that directory.\n\ |
| 6601 | dir_fd may not be implemented on your platform.\n\ |
| 6602 | If it is unavailable, using it will raise a NotImplementedError."); |
| 6603 | |
| 6604 | #if defined(MS_WINDOWS) |
| 6605 | |
| 6606 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6607 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 6608 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
| 6609 | static int |
| 6610 | check_CreateSymbolicLink() |
| 6611 | { |
| 6612 | HINSTANCE hKernel32; |
| 6613 | /* only recheck */ |
| 6614 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 6615 | return 1; |
| 6616 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 6617 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6618 | "CreateSymbolicLinkW"); |
| 6619 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 6620 | "CreateSymbolicLinkA"); |
| 6621 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 6622 | } |
| 6623 | |
| 6624 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6625 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6626 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6627 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6628 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6629 | path_t src; |
| 6630 | path_t dst; |
| 6631 | int dir_fd = DEFAULT_DIR_FD; |
| 6632 | int target_is_directory = 0; |
| 6633 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 6634 | "dir_fd", NULL}; |
| 6635 | PyObject *return_value; |
| 6636 | #ifdef MS_WINDOWS |
| 6637 | DWORD result; |
| 6638 | #else |
| 6639 | int result; |
| 6640 | #endif |
| 6641 | |
| 6642 | memset(&src, 0, sizeof(src)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6643 | src.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6644 | src.argument_name = "src"; |
| 6645 | memset(&dst, 0, sizeof(dst)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 6646 | dst.function_name = "symlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6647 | dst.argument_name = "dst"; |
| 6648 | |
| 6649 | #ifdef MS_WINDOWS |
| 6650 | if (!check_CreateSymbolicLink()) { |
| 6651 | PyErr_SetString(PyExc_NotImplementedError, |
| 6652 | "CreateSymbolicLink functions not found"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 6653 | return NULL; |
| 6654 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6655 | if (!win32_can_symlink) { |
| 6656 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 6657 | return NULL; |
| 6658 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6659 | #endif |
| 6660 | |
| 6661 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 6662 | keywords, |
| 6663 | path_converter, &src, |
| 6664 | path_converter, &dst, |
| 6665 | &target_is_directory, |
| 6666 | #ifdef HAVE_SYMLINKAT |
| 6667 | dir_fd_converter, &dir_fd |
| 6668 | #else |
| 6669 | dir_fd_unavailable, &dir_fd |
| 6670 | #endif |
| 6671 | )) |
| 6672 | return NULL; |
| 6673 | |
| 6674 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 6675 | PyErr_SetString(PyExc_ValueError, |
| 6676 | "symlink: src and dst must be the same type"); |
| 6677 | return_value = NULL; |
| 6678 | goto exit; |
| 6679 | } |
| 6680 | |
| 6681 | #ifdef MS_WINDOWS |
| 6682 | Py_BEGIN_ALLOW_THREADS |
| 6683 | if (dst.wide) |
| 6684 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 6685 | target_is_directory); |
| 6686 | else |
| 6687 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 6688 | target_is_directory); |
| 6689 | Py_END_ALLOW_THREADS |
| 6690 | |
| 6691 | if (!result) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 6692 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6693 | goto exit; |
| 6694 | } |
| 6695 | |
| 6696 | #else |
| 6697 | |
| 6698 | Py_BEGIN_ALLOW_THREADS |
| 6699 | #if HAVE_SYMLINKAT |
| 6700 | if (dir_fd != DEFAULT_DIR_FD) |
| 6701 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 6702 | else |
| 6703 | #endif |
| 6704 | result = symlink(src.narrow, dst.narrow); |
| 6705 | Py_END_ALLOW_THREADS |
| 6706 | |
| 6707 | if (result) { |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 6708 | return_value = path_error(&src); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6709 | goto exit; |
| 6710 | } |
| 6711 | #endif |
| 6712 | |
| 6713 | return_value = Py_None; |
| 6714 | Py_INCREF(Py_None); |
| 6715 | goto exit; /* silence "unused label" warning */ |
| 6716 | exit: |
| 6717 | path_cleanup(&src); |
| 6718 | path_cleanup(&dst); |
| 6719 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6720 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6721 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6722 | #endif /* HAVE_SYMLINK */ |
| 6723 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6724 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6725 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6726 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6727 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6728 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6729 | { |
| 6730 | wchar_t *path; |
| 6731 | DWORD n_bytes_returned; |
| 6732 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6733 | PyObject *po, *result; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 6734 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6735 | HANDLE reparse_point_handle; |
| 6736 | |
| 6737 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6738 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6739 | wchar_t *print_name; |
| 6740 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6741 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 6742 | |
| 6743 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 6744 | &po, |
| 6745 | dir_fd_unavailable, &dir_fd |
| 6746 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6747 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6748 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6749 | path = PyUnicode_AsUnicode(po); |
| 6750 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6751 | return NULL; |
| 6752 | |
| 6753 | /* First get a handle to the reparse point */ |
| 6754 | Py_BEGIN_ALLOW_THREADS |
| 6755 | reparse_point_handle = CreateFileW( |
| 6756 | path, |
| 6757 | 0, |
| 6758 | 0, |
| 6759 | 0, |
| 6760 | OPEN_EXISTING, |
| 6761 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6762 | 0); |
| 6763 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6764 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6765 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6766 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6767 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6768 | Py_BEGIN_ALLOW_THREADS |
| 6769 | /* New call DeviceIoControl to read the reparse point */ |
| 6770 | io_result = DeviceIoControl( |
| 6771 | reparse_point_handle, |
| 6772 | FSCTL_GET_REPARSE_POINT, |
| 6773 | 0, 0, /* in buffer */ |
| 6774 | target_buffer, sizeof(target_buffer), |
| 6775 | &n_bytes_returned, |
| 6776 | 0 /* we're not using OVERLAPPED_IO */ |
| 6777 | ); |
| 6778 | CloseHandle(reparse_point_handle); |
| 6779 | Py_END_ALLOW_THREADS |
| 6780 | |
| 6781 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6782 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6783 | |
| 6784 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6785 | { |
| 6786 | PyErr_SetString(PyExc_ValueError, |
| 6787 | "not a symbolic link"); |
| 6788 | return NULL; |
| 6789 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6790 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6791 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6792 | |
| 6793 | result = PyUnicode_FromWideChar(print_name, |
| 6794 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6795 | return result; |
| 6796 | } |
| 6797 | |
| 6798 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6799 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6800 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6801 | static PyStructSequence_Field times_result_fields[] = { |
| 6802 | {"user", "user time"}, |
| 6803 | {"system", "system time"}, |
| 6804 | {"children_user", "user time of children"}, |
| 6805 | {"children_system", "system time of children"}, |
| 6806 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 6807 | {NULL} |
| 6808 | }; |
| 6809 | |
| 6810 | PyDoc_STRVAR(times_result__doc__, |
| 6811 | "times_result: Result from os.times().\n\n\ |
| 6812 | This object may be accessed either as a tuple of\n\ |
| 6813 | (user, system, children_user, children_system, elapsed),\n\ |
| 6814 | or via the attributes user, system, children_user, children_system,\n\ |
| 6815 | and elapsed.\n\ |
| 6816 | \n\ |
| 6817 | See os.times for more information."); |
| 6818 | |
| 6819 | static PyStructSequence_Desc times_result_desc = { |
| 6820 | "times_result", /* name */ |
| 6821 | times_result__doc__, /* doc */ |
| 6822 | times_result_fields, |
| 6823 | 5 |
| 6824 | }; |
| 6825 | |
| 6826 | static PyTypeObject TimesResultType; |
| 6827 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 6828 | #ifdef MS_WINDOWS |
| 6829 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 6830 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6831 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 6832 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6833 | |
| 6834 | static PyObject * |
| 6835 | build_times_result(double user, double system, |
| 6836 | double children_user, double children_system, |
| 6837 | double elapsed) |
| 6838 | { |
| 6839 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 6840 | if (value == NULL) |
| 6841 | return NULL; |
| 6842 | |
| 6843 | #define SET(i, field) \ |
| 6844 | { \ |
| 6845 | PyObject *o = PyFloat_FromDouble(field); \ |
| 6846 | if (!o) { \ |
| 6847 | Py_DECREF(value); \ |
| 6848 | return NULL; \ |
| 6849 | } \ |
| 6850 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 6851 | } \ |
| 6852 | |
| 6853 | SET(0, user); |
| 6854 | SET(1, system); |
| 6855 | SET(2, children_user); |
| 6856 | SET(3, children_system); |
| 6857 | SET(4, elapsed); |
| 6858 | |
| 6859 | #undef SET |
| 6860 | |
| 6861 | return value; |
| 6862 | } |
| 6863 | |
| 6864 | PyDoc_STRVAR(posix_times__doc__, |
| 6865 | "times() -> times_result\n\n\ |
| 6866 | Return an object containing floating point numbers indicating process\n\ |
| 6867 | times. The object behaves like a named tuple with these fields:\n\ |
| 6868 | (utime, stime, cutime, cstime, elapsed_time)"); |
| 6869 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 6870 | #if defined(MS_WINDOWS) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6871 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6872 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6873 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6874 | FILETIME create, exit, kernel, user; |
| 6875 | HANDLE hProc; |
| 6876 | hProc = GetCurrentProcess(); |
| 6877 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6878 | /* The fields of a FILETIME structure are the hi and lo part |
| 6879 | of a 64-bit value expressed in 100 nanosecond units. |
| 6880 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6881 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6882 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 6883 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6884 | (double)(user.dwHighDateTime*429.4967296 + |
| 6885 | user.dwLowDateTime*1e-7), |
| 6886 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6887 | kernel.dwLowDateTime*1e-7), |
| 6888 | (double)0, |
| 6889 | (double)0, |
| 6890 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6891 | } |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 6892 | #else /* Not Windows */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 6893 | #define NEED_TICKS_PER_SECOND |
| 6894 | static long ticks_per_second = -1; |
| 6895 | static PyObject * |
| 6896 | posix_times(PyObject *self, PyObject *noargs) |
| 6897 | { |
| 6898 | struct tms t; |
| 6899 | clock_t c; |
| 6900 | errno = 0; |
| 6901 | c = times(&t); |
| 6902 | if (c == (clock_t) -1) |
| 6903 | return posix_error(); |
| 6904 | return build_times_result( |
| 6905 | (double)t.tms_utime / ticks_per_second, |
| 6906 | (double)t.tms_stime / ticks_per_second, |
| 6907 | (double)t.tms_cutime / ticks_per_second, |
| 6908 | (double)t.tms_cstime / ticks_per_second, |
| 6909 | (double)c / ticks_per_second); |
| 6910 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6911 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6912 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 6913 | #endif /* HAVE_TIMES */ |
| 6914 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6915 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6916 | #ifdef HAVE_GETSID |
| 6917 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6918 | "getsid(pid) -> sid\n\n\ |
| 6919 | Call the system call getsid()."); |
| 6920 | |
| 6921 | static PyObject * |
| 6922 | posix_getsid(PyObject *self, PyObject *args) |
| 6923 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6924 | pid_t pid; |
| 6925 | int sid; |
| 6926 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 6927 | return NULL; |
| 6928 | sid = getsid(pid); |
| 6929 | if (sid < 0) |
| 6930 | return posix_error(); |
| 6931 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6932 | } |
| 6933 | #endif /* HAVE_GETSID */ |
| 6934 | |
| 6935 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6936 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6937 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6938 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6939 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6940 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6941 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6942 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6943 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6944 | if (setsid() < 0) |
| 6945 | return posix_error(); |
| 6946 | Py_INCREF(Py_None); |
| 6947 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6948 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6949 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6950 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6951 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6952 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6953 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6954 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6955 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6956 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6957 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6958 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6959 | pid_t pid; |
| 6960 | int pgrp; |
| 6961 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6962 | return NULL; |
| 6963 | if (setpgid(pid, pgrp) < 0) |
| 6964 | return posix_error(); |
| 6965 | Py_INCREF(Py_None); |
| 6966 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6967 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6968 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6969 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6970 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6971 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6972 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6973 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6974 | 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] | 6975 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6976 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6977 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6978 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6979 | int fd; |
| 6980 | pid_t pgid; |
| 6981 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6982 | return NULL; |
| 6983 | pgid = tcgetpgrp(fd); |
| 6984 | if (pgid < 0) |
| 6985 | return posix_error(); |
| 6986 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6987 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6988 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6989 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6990 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6991 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6992 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6993 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6994 | 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] | 6995 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6996 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6997 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6998 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6999 | int fd; |
| 7000 | pid_t pgid; |
| 7001 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7002 | return NULL; |
| 7003 | if (tcsetpgrp(fd, pgid) < 0) |
| 7004 | return posix_error(); |
| 7005 | Py_INCREF(Py_None); |
| 7006 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7007 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7008 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7009 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7010 | /* Functions acting on file descriptors */ |
| 7011 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7012 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7013 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7014 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7015 | \n\ |
| 7016 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7017 | and path should be relative; path will then be relative to that directory.\n\ |
| 7018 | dir_fd may not be implemented on your platform.\n\ |
| 7019 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7020 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7021 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7022 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7023 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7024 | path_t path; |
| 7025 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7026 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7027 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7028 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7029 | PyObject *return_value = NULL; |
| 7030 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7031 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7032 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7033 | path.function_name = "open"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7034 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7035 | path_converter, &path, |
| 7036 | &flags, &mode, |
| 7037 | #ifdef HAVE_OPENAT |
| 7038 | dir_fd_converter, &dir_fd |
| 7039 | #else |
| 7040 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7041 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7042 | )) |
| 7043 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7044 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7045 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7046 | #ifdef MS_WINDOWS |
| 7047 | if (path.wide) |
| 7048 | fd = _wopen(path.wide, flags, mode); |
| 7049 | else |
| 7050 | #endif |
| 7051 | #ifdef HAVE_OPENAT |
| 7052 | if (dir_fd != DEFAULT_DIR_FD) |
| 7053 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7054 | else |
| 7055 | #endif |
| 7056 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7057 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7058 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7059 | if (fd == -1) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7060 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7061 | goto exit; |
| 7062 | } |
| 7063 | |
| 7064 | return_value = PyLong_FromLong((long)fd); |
| 7065 | |
| 7066 | exit: |
| 7067 | path_cleanup(&path); |
| 7068 | return return_value; |
| 7069 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7070 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7071 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7072 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7073 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7074 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7075 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7076 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7077 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7078 | int fd, res; |
| 7079 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7080 | return NULL; |
| 7081 | if (!_PyVerify_fd(fd)) |
| 7082 | return posix_error(); |
| 7083 | Py_BEGIN_ALLOW_THREADS |
| 7084 | res = close(fd); |
| 7085 | Py_END_ALLOW_THREADS |
| 7086 | if (res < 0) |
| 7087 | return posix_error(); |
| 7088 | Py_INCREF(Py_None); |
| 7089 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7090 | } |
| 7091 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7092 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7093 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7094 | "closerange(fd_low, fd_high)\n\n\ |
| 7095 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7096 | |
| 7097 | static PyObject * |
| 7098 | posix_closerange(PyObject *self, PyObject *args) |
| 7099 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7100 | int fd_from, fd_to, i; |
| 7101 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7102 | return NULL; |
| 7103 | Py_BEGIN_ALLOW_THREADS |
| 7104 | for (i = fd_from; i < fd_to; i++) |
| 7105 | if (_PyVerify_fd(i)) |
| 7106 | close(i); |
| 7107 | Py_END_ALLOW_THREADS |
| 7108 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7109 | } |
| 7110 | |
| 7111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7112 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7113 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7114 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7115 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7116 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7117 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7118 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7119 | int fd; |
| 7120 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7121 | return NULL; |
| 7122 | if (!_PyVerify_fd(fd)) |
| 7123 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7124 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7125 | if (fd < 0) |
| 7126 | return posix_error(); |
| 7127 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7128 | } |
| 7129 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7130 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7131 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7132 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7133 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7134 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7135 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7136 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7137 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7138 | int fd, fd2, res; |
| 7139 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 7140 | return NULL; |
| 7141 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7142 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7143 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7144 | if (res < 0) |
| 7145 | return posix_error(); |
| 7146 | Py_INCREF(Py_None); |
| 7147 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7148 | } |
| 7149 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7150 | #ifdef HAVE_LOCKF |
| 7151 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7152 | "lockf(fd, cmd, len)\n\n\ |
| 7153 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7154 | fd is an open file descriptor.\n\ |
| 7155 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7156 | F_TEST.\n\ |
| 7157 | len specifies the section of the file to lock."); |
| 7158 | |
| 7159 | static PyObject * |
| 7160 | posix_lockf(PyObject *self, PyObject *args) |
| 7161 | { |
| 7162 | int fd, cmd, res; |
| 7163 | off_t len; |
| 7164 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7165 | &fd, &cmd, _parse_off_t, &len)) |
| 7166 | return NULL; |
| 7167 | |
| 7168 | Py_BEGIN_ALLOW_THREADS |
| 7169 | res = lockf(fd, cmd, len); |
| 7170 | Py_END_ALLOW_THREADS |
| 7171 | |
| 7172 | if (res < 0) |
| 7173 | return posix_error(); |
| 7174 | |
| 7175 | Py_RETURN_NONE; |
| 7176 | } |
| 7177 | #endif |
| 7178 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7179 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7180 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7181 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7182 | Set the current position of a file descriptor.\n\ |
| 7183 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7184 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7185 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7186 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7187 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7188 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7189 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7190 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7191 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7192 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7193 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7194 | PyObject *posobj; |
| 7195 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7196 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7197 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7198 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7199 | switch (how) { |
| 7200 | case 0: how = SEEK_SET; break; |
| 7201 | case 1: how = SEEK_CUR; break; |
| 7202 | case 2: how = SEEK_END; break; |
| 7203 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7204 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7205 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7206 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7207 | pos = PyLong_AsLong(posobj); |
| 7208 | #else |
| 7209 | pos = PyLong_AsLongLong(posobj); |
| 7210 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7211 | if (PyErr_Occurred()) |
| 7212 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7213 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7214 | if (!_PyVerify_fd(fd)) |
| 7215 | return posix_error(); |
| 7216 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7217 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7218 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7219 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7220 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7221 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7222 | Py_END_ALLOW_THREADS |
| 7223 | if (res < 0) |
| 7224 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7225 | |
| 7226 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7227 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7228 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7229 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7230 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7231 | } |
| 7232 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7233 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7234 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7235 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7236 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7237 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7238 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7239 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7240 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7241 | int fd, size; |
| 7242 | Py_ssize_t n; |
| 7243 | PyObject *buffer; |
| 7244 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7245 | return NULL; |
| 7246 | if (size < 0) { |
| 7247 | errno = EINVAL; |
| 7248 | return posix_error(); |
| 7249 | } |
| 7250 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7251 | if (buffer == NULL) |
| 7252 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7253 | if (!_PyVerify_fd(fd)) { |
| 7254 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7255 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7256 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7257 | Py_BEGIN_ALLOW_THREADS |
| 7258 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7259 | Py_END_ALLOW_THREADS |
| 7260 | if (n < 0) { |
| 7261 | Py_DECREF(buffer); |
| 7262 | return posix_error(); |
| 7263 | } |
| 7264 | if (n != size) |
| 7265 | _PyBytes_Resize(&buffer, n); |
| 7266 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7269 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7270 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7271 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7272 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7273 | { |
| 7274 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7275 | Py_ssize_t blen, total = 0; |
| 7276 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7277 | *iov = PyMem_New(struct iovec, cnt); |
| 7278 | if (*iov == NULL) { |
| 7279 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7280 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7281 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7282 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7283 | *buf = PyMem_New(Py_buffer, cnt); |
| 7284 | if (*buf == NULL) { |
| 7285 | PyMem_Del(*iov); |
| 7286 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7287 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7288 | } |
| 7289 | |
| 7290 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7291 | PyObject *item = PySequence_GetItem(seq, i); |
| 7292 | if (item == NULL) |
| 7293 | goto fail; |
| 7294 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7295 | Py_DECREF(item); |
| 7296 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7297 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7298 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7299 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7300 | blen = (*buf)[i].len; |
| 7301 | (*iov)[i].iov_len = blen; |
| 7302 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7303 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7304 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7305 | |
| 7306 | fail: |
| 7307 | PyMem_Del(*iov); |
| 7308 | for (j = 0; j < i; j++) { |
| 7309 | PyBuffer_Release(&(*buf)[j]); |
| 7310 | } |
| 7311 | PyMem_Del(*buf); |
| 7312 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7313 | } |
| 7314 | |
| 7315 | static void |
| 7316 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7317 | { |
| 7318 | int i; |
| 7319 | PyMem_Del(iov); |
| 7320 | for (i = 0; i < cnt; i++) { |
| 7321 | PyBuffer_Release(&buf[i]); |
| 7322 | } |
| 7323 | PyMem_Del(buf); |
| 7324 | } |
| 7325 | #endif |
| 7326 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7327 | #ifdef HAVE_READV |
| 7328 | PyDoc_STRVAR(posix_readv__doc__, |
| 7329 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7330 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7331 | is an arbitrary sequence of writable buffers.\n\ |
| 7332 | Returns the total number of bytes read."); |
| 7333 | |
| 7334 | static PyObject * |
| 7335 | posix_readv(PyObject *self, PyObject *args) |
| 7336 | { |
| 7337 | int fd, cnt; |
| 7338 | Py_ssize_t n; |
| 7339 | PyObject *seq; |
| 7340 | struct iovec *iov; |
| 7341 | Py_buffer *buf; |
| 7342 | |
| 7343 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7344 | return NULL; |
| 7345 | if (!PySequence_Check(seq)) { |
| 7346 | PyErr_SetString(PyExc_TypeError, |
| 7347 | "readv() arg 2 must be a sequence"); |
| 7348 | return NULL; |
| 7349 | } |
| 7350 | cnt = PySequence_Size(seq); |
| 7351 | |
| 7352 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7353 | return NULL; |
| 7354 | |
| 7355 | Py_BEGIN_ALLOW_THREADS |
| 7356 | n = readv(fd, iov, cnt); |
| 7357 | Py_END_ALLOW_THREADS |
| 7358 | |
| 7359 | iov_cleanup(iov, buf, cnt); |
| 7360 | return PyLong_FromSsize_t(n); |
| 7361 | } |
| 7362 | #endif |
| 7363 | |
| 7364 | #ifdef HAVE_PREAD |
| 7365 | PyDoc_STRVAR(posix_pread__doc__, |
| 7366 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7367 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7368 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7369 | |
| 7370 | static PyObject * |
| 7371 | posix_pread(PyObject *self, PyObject *args) |
| 7372 | { |
| 7373 | int fd, size; |
| 7374 | off_t offset; |
| 7375 | Py_ssize_t n; |
| 7376 | PyObject *buffer; |
| 7377 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7378 | return NULL; |
| 7379 | |
| 7380 | if (size < 0) { |
| 7381 | errno = EINVAL; |
| 7382 | return posix_error(); |
| 7383 | } |
| 7384 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7385 | if (buffer == NULL) |
| 7386 | return NULL; |
| 7387 | if (!_PyVerify_fd(fd)) { |
| 7388 | Py_DECREF(buffer); |
| 7389 | return posix_error(); |
| 7390 | } |
| 7391 | Py_BEGIN_ALLOW_THREADS |
| 7392 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7393 | Py_END_ALLOW_THREADS |
| 7394 | if (n < 0) { |
| 7395 | Py_DECREF(buffer); |
| 7396 | return posix_error(); |
| 7397 | } |
| 7398 | if (n != size) |
| 7399 | _PyBytes_Resize(&buffer, n); |
| 7400 | return buffer; |
| 7401 | } |
| 7402 | #endif |
| 7403 | |
| 7404 | PyDoc_STRVAR(posix_write__doc__, |
| 7405 | "write(fd, string) -> byteswritten\n\n\ |
| 7406 | Write a string to a file descriptor."); |
| 7407 | |
| 7408 | static PyObject * |
| 7409 | posix_write(PyObject *self, PyObject *args) |
| 7410 | { |
| 7411 | Py_buffer pbuf; |
| 7412 | int fd; |
| 7413 | Py_ssize_t size, len; |
| 7414 | |
| 7415 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7416 | return NULL; |
| 7417 | if (!_PyVerify_fd(fd)) { |
| 7418 | PyBuffer_Release(&pbuf); |
| 7419 | return posix_error(); |
| 7420 | } |
| 7421 | len = pbuf.len; |
| 7422 | Py_BEGIN_ALLOW_THREADS |
| 7423 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7424 | if (len > INT_MAX) |
| 7425 | len = INT_MAX; |
| 7426 | size = write(fd, pbuf.buf, (int)len); |
| 7427 | #else |
| 7428 | size = write(fd, pbuf.buf, len); |
| 7429 | #endif |
| 7430 | Py_END_ALLOW_THREADS |
| 7431 | PyBuffer_Release(&pbuf); |
| 7432 | if (size < 0) |
| 7433 | return posix_error(); |
| 7434 | return PyLong_FromSsize_t(size); |
| 7435 | } |
| 7436 | |
| 7437 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7438 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7439 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7440 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7441 | -> byteswritten\n\ |
| 7442 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7443 | |
| 7444 | static PyObject * |
| 7445 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7446 | { |
| 7447 | int in, out; |
| 7448 | Py_ssize_t ret; |
| 7449 | off_t offset; |
| 7450 | |
| 7451 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7452 | #ifndef __APPLE__ |
| 7453 | Py_ssize_t len; |
| 7454 | #endif |
| 7455 | PyObject *headers = NULL, *trailers = NULL; |
| 7456 | Py_buffer *hbuf, *tbuf; |
| 7457 | off_t sbytes; |
| 7458 | struct sf_hdtr sf; |
| 7459 | int flags = 0; |
| 7460 | sf.headers = NULL; |
| 7461 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7462 | static char *keywords[] = {"out", "in", |
| 7463 | "offset", "count", |
| 7464 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7465 | |
| 7466 | #ifdef __APPLE__ |
| 7467 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7468 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7469 | #else |
| 7470 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7471 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7472 | #endif |
| 7473 | &headers, &trailers, &flags)) |
| 7474 | return NULL; |
| 7475 | if (headers != NULL) { |
| 7476 | if (!PySequence_Check(headers)) { |
| 7477 | PyErr_SetString(PyExc_TypeError, |
| 7478 | "sendfile() headers must be a sequence or None"); |
| 7479 | return NULL; |
| 7480 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7481 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7482 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7483 | if (sf.hdr_cnt > 0 && |
| 7484 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7485 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7486 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7487 | #ifdef __APPLE__ |
| 7488 | sbytes += i; |
| 7489 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7490 | } |
| 7491 | } |
| 7492 | if (trailers != NULL) { |
| 7493 | if (!PySequence_Check(trailers)) { |
| 7494 | PyErr_SetString(PyExc_TypeError, |
| 7495 | "sendfile() trailers must be a sequence or None"); |
| 7496 | return NULL; |
| 7497 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7498 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7499 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7500 | if (sf.trl_cnt > 0 && |
| 7501 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7502 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7503 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7504 | #ifdef __APPLE__ |
| 7505 | sbytes += i; |
| 7506 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7507 | } |
| 7508 | } |
| 7509 | |
| 7510 | Py_BEGIN_ALLOW_THREADS |
| 7511 | #ifdef __APPLE__ |
| 7512 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7513 | #else |
| 7514 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7515 | #endif |
| 7516 | Py_END_ALLOW_THREADS |
| 7517 | |
| 7518 | if (sf.headers != NULL) |
| 7519 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7520 | if (sf.trailers != NULL) |
| 7521 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7522 | |
| 7523 | if (ret < 0) { |
| 7524 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7525 | if (sbytes != 0) { |
| 7526 | // some data has been sent |
| 7527 | goto done; |
| 7528 | } |
| 7529 | else { |
| 7530 | // no data has been sent; upper application is supposed |
| 7531 | // to retry on EAGAIN or EBUSY |
| 7532 | return posix_error(); |
| 7533 | } |
| 7534 | } |
| 7535 | return posix_error(); |
| 7536 | } |
| 7537 | goto done; |
| 7538 | |
| 7539 | done: |
| 7540 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7541 | return Py_BuildValue("l", sbytes); |
| 7542 | #else |
| 7543 | return Py_BuildValue("L", sbytes); |
| 7544 | #endif |
| 7545 | |
| 7546 | #else |
| 7547 | Py_ssize_t count; |
| 7548 | PyObject *offobj; |
| 7549 | static char *keywords[] = {"out", "in", |
| 7550 | "offset", "count", NULL}; |
| 7551 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7552 | keywords, &out, &in, &offobj, &count)) |
| 7553 | return NULL; |
| 7554 | #ifdef linux |
| 7555 | if (offobj == Py_None) { |
| 7556 | Py_BEGIN_ALLOW_THREADS |
| 7557 | ret = sendfile(out, in, NULL, count); |
| 7558 | Py_END_ALLOW_THREADS |
| 7559 | if (ret < 0) |
| 7560 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7561 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7562 | } |
| 7563 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7564 | if (!_parse_off_t(offobj, &offset)) |
| 7565 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7566 | Py_BEGIN_ALLOW_THREADS |
| 7567 | ret = sendfile(out, in, &offset, count); |
| 7568 | Py_END_ALLOW_THREADS |
| 7569 | if (ret < 0) |
| 7570 | return posix_error(); |
| 7571 | return Py_BuildValue("n", ret); |
| 7572 | #endif |
| 7573 | } |
| 7574 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7576 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7577 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7578 | Like stat(), but for an open file descriptor.\n\ |
| 7579 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7581 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7582 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7583 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7584 | int fd; |
| 7585 | STRUCT_STAT st; |
| 7586 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7587 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7588 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7589 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7590 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7591 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7592 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7593 | Py_BEGIN_ALLOW_THREADS |
| 7594 | res = FSTAT(fd, &st); |
| 7595 | Py_END_ALLOW_THREADS |
| 7596 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7597 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 7598 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7599 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7600 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7601 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7602 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7603 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7604 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7605 | } |
| 7606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7607 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7608 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7609 | 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] | 7610 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7611 | |
| 7612 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7613 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7614 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7615 | int fd; |
| 7616 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7617 | return NULL; |
| 7618 | if (!_PyVerify_fd(fd)) |
| 7619 | return PyBool_FromLong(0); |
| 7620 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7621 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7622 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7623 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7624 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7625 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7626 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7627 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7628 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7629 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7630 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7631 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7632 | int fds[2]; |
| 7633 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7634 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7635 | if (res != 0) |
| 7636 | return posix_error(); |
| 7637 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7638 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7639 | HANDLE read, write; |
| 7640 | int read_fd, write_fd; |
| 7641 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7642 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7643 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 7644 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7645 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7646 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7647 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7648 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7649 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7650 | #endif /* HAVE_PIPE */ |
| 7651 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7652 | #ifdef HAVE_PIPE2 |
| 7653 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7654 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7655 | Create a pipe with flags set atomically.\n\ |
| 7656 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7657 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7658 | "); |
| 7659 | |
| 7660 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7661 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7662 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7663 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7664 | int fds[2]; |
| 7665 | int res; |
| 7666 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7667 | flags = PyLong_AsLong(arg); |
| 7668 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7669 | return NULL; |
| 7670 | |
| 7671 | res = pipe2(fds, flags); |
| 7672 | if (res != 0) |
| 7673 | return posix_error(); |
| 7674 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7675 | } |
| 7676 | #endif /* HAVE_PIPE2 */ |
| 7677 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7678 | #ifdef HAVE_WRITEV |
| 7679 | PyDoc_STRVAR(posix_writev__doc__, |
| 7680 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7681 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7682 | arbitrary sequence of buffers.\n\ |
| 7683 | Returns the total bytes written."); |
| 7684 | |
| 7685 | static PyObject * |
| 7686 | posix_writev(PyObject *self, PyObject *args) |
| 7687 | { |
| 7688 | int fd, cnt; |
| 7689 | Py_ssize_t res; |
| 7690 | PyObject *seq; |
| 7691 | struct iovec *iov; |
| 7692 | Py_buffer *buf; |
| 7693 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7694 | return NULL; |
| 7695 | if (!PySequence_Check(seq)) { |
| 7696 | PyErr_SetString(PyExc_TypeError, |
| 7697 | "writev() arg 2 must be a sequence"); |
| 7698 | return NULL; |
| 7699 | } |
| 7700 | cnt = PySequence_Size(seq); |
| 7701 | |
| 7702 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7703 | return NULL; |
| 7704 | } |
| 7705 | |
| 7706 | Py_BEGIN_ALLOW_THREADS |
| 7707 | res = writev(fd, iov, cnt); |
| 7708 | Py_END_ALLOW_THREADS |
| 7709 | |
| 7710 | iov_cleanup(iov, buf, cnt); |
| 7711 | return PyLong_FromSsize_t(res); |
| 7712 | } |
| 7713 | #endif |
| 7714 | |
| 7715 | #ifdef HAVE_PWRITE |
| 7716 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7717 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7718 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7719 | offset unchanged."); |
| 7720 | |
| 7721 | static PyObject * |
| 7722 | posix_pwrite(PyObject *self, PyObject *args) |
| 7723 | { |
| 7724 | Py_buffer pbuf; |
| 7725 | int fd; |
| 7726 | off_t offset; |
| 7727 | Py_ssize_t size; |
| 7728 | |
| 7729 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7730 | return NULL; |
| 7731 | |
| 7732 | if (!_PyVerify_fd(fd)) { |
| 7733 | PyBuffer_Release(&pbuf); |
| 7734 | return posix_error(); |
| 7735 | } |
| 7736 | Py_BEGIN_ALLOW_THREADS |
| 7737 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7738 | Py_END_ALLOW_THREADS |
| 7739 | PyBuffer_Release(&pbuf); |
| 7740 | if (size < 0) |
| 7741 | return posix_error(); |
| 7742 | return PyLong_FromSsize_t(size); |
| 7743 | } |
| 7744 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7745 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7746 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7747 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7748 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 7749 | Create a FIFO (a POSIX named pipe).\n\ |
| 7750 | \n\ |
| 7751 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7752 | and path should be relative; path will then be relative to that directory.\n\ |
| 7753 | dir_fd may not be implemented on your platform.\n\ |
| 7754 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7755 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7756 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7757 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7758 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7759 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7760 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7761 | int dir_fd = DEFAULT_DIR_FD; |
| 7762 | int result; |
| 7763 | PyObject *return_value = NULL; |
| 7764 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 7765 | |
| 7766 | memset(&path, 0, sizeof(path)); |
| 7767 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 7768 | path_converter, &path, |
| 7769 | &mode, |
| 7770 | #ifdef HAVE_MKFIFOAT |
| 7771 | dir_fd_converter, &dir_fd |
| 7772 | #else |
| 7773 | dir_fd_unavailable, &dir_fd |
| 7774 | #endif |
| 7775 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7776 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7777 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7778 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7779 | #ifdef HAVE_MKFIFOAT |
| 7780 | if (dir_fd != DEFAULT_DIR_FD) |
| 7781 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 7782 | else |
| 7783 | #endif |
| 7784 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7785 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7786 | |
| 7787 | if (result < 0) { |
| 7788 | return_value = posix_error(); |
| 7789 | goto exit; |
| 7790 | } |
| 7791 | |
| 7792 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7793 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7794 | |
| 7795 | exit: |
| 7796 | path_cleanup(&path); |
| 7797 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7798 | } |
| 7799 | #endif |
| 7800 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7801 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7802 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7803 | "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] | 7804 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7805 | named filename. mode specifies both the permissions to use and the\n\ |
| 7806 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7807 | 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] | 7808 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7809 | os.makedev()), otherwise it is ignored.\n\ |
| 7810 | \n\ |
| 7811 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7812 | and path should be relative; path will then be relative to that directory.\n\ |
| 7813 | dir_fd may not be implemented on your platform.\n\ |
| 7814 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7815 | |
| 7816 | |
| 7817 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7818 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7819 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7820 | path_t path; |
| 7821 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7822 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7823 | int dir_fd = DEFAULT_DIR_FD; |
| 7824 | int result; |
| 7825 | PyObject *return_value = NULL; |
| 7826 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 7827 | |
| 7828 | memset(&path, 0, sizeof(path)); |
| 7829 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 7830 | path_converter, &path, |
| 7831 | &mode, &device, |
| 7832 | #ifdef HAVE_MKNODAT |
| 7833 | dir_fd_converter, &dir_fd |
| 7834 | #else |
| 7835 | dir_fd_unavailable, &dir_fd |
| 7836 | #endif |
| 7837 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7838 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7839 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7840 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7841 | #ifdef HAVE_MKNODAT |
| 7842 | if (dir_fd != DEFAULT_DIR_FD) |
| 7843 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 7844 | else |
| 7845 | #endif |
| 7846 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7847 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7848 | |
| 7849 | if (result < 0) { |
| 7850 | return_value = posix_error(); |
| 7851 | goto exit; |
| 7852 | } |
| 7853 | |
| 7854 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7855 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 7856 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7857 | exit: |
| 7858 | path_cleanup(&path); |
| 7859 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7860 | } |
| 7861 | #endif |
| 7862 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7863 | #ifdef HAVE_DEVICE_MACROS |
| 7864 | PyDoc_STRVAR(posix_major__doc__, |
| 7865 | "major(device) -> major number\n\ |
| 7866 | Extracts a device major number from a raw device number."); |
| 7867 | |
| 7868 | static PyObject * |
| 7869 | posix_major(PyObject *self, PyObject *args) |
| 7870 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7871 | int device; |
| 7872 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7873 | return NULL; |
| 7874 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7875 | } |
| 7876 | |
| 7877 | PyDoc_STRVAR(posix_minor__doc__, |
| 7878 | "minor(device) -> minor number\n\ |
| 7879 | Extracts a device minor number from a raw device number."); |
| 7880 | |
| 7881 | static PyObject * |
| 7882 | posix_minor(PyObject *self, PyObject *args) |
| 7883 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7884 | int device; |
| 7885 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7886 | return NULL; |
| 7887 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7888 | } |
| 7889 | |
| 7890 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7891 | "makedev(major, minor) -> device number\n\ |
| 7892 | Composes a raw device number from the major and minor device numbers."); |
| 7893 | |
| 7894 | static PyObject * |
| 7895 | posix_makedev(PyObject *self, PyObject *args) |
| 7896 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7897 | int major, minor; |
| 7898 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7899 | return NULL; |
| 7900 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7901 | } |
| 7902 | #endif /* device macros */ |
| 7903 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7904 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7905 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7906 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7907 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7908 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7909 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7910 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7911 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7912 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7913 | int fd; |
| 7914 | off_t length; |
| 7915 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7916 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7917 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7918 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7919 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7920 | Py_BEGIN_ALLOW_THREADS |
| 7921 | res = ftruncate(fd, length); |
| 7922 | Py_END_ALLOW_THREADS |
| 7923 | if (res < 0) |
| 7924 | return posix_error(); |
| 7925 | Py_INCREF(Py_None); |
| 7926 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7927 | } |
| 7928 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7929 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7930 | #ifdef HAVE_TRUNCATE |
| 7931 | PyDoc_STRVAR(posix_truncate__doc__, |
| 7932 | "truncate(path, length)\n\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 7933 | Truncate the file given by path to length bytes.\n\ |
| 7934 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 7935 | If this functionality is unavailable, using it raises an exception."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7936 | |
| 7937 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 7938 | posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7939 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 7940 | path_t path; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7941 | off_t length; |
| 7942 | int res; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 7943 | PyObject *result = NULL; |
| 7944 | static char *keywords[] = {"path", "length", NULL}; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7945 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 7946 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7947 | path.function_name = "truncate"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 7948 | #ifdef HAVE_FTRUNCATE |
| 7949 | path.allow_fd = 1; |
| 7950 | #endif |
| 7951 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords, |
| 7952 | path_converter, &path, |
| 7953 | _parse_off_t, &length)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7954 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7955 | |
| 7956 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 7957 | #ifdef HAVE_FTRUNCATE |
| 7958 | if (path.fd != -1) |
| 7959 | res = ftruncate(path.fd, length); |
| 7960 | else |
| 7961 | #endif |
| 7962 | res = truncate(path.narrow, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7963 | Py_END_ALLOW_THREADS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7964 | if (res < 0) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 7965 | result = path_error(&path); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 7966 | else { |
| 7967 | Py_INCREF(Py_None); |
| 7968 | result = Py_None; |
| 7969 | } |
| 7970 | path_cleanup(&path); |
| 7971 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7972 | } |
| 7973 | #endif |
| 7974 | |
| 7975 | #ifdef HAVE_POSIX_FALLOCATE |
| 7976 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 7977 | "posix_fallocate(fd, offset, len)\n\n\ |
| 7978 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 7979 | starting from offset and continuing for len bytes."); |
| 7980 | |
| 7981 | static PyObject * |
| 7982 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 7983 | { |
| 7984 | off_t len, offset; |
| 7985 | int res, fd; |
| 7986 | |
| 7987 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 7988 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 7989 | return NULL; |
| 7990 | |
| 7991 | Py_BEGIN_ALLOW_THREADS |
| 7992 | res = posix_fallocate(fd, offset, len); |
| 7993 | Py_END_ALLOW_THREADS |
| 7994 | if (res != 0) { |
| 7995 | errno = res; |
| 7996 | return posix_error(); |
| 7997 | } |
| 7998 | Py_RETURN_NONE; |
| 7999 | } |
| 8000 | #endif |
| 8001 | |
| 8002 | #ifdef HAVE_POSIX_FADVISE |
| 8003 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8004 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8005 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8006 | the kernel to make optimizations.\n\ |
| 8007 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8008 | offset and continuing for len bytes.\n\ |
| 8009 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8010 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8011 | POSIX_FADV_DONTNEED."); |
| 8012 | |
| 8013 | static PyObject * |
| 8014 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8015 | { |
| 8016 | off_t len, offset; |
| 8017 | int res, fd, advice; |
| 8018 | |
| 8019 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8020 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8021 | return NULL; |
| 8022 | |
| 8023 | Py_BEGIN_ALLOW_THREADS |
| 8024 | res = posix_fadvise(fd, offset, len, advice); |
| 8025 | Py_END_ALLOW_THREADS |
| 8026 | if (res != 0) { |
| 8027 | errno = res; |
| 8028 | return posix_error(); |
| 8029 | } |
| 8030 | Py_RETURN_NONE; |
| 8031 | } |
| 8032 | #endif |
| 8033 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8034 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8035 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8036 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8037 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8038 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8039 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8040 | * get re-set with another call for the same key. */ |
| 8041 | static PyObject *posix_putenv_garbage; |
| 8042 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8043 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8044 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8045 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8046 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8047 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8048 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8049 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8051 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8052 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8053 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8054 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8055 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8056 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8057 | if (newstr == NULL) { |
| 8058 | PyErr_NoMemory(); |
| 8059 | goto error; |
| 8060 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8061 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8062 | PyErr_Format(PyExc_ValueError, |
| 8063 | "the environment variable is longer than %u characters", |
| 8064 | _MAX_ENV); |
| 8065 | goto error; |
| 8066 | } |
| 8067 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8068 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8069 | if (newenv == NULL) |
| 8070 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8071 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8072 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8073 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8074 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8075 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8076 | PyObject *os1, *os2; |
| 8077 | char *s1, *s2; |
| 8078 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8079 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8080 | if (!PyArg_ParseTuple(args, |
| 8081 | "O&O&:putenv", |
| 8082 | PyUnicode_FSConverter, &os1, |
| 8083 | PyUnicode_FSConverter, &os2)) |
| 8084 | return NULL; |
| 8085 | s1 = PyBytes_AsString(os1); |
| 8086 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8087 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8088 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8089 | if (newstr == NULL) { |
| 8090 | PyErr_NoMemory(); |
| 8091 | goto error; |
| 8092 | } |
| 8093 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8094 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8095 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8096 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8097 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8098 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8099 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8100 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8101 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8102 | * this will cause previous value to be collected. This has to |
| 8103 | * happen after the real putenv() call because the old value |
| 8104 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8105 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8106 | /* really not much we can do; just leak */ |
| 8107 | PyErr_Clear(); |
| 8108 | } |
| 8109 | else { |
| 8110 | Py_DECREF(newstr); |
| 8111 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8112 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8113 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8114 | Py_DECREF(os1); |
| 8115 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8116 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8117 | Py_RETURN_NONE; |
| 8118 | |
| 8119 | error: |
| 8120 | #ifndef MS_WINDOWS |
| 8121 | Py_DECREF(os1); |
| 8122 | Py_DECREF(os2); |
| 8123 | #endif |
| 8124 | Py_XDECREF(newstr); |
| 8125 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8126 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8127 | #endif /* putenv */ |
| 8128 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8129 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8130 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8131 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8132 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8133 | |
| 8134 | static PyObject * |
| 8135 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8136 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8137 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8138 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8139 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8140 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8141 | |
| 8142 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8143 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8144 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8145 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8146 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8147 | #ifdef HAVE_BROKEN_UNSETENV |
| 8148 | unsetenv(PyBytes_AS_STRING(name)); |
| 8149 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8150 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8151 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8152 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8153 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8154 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8155 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8156 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8157 | /* Remove the key from posix_putenv_garbage; |
| 8158 | * this will cause it to be collected. This has to |
| 8159 | * happen after the real unsetenv() call because the |
| 8160 | * old value was still accessible until then. |
| 8161 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8162 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8163 | /* really not much we can do; just leak */ |
| 8164 | PyErr_Clear(); |
| 8165 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8166 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8167 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8168 | } |
| 8169 | #endif /* unsetenv */ |
| 8170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8171 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8172 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8173 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8174 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8175 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8176 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8177 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8178 | int code; |
| 8179 | char *message; |
| 8180 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8181 | return NULL; |
| 8182 | message = strerror(code); |
| 8183 | if (message == NULL) { |
| 8184 | PyErr_SetString(PyExc_ValueError, |
| 8185 | "strerror() argument out of range"); |
| 8186 | return NULL; |
| 8187 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8188 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8189 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8190 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8191 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8192 | #ifdef HAVE_SYS_WAIT_H |
| 8193 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8194 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8195 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8196 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8197 | 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] | 8198 | |
| 8199 | static PyObject * |
| 8200 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 8201 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8202 | WAIT_TYPE status; |
| 8203 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8204 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8205 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 8206 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8207 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8208 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8209 | } |
| 8210 | #endif /* WCOREDUMP */ |
| 8211 | |
| 8212 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8213 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8214 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8215 | 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] | 8216 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8217 | |
| 8218 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8219 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8220 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8221 | WAIT_TYPE status; |
| 8222 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8223 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8224 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 8225 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8226 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8227 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8228 | } |
| 8229 | #endif /* WIFCONTINUED */ |
| 8230 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8231 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8232 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8233 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8234 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8235 | |
| 8236 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8237 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8238 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8239 | WAIT_TYPE status; |
| 8240 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8241 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8242 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 8243 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8244 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8245 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8246 | } |
| 8247 | #endif /* WIFSTOPPED */ |
| 8248 | |
| 8249 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8250 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8251 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8252 | 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] | 8253 | |
| 8254 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8255 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8256 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8257 | WAIT_TYPE status; |
| 8258 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8259 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8260 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 8261 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8262 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8263 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8264 | } |
| 8265 | #endif /* WIFSIGNALED */ |
| 8266 | |
| 8267 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8268 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8269 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8270 | 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] | 8271 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8272 | |
| 8273 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8274 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8275 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8276 | WAIT_TYPE status; |
| 8277 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8278 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8279 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 8280 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8281 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8282 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8283 | } |
| 8284 | #endif /* WIFEXITED */ |
| 8285 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8286 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8287 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8288 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8289 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8290 | |
| 8291 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8292 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8293 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8294 | WAIT_TYPE status; |
| 8295 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8296 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8297 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8298 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8299 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8300 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8301 | } |
| 8302 | #endif /* WEXITSTATUS */ |
| 8303 | |
| 8304 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8305 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8306 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8307 | 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] | 8308 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8309 | |
| 8310 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8311 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8312 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8313 | WAIT_TYPE status; |
| 8314 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8316 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8317 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8318 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8319 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8320 | } |
| 8321 | #endif /* WTERMSIG */ |
| 8322 | |
| 8323 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8324 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8325 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8326 | Return the signal that stopped the process that provided\n\ |
| 8327 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8328 | |
| 8329 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8330 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8331 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8332 | WAIT_TYPE status; |
| 8333 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8334 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8335 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8336 | return NULL; |
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 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8339 | } |
| 8340 | #endif /* WSTOPSIG */ |
| 8341 | |
| 8342 | #endif /* HAVE_SYS_WAIT_H */ |
| 8343 | |
| 8344 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8345 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8346 | #ifdef _SCO_DS |
| 8347 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8348 | needed definitions in sys/statvfs.h */ |
| 8349 | #define _SVID3 |
| 8350 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8351 | #include <sys/statvfs.h> |
| 8352 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8353 | static PyObject* |
| 8354 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8355 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8356 | if (v == NULL) |
| 8357 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8358 | |
| 8359 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8360 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8361 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8362 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8363 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8364 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8365 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8366 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8367 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8368 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8369 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8370 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8371 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8372 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8373 | PyStructSequence_SET_ITEM(v, 2, |
| 8374 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8375 | PyStructSequence_SET_ITEM(v, 3, |
| 8376 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8377 | PyStructSequence_SET_ITEM(v, 4, |
| 8378 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8379 | PyStructSequence_SET_ITEM(v, 5, |
| 8380 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8381 | PyStructSequence_SET_ITEM(v, 6, |
| 8382 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8383 | PyStructSequence_SET_ITEM(v, 7, |
| 8384 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8385 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8386 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8387 | #endif |
| 8388 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8389 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8390 | } |
| 8391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8392 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8393 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8394 | Perform an fstatvfs system call on the given fd.\n\ |
| 8395 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8396 | |
| 8397 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8398 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8399 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8400 | int fd, res; |
| 8401 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8402 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8403 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8404 | return NULL; |
| 8405 | Py_BEGIN_ALLOW_THREADS |
| 8406 | res = fstatvfs(fd, &st); |
| 8407 | Py_END_ALLOW_THREADS |
| 8408 | if (res != 0) |
| 8409 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8410 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8411 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8412 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8413 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8414 | |
| 8415 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8416 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8417 | #include <sys/statvfs.h> |
| 8418 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8419 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8420 | "statvfs(path)\n\n\ |
| 8421 | Perform a statvfs system call on the given path.\n\ |
| 8422 | \n\ |
| 8423 | path may always be specified as a string.\n\ |
| 8424 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8425 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8426 | |
| 8427 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8428 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8429 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8430 | static char *keywords[] = {"path", NULL}; |
| 8431 | path_t path; |
| 8432 | int result; |
| 8433 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8434 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8435 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8436 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8437 | path.function_name = "statvfs"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8438 | #ifdef HAVE_FSTATVFS |
| 8439 | path.allow_fd = 1; |
| 8440 | #endif |
| 8441 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 8442 | path_converter, &path |
| 8443 | )) |
| 8444 | return NULL; |
| 8445 | |
| 8446 | Py_BEGIN_ALLOW_THREADS |
| 8447 | #ifdef HAVE_FSTATVFS |
| 8448 | if (path.fd != -1) { |
| 8449 | #ifdef __APPLE__ |
| 8450 | /* handle weak-linking on Mac OS X 10.3 */ |
| 8451 | if (fstatvfs == NULL) { |
| 8452 | fd_specified("statvfs", path.fd); |
| 8453 | goto exit; |
| 8454 | } |
| 8455 | #endif |
| 8456 | result = fstatvfs(path.fd, &st); |
| 8457 | } |
| 8458 | else |
| 8459 | #endif |
| 8460 | result = statvfs(path.narrow, &st); |
| 8461 | Py_END_ALLOW_THREADS |
| 8462 | |
| 8463 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8464 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8465 | goto exit; |
| 8466 | } |
| 8467 | |
| 8468 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 8469 | |
| 8470 | exit: |
| 8471 | path_cleanup(&path); |
| 8472 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8473 | } |
| 8474 | #endif /* HAVE_STATVFS */ |
| 8475 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8476 | #ifdef MS_WINDOWS |
| 8477 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8478 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8479 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8480 | |
| 8481 | static PyObject * |
| 8482 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8483 | { |
| 8484 | BOOL retval; |
| 8485 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8486 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8487 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8488 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8489 | return NULL; |
| 8490 | |
| 8491 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8492 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8493 | Py_END_ALLOW_THREADS |
| 8494 | if (retval == 0) |
| 8495 | return PyErr_SetFromWindowsErr(0); |
| 8496 | |
| 8497 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8498 | } |
| 8499 | #endif |
| 8500 | |
| 8501 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8502 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8503 | * It maps strings representing configuration variable names to |
| 8504 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8505 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8506 | * rarely-used constants. There are three separate tables that use |
| 8507 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8508 | * |
| 8509 | * This code is always included, even if none of the interfaces that |
| 8510 | * need it are included. The #if hackery needed to avoid it would be |
| 8511 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8512 | */ |
| 8513 | struct constdef { |
| 8514 | char *name; |
| 8515 | long value; |
| 8516 | }; |
| 8517 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8518 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8519 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8520 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8521 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8522 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8523 | *valuep = PyLong_AS_LONG(arg); |
| 8524 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8525 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8526 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8527 | /* look up the value in the table using a binary search */ |
| 8528 | size_t lo = 0; |
| 8529 | size_t mid; |
| 8530 | size_t hi = tablesize; |
| 8531 | int cmp; |
| 8532 | const char *confname; |
| 8533 | if (!PyUnicode_Check(arg)) { |
| 8534 | PyErr_SetString(PyExc_TypeError, |
| 8535 | "configuration names must be strings or integers"); |
| 8536 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8537 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8538 | confname = _PyUnicode_AsString(arg); |
| 8539 | if (confname == NULL) |
| 8540 | return 0; |
| 8541 | while (lo < hi) { |
| 8542 | mid = (lo + hi) / 2; |
| 8543 | cmp = strcmp(confname, table[mid].name); |
| 8544 | if (cmp < 0) |
| 8545 | hi = mid; |
| 8546 | else if (cmp > 0) |
| 8547 | lo = mid + 1; |
| 8548 | else { |
| 8549 | *valuep = table[mid].value; |
| 8550 | return 1; |
| 8551 | } |
| 8552 | } |
| 8553 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8554 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8555 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8556 | } |
| 8557 | |
| 8558 | |
| 8559 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8560 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8561 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8562 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8563 | #endif |
| 8564 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8565 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8566 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8567 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8568 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8569 | #endif |
| 8570 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8571 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8572 | #endif |
| 8573 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8574 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8575 | #endif |
| 8576 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8577 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8578 | #endif |
| 8579 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8580 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8581 | #endif |
| 8582 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8583 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8584 | #endif |
| 8585 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8586 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8587 | #endif |
| 8588 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8589 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8590 | #endif |
| 8591 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8592 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8593 | #endif |
| 8594 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8595 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8596 | #endif |
| 8597 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8598 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8599 | #endif |
| 8600 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8601 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8602 | #endif |
| 8603 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8605 | #endif |
| 8606 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8608 | #endif |
| 8609 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8610 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8611 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8612 | #ifdef _PC_ACL_ENABLED |
| 8613 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8614 | #endif |
| 8615 | #ifdef _PC_MIN_HOLE_SIZE |
| 8616 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8617 | #endif |
| 8618 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8619 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8620 | #endif |
| 8621 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8622 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8623 | #endif |
| 8624 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8625 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8626 | #endif |
| 8627 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8628 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8629 | #endif |
| 8630 | #ifdef _PC_REC_XFER_ALIGN |
| 8631 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8632 | #endif |
| 8633 | #ifdef _PC_SYMLINK_MAX |
| 8634 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8635 | #endif |
| 8636 | #ifdef _PC_XATTR_ENABLED |
| 8637 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8638 | #endif |
| 8639 | #ifdef _PC_XATTR_EXISTS |
| 8640 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8641 | #endif |
| 8642 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8643 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8644 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8645 | }; |
| 8646 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8647 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8648 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8649 | { |
| 8650 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8651 | sizeof(posix_constants_pathconf) |
| 8652 | / sizeof(struct constdef)); |
| 8653 | } |
| 8654 | #endif |
| 8655 | |
| 8656 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8657 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8658 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8659 | 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] | 8660 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8661 | |
| 8662 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8663 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8664 | { |
| 8665 | PyObject *result = NULL; |
| 8666 | int name, fd; |
| 8667 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8668 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8669 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8670 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8671 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8672 | errno = 0; |
| 8673 | limit = fpathconf(fd, name); |
| 8674 | if (limit == -1 && errno != 0) |
| 8675 | posix_error(); |
| 8676 | else |
| 8677 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8678 | } |
| 8679 | return result; |
| 8680 | } |
| 8681 | #endif |
| 8682 | |
| 8683 | |
| 8684 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8685 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8686 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8687 | Return the configuration limit name for the file or directory path.\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8688 | If there is no limit, return -1.\n\ |
| 8689 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8690 | If this functionality is unavailable, using it raises an exception."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8691 | |
| 8692 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8693 | posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8694 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8695 | path_t path; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8696 | PyObject *result = NULL; |
| 8697 | int name; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8698 | static char *keywords[] = {"path", "name", NULL}; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8699 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8700 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8701 | path.function_name = "pathconf"; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8702 | #ifdef HAVE_FPATHCONF |
| 8703 | path.allow_fd = 1; |
| 8704 | #endif |
| 8705 | if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords, |
| 8706 | path_converter, &path, |
| 8707 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8708 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8709 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8710 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8711 | #ifdef HAVE_FPATHCONF |
| 8712 | if (path.fd != -1) |
| 8713 | limit = fpathconf(path.fd, name); |
| 8714 | else |
| 8715 | #endif |
| 8716 | limit = pathconf(path.narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8717 | if (limit == -1 && errno != 0) { |
| 8718 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8719 | /* could be a path or name problem */ |
| 8720 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8721 | else |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 8722 | result = path_error(&path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8723 | } |
| 8724 | else |
| 8725 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8726 | } |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8727 | path_cleanup(&path); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8728 | return result; |
| 8729 | } |
| 8730 | #endif |
| 8731 | |
| 8732 | #ifdef HAVE_CONFSTR |
| 8733 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8734 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8735 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8736 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8737 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8738 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8739 | #endif |
| 8740 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8741 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8742 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8743 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8744 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8745 | #endif |
| 8746 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8747 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8748 | #endif |
| 8749 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8750 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8751 | #endif |
| 8752 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8753 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8754 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8755 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8756 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8757 | #endif |
| 8758 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8759 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8760 | #endif |
| 8761 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8762 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8763 | #endif |
| 8764 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8765 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8766 | #endif |
| 8767 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8768 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8769 | #endif |
| 8770 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8771 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8772 | #endif |
| 8773 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8774 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8775 | #endif |
| 8776 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8777 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8778 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8779 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8780 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8781 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8782 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8783 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8784 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8785 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8786 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8787 | #endif |
| 8788 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8789 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8790 | #endif |
| 8791 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8792 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8793 | #endif |
| 8794 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8795 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8796 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8797 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8798 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8799 | #endif |
| 8800 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8801 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8802 | #endif |
| 8803 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8804 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8805 | #endif |
| 8806 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8807 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8808 | #endif |
| 8809 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8810 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8811 | #endif |
| 8812 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8813 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8814 | #endif |
| 8815 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8816 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8817 | #endif |
| 8818 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8819 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8820 | #endif |
| 8821 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8822 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8823 | #endif |
| 8824 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8825 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8826 | #endif |
| 8827 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8828 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8829 | #endif |
| 8830 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8831 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8832 | #endif |
| 8833 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8834 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8835 | #endif |
| 8836 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8837 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8838 | #endif |
| 8839 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8840 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8841 | #endif |
| 8842 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8843 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8844 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8845 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8846 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8847 | #endif |
| 8848 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8849 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8850 | #endif |
| 8851 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8852 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8853 | #endif |
| 8854 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8855 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8856 | #endif |
| 8857 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8858 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8859 | #endif |
| 8860 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8861 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8862 | #endif |
| 8863 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8864 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8865 | #endif |
| 8866 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8867 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8868 | #endif |
| 8869 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8870 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8871 | #endif |
| 8872 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8873 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8874 | #endif |
| 8875 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8876 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8877 | #endif |
| 8878 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8879 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8880 | #endif |
| 8881 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8882 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8883 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8884 | }; |
| 8885 | |
| 8886 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8887 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8888 | { |
| 8889 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8890 | sizeof(posix_constants_confstr) |
| 8891 | / sizeof(struct constdef)); |
| 8892 | } |
| 8893 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8894 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8895 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8896 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8897 | |
| 8898 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8899 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8900 | { |
| 8901 | PyObject *result = NULL; |
| 8902 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8903 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8904 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8905 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8906 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8907 | return NULL; |
| 8908 | |
| 8909 | errno = 0; |
| 8910 | len = confstr(name, buffer, sizeof(buffer)); |
| 8911 | if (len == 0) { |
| 8912 | if (errno) { |
| 8913 | posix_error(); |
| 8914 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8915 | } |
| 8916 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8917 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8918 | } |
| 8919 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8920 | |
| 8921 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8922 | char *buf = PyMem_Malloc(len); |
| 8923 | if (buf == NULL) |
| 8924 | return PyErr_NoMemory(); |
| 8925 | confstr(name, buf, len); |
| 8926 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8927 | PyMem_Free(buf); |
| 8928 | } |
| 8929 | else |
| 8930 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8931 | return result; |
| 8932 | } |
| 8933 | #endif |
| 8934 | |
| 8935 | |
| 8936 | #ifdef HAVE_SYSCONF |
| 8937 | static struct constdef posix_constants_sysconf[] = { |
| 8938 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8939 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8940 | #endif |
| 8941 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8942 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8943 | #endif |
| 8944 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8945 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8946 | #endif |
| 8947 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8948 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8949 | #endif |
| 8950 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8951 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8952 | #endif |
| 8953 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8954 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8955 | #endif |
| 8956 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8957 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8958 | #endif |
| 8959 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8960 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8961 | #endif |
| 8962 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8963 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8964 | #endif |
| 8965 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8966 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8967 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8968 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8969 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8970 | #endif |
| 8971 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8972 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8973 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8974 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8975 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8976 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8977 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8978 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8979 | #endif |
| 8980 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8981 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8982 | #endif |
| 8983 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8984 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8985 | #endif |
| 8986 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8987 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8988 | #endif |
| 8989 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8990 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8991 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8992 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8993 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8994 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8995 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8996 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8997 | #endif |
| 8998 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8999 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9000 | #endif |
| 9001 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9002 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9003 | #endif |
| 9004 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9005 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9006 | #endif |
| 9007 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9008 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9009 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9010 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9011 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9012 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9013 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9014 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9015 | #endif |
| 9016 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9017 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9018 | #endif |
| 9019 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9020 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9021 | #endif |
| 9022 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9023 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9024 | #endif |
| 9025 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9026 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9027 | #endif |
| 9028 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9029 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9030 | #endif |
| 9031 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9032 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9033 | #endif |
| 9034 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9035 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9036 | #endif |
| 9037 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9038 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9039 | #endif |
| 9040 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9041 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9042 | #endif |
| 9043 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9044 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9045 | #endif |
| 9046 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9047 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9048 | #endif |
| 9049 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9050 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9051 | #endif |
| 9052 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9053 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9054 | #endif |
| 9055 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9056 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9057 | #endif |
| 9058 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9059 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9060 | #endif |
| 9061 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9062 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9063 | #endif |
| 9064 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9065 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9066 | #endif |
| 9067 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9068 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9069 | #endif |
| 9070 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9071 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9072 | #endif |
| 9073 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9074 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9075 | #endif |
| 9076 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9078 | #endif |
| 9079 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9080 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9081 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9082 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9084 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9085 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9086 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9087 | #endif |
| 9088 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9089 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9090 | #endif |
| 9091 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9092 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9093 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9094 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9095 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9096 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9097 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9098 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9099 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9100 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9101 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9102 | #endif |
| 9103 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9104 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9105 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9106 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9107 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9108 | #endif |
| 9109 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9110 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9111 | #endif |
| 9112 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9113 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9114 | #endif |
| 9115 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9116 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9117 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9118 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9119 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9120 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9121 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9123 | #endif |
| 9124 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9125 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9126 | #endif |
| 9127 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9128 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9129 | #endif |
| 9130 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9131 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9132 | #endif |
| 9133 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9134 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9135 | #endif |
| 9136 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9137 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9138 | #endif |
| 9139 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9140 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9141 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9142 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9143 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9144 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9145 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9146 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9147 | #endif |
| 9148 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9149 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9150 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9151 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9152 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9153 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9154 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9155 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9156 | #endif |
| 9157 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9158 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9159 | #endif |
| 9160 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9161 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9162 | #endif |
| 9163 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9164 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9165 | #endif |
| 9166 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9167 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9168 | #endif |
| 9169 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9170 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9171 | #endif |
| 9172 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9173 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9174 | #endif |
| 9175 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9176 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9177 | #endif |
| 9178 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9179 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9180 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9181 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9182 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9183 | #endif |
| 9184 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9185 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9186 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9187 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9188 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9189 | #endif |
| 9190 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9191 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9192 | #endif |
| 9193 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9194 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9195 | #endif |
| 9196 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9197 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9198 | #endif |
| 9199 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9200 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9201 | #endif |
| 9202 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9203 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9204 | #endif |
| 9205 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9206 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9207 | #endif |
| 9208 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9209 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9210 | #endif |
| 9211 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9212 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9213 | #endif |
| 9214 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9215 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9216 | #endif |
| 9217 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9218 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9219 | #endif |
| 9220 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9221 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9222 | #endif |
| 9223 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9224 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9225 | #endif |
| 9226 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9227 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9228 | #endif |
| 9229 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9230 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9231 | #endif |
| 9232 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9233 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9234 | #endif |
| 9235 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9236 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9237 | #endif |
| 9238 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9239 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9240 | #endif |
| 9241 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9242 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9243 | #endif |
| 9244 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9245 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9246 | #endif |
| 9247 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9248 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9249 | #endif |
| 9250 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9251 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9252 | #endif |
| 9253 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9254 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9255 | #endif |
| 9256 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9258 | #endif |
| 9259 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9260 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9261 | #endif |
| 9262 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9263 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9264 | #endif |
| 9265 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9266 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9267 | #endif |
| 9268 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9269 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9270 | #endif |
| 9271 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9272 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9273 | #endif |
| 9274 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9275 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9276 | #endif |
| 9277 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9279 | #endif |
| 9280 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9281 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9282 | #endif |
| 9283 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9284 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9285 | #endif |
| 9286 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9287 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9288 | #endif |
| 9289 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9290 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9291 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9292 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9293 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9294 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9295 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9296 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9297 | #endif |
| 9298 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9299 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9300 | #endif |
| 9301 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9302 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9303 | #endif |
| 9304 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9305 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9306 | #endif |
| 9307 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9308 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9309 | #endif |
| 9310 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9311 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9312 | #endif |
| 9313 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9314 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9315 | #endif |
| 9316 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9317 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9318 | #endif |
| 9319 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9320 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9321 | #endif |
| 9322 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9323 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9324 | #endif |
| 9325 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9326 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9327 | #endif |
| 9328 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9329 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9330 | #endif |
| 9331 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9332 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9333 | #endif |
| 9334 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9335 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9336 | #endif |
| 9337 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9338 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9339 | #endif |
| 9340 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9341 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9342 | #endif |
| 9343 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9344 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9345 | #endif |
| 9346 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9347 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9348 | #endif |
| 9349 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9350 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9351 | #endif |
| 9352 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9353 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9354 | #endif |
| 9355 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9356 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9357 | #endif |
| 9358 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9359 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9360 | #endif |
| 9361 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9362 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9363 | #endif |
| 9364 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9365 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9366 | #endif |
| 9367 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9368 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9369 | #endif |
| 9370 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9371 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9372 | #endif |
| 9373 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9375 | #endif |
| 9376 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9377 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9378 | #endif |
| 9379 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9380 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9381 | #endif |
| 9382 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9383 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9384 | #endif |
| 9385 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9386 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9387 | #endif |
| 9388 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9389 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9390 | #endif |
| 9391 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9392 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9393 | #endif |
| 9394 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9395 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9396 | #endif |
| 9397 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9398 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9399 | #endif |
| 9400 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9401 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9402 | #endif |
| 9403 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9404 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9405 | #endif |
| 9406 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9407 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9408 | #endif |
| 9409 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9410 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9411 | #endif |
| 9412 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9413 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9414 | #endif |
| 9415 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9416 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9417 | #endif |
| 9418 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9419 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9420 | #endif |
| 9421 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9422 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9423 | #endif |
| 9424 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9425 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9426 | #endif |
| 9427 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9428 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9429 | #endif |
| 9430 | }; |
| 9431 | |
| 9432 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9433 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9434 | { |
| 9435 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9436 | sizeof(posix_constants_sysconf) |
| 9437 | / sizeof(struct constdef)); |
| 9438 | } |
| 9439 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9440 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9441 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9442 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9443 | |
| 9444 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9445 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9446 | { |
| 9447 | PyObject *result = NULL; |
| 9448 | int name; |
| 9449 | |
| 9450 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9451 | int value; |
| 9452 | |
| 9453 | errno = 0; |
| 9454 | value = sysconf(name); |
| 9455 | if (value == -1 && errno != 0) |
| 9456 | posix_error(); |
| 9457 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9458 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9459 | } |
| 9460 | return result; |
| 9461 | } |
| 9462 | #endif |
| 9463 | |
| 9464 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9465 | /* This code is used to ensure that the tables of configuration value names |
| 9466 | * are in sorted order as required by conv_confname(), and also to build the |
| 9467 | * the exported dictionaries that are used to publish information about the |
| 9468 | * names available on the host platform. |
| 9469 | * |
| 9470 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9471 | * when used, even for platforms we're not able to test on. It also makes |
| 9472 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9473 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9474 | |
| 9475 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9476 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9477 | { |
| 9478 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9479 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9480 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9481 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9482 | |
| 9483 | return strcmp(c1->name, c2->name); |
| 9484 | } |
| 9485 | |
| 9486 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9487 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9488 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9489 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9490 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9491 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9492 | |
| 9493 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9494 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9495 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9496 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9497 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9498 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9499 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9500 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9501 | Py_XDECREF(o); |
| 9502 | Py_DECREF(d); |
| 9503 | return -1; |
| 9504 | } |
| 9505 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9506 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9507 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9508 | } |
| 9509 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9510 | /* Return -1 on failure, 0 on success. */ |
| 9511 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9512 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9513 | { |
| 9514 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9515 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9516 | sizeof(posix_constants_pathconf) |
| 9517 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9518 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9519 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9520 | #endif |
| 9521 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9522 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9523 | sizeof(posix_constants_confstr) |
| 9524 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9525 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9526 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9527 | #endif |
| 9528 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9529 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9530 | sizeof(posix_constants_sysconf) |
| 9531 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9532 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9533 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9534 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9535 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9536 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9537 | |
| 9538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9539 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9540 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9541 | 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] | 9542 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9543 | |
| 9544 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9545 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9546 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9547 | abort(); |
| 9548 | /*NOTREACHED*/ |
| 9549 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9550 | return NULL; |
| 9551 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9552 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9553 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9554 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9555 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9556 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9557 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9558 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9559 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9560 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9561 | application (if any) its extension is associated.\n\ |
| 9562 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9563 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9564 | \n\ |
| 9565 | startfile returns as soon as the associated application is launched.\n\ |
| 9566 | There is no option to wait for the application to close, and no way\n\ |
| 9567 | to retrieve the application's exit status.\n\ |
| 9568 | \n\ |
| 9569 | The filepath is relative to the current directory. If you want to use\n\ |
| 9570 | 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] | 9571 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9572 | |
| 9573 | static PyObject * |
| 9574 | win32_startfile(PyObject *self, PyObject *args) |
| 9575 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9576 | PyObject *ofilepath; |
| 9577 | char *filepath; |
| 9578 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9579 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9580 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9581 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9582 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9583 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9584 | &unipath, &operation)) { |
| 9585 | PyErr_Clear(); |
| 9586 | goto normal; |
| 9587 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9588 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9589 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9590 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9591 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9592 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9593 | PyErr_Clear(); |
| 9594 | operation = NULL; |
| 9595 | goto normal; |
| 9596 | } |
| 9597 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9598 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9599 | wpath = PyUnicode_AsUnicode(unipath); |
| 9600 | if (wpath == NULL) |
| 9601 | goto normal; |
| 9602 | if (uoperation) { |
| 9603 | woperation = PyUnicode_AsUnicode(uoperation); |
| 9604 | if (woperation == NULL) |
| 9605 | goto normal; |
| 9606 | } |
| 9607 | else |
| 9608 | woperation = NULL; |
| 9609 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9610 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9611 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 9612 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9613 | Py_END_ALLOW_THREADS |
| 9614 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9615 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9616 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9617 | win32_error_object("startfile", unipath); |
| 9618 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9619 | } |
| 9620 | Py_INCREF(Py_None); |
| 9621 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9622 | |
| 9623 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9624 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9625 | PyUnicode_FSConverter, &ofilepath, |
| 9626 | &operation)) |
| 9627 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 9628 | if (win32_warn_bytes_api()) { |
| 9629 | Py_DECREF(ofilepath); |
| 9630 | return NULL; |
| 9631 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9632 | filepath = PyBytes_AsString(ofilepath); |
| 9633 | Py_BEGIN_ALLOW_THREADS |
| 9634 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9635 | NULL, NULL, SW_SHOWNORMAL); |
| 9636 | Py_END_ALLOW_THREADS |
| 9637 | if (rc <= (HINSTANCE)32) { |
| 9638 | PyObject *errval = win32_error("startfile", filepath); |
| 9639 | Py_DECREF(ofilepath); |
| 9640 | return errval; |
| 9641 | } |
| 9642 | Py_DECREF(ofilepath); |
| 9643 | Py_INCREF(Py_None); |
| 9644 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9645 | } |
| 9646 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9647 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9648 | #ifdef HAVE_GETLOADAVG |
| 9649 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9650 | "getloadavg() -> (float, float, float)\n\n\ |
| 9651 | Return the number of processes in the system run queue averaged over\n\ |
| 9652 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9653 | was unobtainable"); |
| 9654 | |
| 9655 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9656 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9657 | { |
| 9658 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9659 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9660 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9661 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9662 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9663 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9664 | } |
| 9665 | #endif |
| 9666 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9667 | PyDoc_STRVAR(device_encoding__doc__, |
| 9668 | "device_encoding(fd) -> str\n\n\ |
| 9669 | Return a string describing the encoding of the device\n\ |
| 9670 | if the output is a terminal; else return None."); |
| 9671 | |
| 9672 | static PyObject * |
| 9673 | device_encoding(PyObject *self, PyObject *args) |
| 9674 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9675 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9676 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9677 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9678 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9679 | |
| 9680 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9681 | } |
| 9682 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9683 | #ifdef HAVE_SETRESUID |
| 9684 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9685 | "setresuid(ruid, euid, suid)\n\n\ |
| 9686 | Set the current process's real, effective, and saved user ids."); |
| 9687 | |
| 9688 | static PyObject* |
| 9689 | posix_setresuid (PyObject *self, PyObject *args) |
| 9690 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9691 | /* We assume uid_t is no larger than a long. */ |
| 9692 | long ruid, euid, suid; |
| 9693 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9694 | return NULL; |
| 9695 | if (setresuid(ruid, euid, suid) < 0) |
| 9696 | return posix_error(); |
| 9697 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9698 | } |
| 9699 | #endif |
| 9700 | |
| 9701 | #ifdef HAVE_SETRESGID |
| 9702 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9703 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9704 | Set the current process's real, effective, and saved group ids."); |
| 9705 | |
| 9706 | static PyObject* |
| 9707 | posix_setresgid (PyObject *self, PyObject *args) |
| 9708 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9709 | /* We assume uid_t is no larger than a long. */ |
| 9710 | long rgid, egid, sgid; |
| 9711 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9712 | return NULL; |
| 9713 | if (setresgid(rgid, egid, sgid) < 0) |
| 9714 | return posix_error(); |
| 9715 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9716 | } |
| 9717 | #endif |
| 9718 | |
| 9719 | #ifdef HAVE_GETRESUID |
| 9720 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9721 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9722 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9723 | |
| 9724 | static PyObject* |
| 9725 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9726 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9727 | uid_t ruid, euid, suid; |
| 9728 | long l_ruid, l_euid, l_suid; |
| 9729 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9730 | return posix_error(); |
| 9731 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9732 | l_ruid = ruid; |
| 9733 | l_euid = euid; |
| 9734 | l_suid = suid; |
| 9735 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9736 | } |
| 9737 | #endif |
| 9738 | |
| 9739 | #ifdef HAVE_GETRESGID |
| 9740 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9741 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9742 | 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] | 9743 | |
| 9744 | static PyObject* |
| 9745 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9746 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9747 | uid_t rgid, egid, sgid; |
| 9748 | long l_rgid, l_egid, l_sgid; |
| 9749 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9750 | return posix_error(); |
| 9751 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9752 | l_rgid = rgid; |
| 9753 | l_egid = egid; |
| 9754 | l_sgid = sgid; |
| 9755 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9756 | } |
| 9757 | #endif |
| 9758 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 9759 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9760 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9761 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9762 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 9763 | Return the value of extended attribute attribute on path.\n\ |
| 9764 | \n\ |
| 9765 | path may be either a string or an open file descriptor.\n\ |
| 9766 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 9767 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 9768 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9769 | |
| 9770 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9771 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9772 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9773 | path_t path; |
| 9774 | path_t attribute; |
| 9775 | int follow_symlinks = 1; |
| 9776 | PyObject *buffer = NULL; |
| 9777 | int i; |
| 9778 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9779 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9780 | memset(&path, 0, sizeof(path)); |
| 9781 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9782 | path.function_name = "getxattr"; |
| 9783 | attribute.function_name = "getxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9784 | path.allow_fd = 1; |
| 9785 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 9786 | path_converter, &path, |
| 9787 | path_converter, &attribute, |
| 9788 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9789 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9790 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9791 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 9792 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9793 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9794 | for (i = 0; ; i++) { |
| 9795 | void *ptr; |
| 9796 | ssize_t result; |
| 9797 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 9798 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 9799 | if (!buffer_size) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9800 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9801 | goto exit; |
| 9802 | } |
| 9803 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 9804 | if (!buffer) |
| 9805 | goto exit; |
| 9806 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9807 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9808 | Py_BEGIN_ALLOW_THREADS; |
| 9809 | if (path.fd >= 0) |
| 9810 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 9811 | else if (follow_symlinks) |
| 9812 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 9813 | else |
| 9814 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 9815 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9816 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9817 | if (result < 0) { |
| 9818 | Py_DECREF(buffer); |
| 9819 | buffer = NULL; |
| 9820 | if (errno == ERANGE) |
| 9821 | continue; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9822 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9823 | goto exit; |
| 9824 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9825 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9826 | if (result != buffer_size) { |
| 9827 | /* Can only shrink. */ |
| 9828 | _PyBytes_Resize(&buffer, result); |
| 9829 | } |
| 9830 | break; |
| 9831 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9832 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9833 | exit: |
| 9834 | path_cleanup(&path); |
| 9835 | path_cleanup(&attribute); |
| 9836 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9837 | } |
| 9838 | |
| 9839 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9840 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 9841 | Set extended attribute attribute on path to value.\n\ |
| 9842 | path may be either a string or an open file descriptor.\n\ |
| 9843 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 9844 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 9845 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9846 | |
| 9847 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9848 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9849 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9850 | path_t path; |
| 9851 | path_t attribute; |
| 9852 | Py_buffer value; |
| 9853 | int flags = 0; |
| 9854 | int follow_symlinks = 1; |
| 9855 | int result; |
| 9856 | PyObject *return_value = NULL; |
| 9857 | static char *keywords[] = {"path", "attribute", "value", |
| 9858 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9859 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9860 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9861 | path.function_name = "setxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9862 | path.allow_fd = 1; |
| 9863 | memset(&attribute, 0, sizeof(attribute)); |
| 9864 | memset(&value, 0, sizeof(value)); |
| 9865 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 9866 | keywords, |
| 9867 | path_converter, &path, |
| 9868 | path_converter, &attribute, |
| 9869 | &value, &flags, |
| 9870 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9871 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9872 | |
| 9873 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 9874 | goto exit; |
| 9875 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9876 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9877 | if (path.fd > -1) |
| 9878 | result = fsetxattr(path.fd, attribute.narrow, |
| 9879 | value.buf, value.len, flags); |
| 9880 | else if (follow_symlinks) |
| 9881 | result = setxattr(path.narrow, attribute.narrow, |
| 9882 | value.buf, value.len, flags); |
| 9883 | else |
| 9884 | result = lsetxattr(path.narrow, attribute.narrow, |
| 9885 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9886 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9887 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9888 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9889 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9890 | goto exit; |
| 9891 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9892 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9893 | return_value = Py_None; |
| 9894 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9895 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9896 | exit: |
| 9897 | path_cleanup(&path); |
| 9898 | path_cleanup(&attribute); |
| 9899 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9900 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9901 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9902 | } |
| 9903 | |
| 9904 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9905 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 9906 | Remove extended attribute attribute on path.\n\ |
| 9907 | path may be either a string or an open file descriptor.\n\ |
| 9908 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 9909 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 9910 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9911 | |
| 9912 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9913 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9914 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9915 | path_t path; |
| 9916 | path_t attribute; |
| 9917 | int follow_symlinks = 1; |
| 9918 | int result; |
| 9919 | PyObject *return_value = NULL; |
| 9920 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9921 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9922 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9923 | path.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9924 | memset(&attribute, 0, sizeof(attribute)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9925 | attribute.function_name = "removexattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9926 | path.allow_fd = 1; |
| 9927 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 9928 | keywords, |
| 9929 | path_converter, &path, |
| 9930 | path_converter, &attribute, |
| 9931 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9932 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9933 | |
| 9934 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 9935 | goto exit; |
| 9936 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9937 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9938 | if (path.fd > -1) |
| 9939 | result = fremovexattr(path.fd, attribute.narrow); |
| 9940 | else if (follow_symlinks) |
| 9941 | result = removexattr(path.narrow, attribute.narrow); |
| 9942 | else |
| 9943 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9944 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9945 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9946 | if (result) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9947 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9948 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9949 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9950 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9951 | return_value = Py_None; |
| 9952 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9953 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9954 | exit: |
| 9955 | path_cleanup(&path); |
| 9956 | path_cleanup(&attribute); |
| 9957 | |
| 9958 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9959 | } |
| 9960 | |
| 9961 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9962 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 9963 | Return a list of extended attributes on path.\n\ |
| 9964 | \n\ |
| 9965 | path may be either None, a string, or an open file descriptor.\n\ |
| 9966 | if path is None, listxattr will examine the current directory.\n\ |
| 9967 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 9968 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 9969 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9970 | |
| 9971 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9972 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9973 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9974 | path_t path; |
| 9975 | int follow_symlinks = 1; |
| 9976 | Py_ssize_t i; |
| 9977 | PyObject *result = NULL; |
| 9978 | char *buffer = NULL; |
| 9979 | char *name; |
| 9980 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9981 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9982 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9983 | path.function_name = "listxattr"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9984 | path.allow_fd = 1; |
| 9985 | path.fd = -1; |
| 9986 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 9987 | path_converter, &path, |
| 9988 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9989 | return NULL; |
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 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 9992 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 9993 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9994 | name = path.narrow ? path.narrow : "."; |
| 9995 | for (i = 0; ; i++) { |
| 9996 | char *start, *trace, *end; |
| 9997 | ssize_t length; |
| 9998 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 9999 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10000 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10001 | /* ERANGE */ |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10002 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10003 | break; |
| 10004 | } |
| 10005 | buffer = PyMem_MALLOC(buffer_size); |
| 10006 | if (!buffer) { |
| 10007 | PyErr_NoMemory(); |
| 10008 | break; |
| 10009 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10010 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10011 | Py_BEGIN_ALLOW_THREADS; |
| 10012 | if (path.fd > -1) |
| 10013 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10014 | else if (follow_symlinks) |
| 10015 | length = listxattr(name, buffer, buffer_size); |
| 10016 | else |
| 10017 | length = llistxattr(name, buffer, buffer_size); |
| 10018 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10019 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10020 | if (length < 0) { |
| 10021 | if (errno == ERANGE) |
| 10022 | continue; |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 10023 | path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10024 | break; |
| 10025 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10026 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10027 | result = PyList_New(0); |
| 10028 | if (!result) { |
| 10029 | goto exit; |
| 10030 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10031 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10032 | end = buffer + length; |
| 10033 | for (trace = start = buffer; trace != end; trace++) { |
| 10034 | if (!*trace) { |
| 10035 | int error; |
| 10036 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10037 | trace - start); |
| 10038 | if (!attribute) { |
| 10039 | Py_DECREF(result); |
| 10040 | result = NULL; |
| 10041 | goto exit; |
| 10042 | } |
| 10043 | error = PyList_Append(result, attribute); |
| 10044 | Py_DECREF(attribute); |
| 10045 | if (error) { |
| 10046 | Py_DECREF(result); |
| 10047 | result = NULL; |
| 10048 | goto exit; |
| 10049 | } |
| 10050 | start = trace + 1; |
| 10051 | } |
| 10052 | } |
| 10053 | break; |
| 10054 | } |
| 10055 | exit: |
| 10056 | path_cleanup(&path); |
| 10057 | if (buffer) |
| 10058 | PyMem_FREE(buffer); |
| 10059 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10060 | } |
| 10061 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10062 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10063 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10064 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10065 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10066 | "urandom(n) -> str\n\n\ |
| 10067 | Return n random bytes suitable for cryptographic use."); |
| 10068 | |
| 10069 | static PyObject * |
| 10070 | posix_urandom(PyObject *self, PyObject *args) |
| 10071 | { |
| 10072 | Py_ssize_t size; |
| 10073 | PyObject *result; |
| 10074 | int ret; |
| 10075 | |
| 10076 | /* Read arguments */ |
| 10077 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10078 | return NULL; |
| 10079 | if (size < 0) |
| 10080 | return PyErr_Format(PyExc_ValueError, |
| 10081 | "negative argument not allowed"); |
| 10082 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10083 | if (result == NULL) |
| 10084 | return NULL; |
| 10085 | |
| 10086 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10087 | PyBytes_GET_SIZE(result)); |
| 10088 | if (ret == -1) { |
| 10089 | Py_DECREF(result); |
| 10090 | return NULL; |
| 10091 | } |
| 10092 | return result; |
| 10093 | } |
| 10094 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10095 | /* Terminal size querying */ |
| 10096 | |
| 10097 | static PyTypeObject TerminalSizeType; |
| 10098 | |
| 10099 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10100 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10101 | |
| 10102 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10103 | {"columns", "width of the terminal window in characters"}, |
| 10104 | {"lines", "height of the terminal window in characters"}, |
| 10105 | {NULL, NULL} |
| 10106 | }; |
| 10107 | |
| 10108 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10109 | "os.terminal_size", |
| 10110 | TerminalSize_docstring, |
| 10111 | TerminalSize_fields, |
| 10112 | 2, |
| 10113 | }; |
| 10114 | |
| 10115 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10116 | PyDoc_STRVAR(termsize__doc__, |
| 10117 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10118 | "\n" \ |
| 10119 | "The optional argument fd (default standard output) specifies\n" \ |
| 10120 | "which file descriptor should be queried.\n" \ |
| 10121 | "\n" \ |
| 10122 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10123 | "is thrown.\n" \ |
| 10124 | "\n" \ |
| 10125 | "This function will only be defined if an implementation is\n" \ |
| 10126 | "available for this system.\n" \ |
| 10127 | "\n" \ |
| 10128 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10129 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10130 | |
| 10131 | static PyObject* |
| 10132 | get_terminal_size(PyObject *self, PyObject *args) |
| 10133 | { |
| 10134 | int columns, lines; |
| 10135 | PyObject *termsize; |
| 10136 | |
| 10137 | int fd = fileno(stdout); |
| 10138 | /* Under some conditions stdout may not be connected and |
| 10139 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10140 | * GUI apps don't have valid standard streams by default. |
| 10141 | * |
| 10142 | * If this happens, and the optional fd argument is not present, |
| 10143 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10144 | */ |
| 10145 | |
| 10146 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10147 | return NULL; |
| 10148 | |
| 10149 | #ifdef TERMSIZE_USE_IOCTL |
| 10150 | { |
| 10151 | struct winsize w; |
| 10152 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10153 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10154 | columns = w.ws_col; |
| 10155 | lines = w.ws_row; |
| 10156 | } |
| 10157 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10158 | |
| 10159 | #ifdef TERMSIZE_USE_CONIO |
| 10160 | { |
| 10161 | DWORD nhandle; |
| 10162 | HANDLE handle; |
| 10163 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10164 | switch (fd) { |
| 10165 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10166 | break; |
| 10167 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10168 | break; |
| 10169 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10170 | break; |
| 10171 | default: |
| 10172 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10173 | } |
| 10174 | handle = GetStdHandle(nhandle); |
| 10175 | if (handle == NULL) |
| 10176 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10177 | if (handle == INVALID_HANDLE_VALUE) |
| 10178 | return PyErr_SetFromWindowsErr(0); |
| 10179 | |
| 10180 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10181 | return PyErr_SetFromWindowsErr(0); |
| 10182 | |
| 10183 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10184 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10185 | } |
| 10186 | #endif /* TERMSIZE_USE_CONIO */ |
| 10187 | |
| 10188 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10189 | if (termsize == NULL) |
| 10190 | return NULL; |
| 10191 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10192 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10193 | if (PyErr_Occurred()) { |
| 10194 | Py_DECREF(termsize); |
| 10195 | return NULL; |
| 10196 | } |
| 10197 | return termsize; |
| 10198 | } |
| 10199 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10200 | |
| 10201 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10202 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10203 | {"access", (PyCFunction)posix_access, |
| 10204 | METH_VARARGS | METH_KEYWORDS, |
| 10205 | posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10206 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10207 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10208 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10209 | {"chdir", (PyCFunction)posix_chdir, |
| 10210 | METH_VARARGS | METH_KEYWORDS, |
| 10211 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10212 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10213 | {"chflags", (PyCFunction)posix_chflags, |
| 10214 | METH_VARARGS | METH_KEYWORDS, |
| 10215 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10216 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10217 | {"chmod", (PyCFunction)posix_chmod, |
| 10218 | METH_VARARGS | METH_KEYWORDS, |
| 10219 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10220 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10221 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10222 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10223 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10224 | {"chown", (PyCFunction)posix_chown, |
| 10225 | METH_VARARGS | METH_KEYWORDS, |
| 10226 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10227 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10228 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10229 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10230 | #endif /* HAVE_LCHMOD */ |
| 10231 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10232 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10233 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10234 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10235 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10236 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10237 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10238 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10239 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10240 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10241 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10242 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10243 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10244 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10245 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10246 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10247 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10248 | METH_NOARGS, posix_getcwd__doc__}, |
| 10249 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10250 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10251 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10252 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 10253 | {"link", (PyCFunction)posix_link, |
| 10254 | METH_VARARGS | METH_KEYWORDS, |
| 10255 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10256 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10257 | {"listdir", (PyCFunction)posix_listdir, |
| 10258 | METH_VARARGS | METH_KEYWORDS, |
| 10259 | posix_listdir__doc__}, |
| 10260 | {"lstat", (PyCFunction)posix_lstat, |
| 10261 | METH_VARARGS | METH_KEYWORDS, |
| 10262 | posix_lstat__doc__}, |
| 10263 | {"mkdir", (PyCFunction)posix_mkdir, |
| 10264 | METH_VARARGS | METH_KEYWORDS, |
| 10265 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10266 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10267 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10268 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10269 | #ifdef HAVE_GETPRIORITY |
| 10270 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10271 | #endif /* HAVE_GETPRIORITY */ |
| 10272 | #ifdef HAVE_SETPRIORITY |
| 10273 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10274 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10275 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10276 | {"readlink", (PyCFunction)posix_readlink, |
| 10277 | METH_VARARGS | METH_KEYWORDS, |
| 10278 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10279 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10280 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10281 | {"readlink", (PyCFunction)win_readlink, |
| 10282 | METH_VARARGS | METH_KEYWORDS, |
| 10283 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10284 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10285 | {"rename", (PyCFunction)posix_rename, |
| 10286 | METH_VARARGS | METH_KEYWORDS, |
| 10287 | posix_rename__doc__}, |
| 10288 | {"replace", (PyCFunction)posix_replace, |
| 10289 | METH_VARARGS | METH_KEYWORDS, |
| 10290 | posix_replace__doc__}, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 10291 | {"rmdir", (PyCFunction)posix_rmdir, |
| 10292 | METH_VARARGS | METH_KEYWORDS, |
| 10293 | posix_rmdir__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10294 | {"stat", (PyCFunction)posix_stat, |
| 10295 | METH_VARARGS | METH_KEYWORDS, |
| 10296 | posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10297 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10298 | #if defined(HAVE_SYMLINK) |
| 10299 | {"symlink", (PyCFunction)posix_symlink, |
| 10300 | METH_VARARGS | METH_KEYWORDS, |
| 10301 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10302 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10303 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10304 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10305 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10306 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10307 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10308 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10309 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10310 | {"unlink", (PyCFunction)posix_unlink, |
| 10311 | METH_VARARGS | METH_KEYWORDS, |
| 10312 | posix_unlink__doc__}, |
| 10313 | {"remove", (PyCFunction)posix_unlink, |
| 10314 | METH_VARARGS | METH_KEYWORDS, |
| 10315 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10316 | {"utime", (PyCFunction)posix_utime, |
| 10317 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10318 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10319 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10320 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10321 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10322 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10323 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10324 | {"execve", (PyCFunction)posix_execve, |
| 10325 | METH_VARARGS | METH_KEYWORDS, |
| 10326 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10327 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10328 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10329 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10330 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10331 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10332 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10333 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10334 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10335 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10336 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10337 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10338 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10339 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10340 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10341 | {"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] | 10342 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10343 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10344 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10345 | #endif |
| 10346 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10347 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10348 | #endif |
| 10349 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10350 | {"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] | 10351 | #endif |
| 10352 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10353 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10354 | #endif |
| 10355 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10356 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10357 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10358 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10359 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10360 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10361 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10362 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10363 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10364 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10365 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10366 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10367 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10368 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10369 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10370 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10371 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 10372 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10373 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10374 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 10375 | #endif /* HAVE_GETEUID */ |
| 10376 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10377 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10378 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10379 | #ifdef HAVE_GETGROUPLIST |
| 10380 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10381 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10382 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10383 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10384 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10385 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10386 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10387 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10388 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10389 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10390 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10391 | #endif /* HAVE_GETPPID */ |
| 10392 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10393 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10394 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10395 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10396 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10397 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10398 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10399 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10400 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10401 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10402 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10403 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10404 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10405 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10406 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10407 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10408 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10409 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10410 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10411 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10412 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10413 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10414 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10415 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10416 | #endif /* HAVE_SETEUID */ |
| 10417 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10418 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10419 | #endif /* HAVE_SETEGID */ |
| 10420 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10421 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10422 | #endif /* HAVE_SETREUID */ |
| 10423 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10424 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10425 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10426 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10427 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10428 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10429 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10430 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10431 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10432 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10433 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10434 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10435 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10436 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10437 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10438 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10439 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10440 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10441 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10442 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10443 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10444 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10445 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10446 | #endif /* HAVE_WAIT3 */ |
| 10447 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10448 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10449 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10450 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10451 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10452 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10453 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10454 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10455 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10456 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10457 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10458 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10459 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10460 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10461 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10462 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10463 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10464 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10465 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10466 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10467 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10468 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10469 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10470 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10471 | {"open", (PyCFunction)posix_open,\ |
| 10472 | METH_VARARGS | METH_KEYWORDS, |
| 10473 | posix_open__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10474 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10475 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10476 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10477 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10478 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10479 | #ifdef HAVE_LOCKF |
| 10480 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10481 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10482 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10483 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10484 | #ifdef HAVE_READV |
| 10485 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10486 | #endif |
| 10487 | #ifdef HAVE_PREAD |
| 10488 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10489 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10490 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10491 | #ifdef HAVE_WRITEV |
| 10492 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10493 | #endif |
| 10494 | #ifdef HAVE_PWRITE |
| 10495 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10496 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10497 | #ifdef HAVE_SENDFILE |
| 10498 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10499 | posix_sendfile__doc__}, |
| 10500 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10501 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10502 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10503 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10504 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10505 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10506 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10507 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10508 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10509 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10510 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 10511 | METH_VARARGS | METH_KEYWORDS, |
| 10512 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10513 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10514 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10515 | {"mknod", (PyCFunction)posix_mknod, |
| 10516 | METH_VARARGS | METH_KEYWORDS, |
| 10517 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10518 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10519 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10520 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10521 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10522 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10523 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10524 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10525 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10526 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10527 | #ifdef HAVE_TRUNCATE |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10528 | {"truncate", (PyCFunction)posix_truncate, |
| 10529 | METH_VARARGS | METH_KEYWORDS, |
| 10530 | posix_truncate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10531 | #endif |
| 10532 | #ifdef HAVE_POSIX_FALLOCATE |
| 10533 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10534 | #endif |
| 10535 | #ifdef HAVE_POSIX_FADVISE |
| 10536 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10537 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10538 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10539 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10540 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10541 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10542 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10543 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10544 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10545 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10546 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10547 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10548 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10549 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10550 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10551 | #ifdef HAVE_SYNC |
| 10552 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10553 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10554 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10555 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10556 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10557 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10558 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10559 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10560 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10561 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10562 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10563 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10564 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10565 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10566 | #endif /* WIFSTOPPED */ |
| 10567 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10568 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10569 | #endif /* WIFSIGNALED */ |
| 10570 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10571 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10572 | #endif /* WIFEXITED */ |
| 10573 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10574 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10575 | #endif /* WEXITSTATUS */ |
| 10576 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10577 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10578 | #endif /* WTERMSIG */ |
| 10579 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10580 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10581 | #endif /* WSTOPSIG */ |
| 10582 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10583 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10584 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10585 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10586 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10587 | {"statvfs", (PyCFunction)posix_statvfs, |
| 10588 | METH_VARARGS | METH_KEYWORDS, |
| 10589 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10590 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10591 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10592 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10593 | #endif |
| 10594 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10595 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10596 | #endif |
| 10597 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10598 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10599 | #endif |
| 10600 | #ifdef HAVE_PATHCONF |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10601 | {"pathconf", (PyCFunction)posix_pathconf, |
| 10602 | METH_VARARGS | METH_KEYWORDS, |
| 10603 | posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10604 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10605 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10606 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10607 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10608 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 10609 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10610 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10611 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10612 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10613 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10614 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10615 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 10616 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10617 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10618 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10619 | #endif |
| 10620 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10621 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10622 | #endif |
| 10623 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10624 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10625 | #endif |
| 10626 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10627 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10628 | #endif |
| 10629 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10630 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10631 | {"setxattr", (PyCFunction)posix_setxattr, |
| 10632 | METH_VARARGS | METH_KEYWORDS, |
| 10633 | posix_setxattr__doc__}, |
| 10634 | {"getxattr", (PyCFunction)posix_getxattr, |
| 10635 | METH_VARARGS | METH_KEYWORDS, |
| 10636 | posix_getxattr__doc__}, |
| 10637 | {"removexattr", (PyCFunction)posix_removexattr, |
| 10638 | METH_VARARGS | METH_KEYWORDS, |
| 10639 | posix_removexattr__doc__}, |
| 10640 | {"listxattr", (PyCFunction)posix_listxattr, |
| 10641 | METH_VARARGS | METH_KEYWORDS, |
| 10642 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10643 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10644 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10645 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 10646 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10647 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10648 | }; |
| 10649 | |
| 10650 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10651 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10652 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10653 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10654 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10655 | } |
| 10656 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10657 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10658 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10659 | enable_symlink() |
| 10660 | { |
| 10661 | HANDLE tok; |
| 10662 | TOKEN_PRIVILEGES tok_priv; |
| 10663 | LUID luid; |
| 10664 | int meth_idx = 0; |
| 10665 | |
| 10666 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10667 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10668 | |
| 10669 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10670 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10671 | |
| 10672 | tok_priv.PrivilegeCount = 1; |
| 10673 | tok_priv.Privileges[0].Luid = luid; |
| 10674 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 10675 | |
| 10676 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 10677 | sizeof(TOKEN_PRIVILEGES), |
| 10678 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10679 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10680 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10681 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 10682 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10683 | } |
| 10684 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 10685 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10686 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 10687 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10688 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10689 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10691 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10692 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10693 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10694 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10695 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10696 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10697 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10698 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10699 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10700 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10701 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10702 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10703 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10704 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10705 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10706 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10707 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10708 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10709 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10710 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10711 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10712 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10713 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10714 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10715 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10716 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10717 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10718 | #endif |
| 10719 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10720 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10721 | #endif |
| 10722 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10723 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10724 | #endif |
| 10725 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10726 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10727 | #endif |
| 10728 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10729 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10730 | #endif |
| 10731 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10732 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10733 | #endif |
| 10734 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10735 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10736 | #endif |
| 10737 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10738 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10739 | #endif |
| 10740 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10741 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10742 | #endif |
| 10743 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10744 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10745 | #endif |
| 10746 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10747 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10748 | #endif |
| 10749 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10750 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10751 | #endif |
| 10752 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10753 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10754 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10755 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10756 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10757 | #endif |
| 10758 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10759 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10760 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10761 | #ifdef O_XATTR |
| 10762 | if (ins(d, "O_XATTR", (long)O_XATTR)) return -1; |
| 10763 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10764 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10765 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10766 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10767 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10768 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10769 | #endif |
| 10770 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10771 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10772 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 10773 | #ifdef O_EXEC |
| 10774 | if (ins(d, "O_EXEC", (long)O_EXEC)) return -1; |
| 10775 | #endif |
| 10776 | #ifdef O_SEARCH |
| 10777 | if (ins(d, "O_SEARCH", (long)O_SEARCH)) return -1; |
| 10778 | #endif |
| 10779 | #ifdef O_TTY_INIT |
| 10780 | if (ins(d, "O_TTY_INIT", (long)O_TTY_INIT)) return -1; |
| 10781 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10782 | #ifdef PRIO_PROCESS |
| 10783 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 10784 | #endif |
| 10785 | #ifdef PRIO_PGRP |
| 10786 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 10787 | #endif |
| 10788 | #ifdef PRIO_USER |
| 10789 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 10790 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 10791 | #ifdef O_CLOEXEC |
| 10792 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 10793 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10794 | #ifdef O_ACCMODE |
| 10795 | if (ins(d, "O_ACCMODE", (long)O_ACCMODE)) return -1; |
| 10796 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10797 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10798 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 10799 | #ifdef SEEK_HOLE |
| 10800 | if (ins(d, "SEEK_HOLE", (long)SEEK_HOLE)) return -1; |
| 10801 | #endif |
| 10802 | #ifdef SEEK_DATA |
| 10803 | if (ins(d, "SEEK_DATA", (long)SEEK_DATA)) return -1; |
| 10804 | #endif |
| 10805 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10806 | /* MS Windows */ |
| 10807 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10808 | /* Don't inherit in child processes. */ |
| 10809 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10810 | #endif |
| 10811 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10812 | /* Optimize for short life (keep in memory). */ |
| 10813 | /* MS forgot to define this one with a non-underscore form too. */ |
| 10814 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10815 | #endif |
| 10816 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10817 | /* Automatically delete when last handle is closed. */ |
| 10818 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10819 | #endif |
| 10820 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10821 | /* Optimize for random access. */ |
| 10822 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10823 | #endif |
| 10824 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10825 | /* Optimize for sequential access. */ |
| 10826 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10827 | #endif |
| 10828 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10829 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 10830 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10831 | /* Send a SIGIO signal whenever input or output |
| 10832 | becomes available on file descriptor */ |
| 10833 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 10834 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10835 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10836 | /* Direct disk access. */ |
| 10837 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10838 | #endif |
| 10839 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10840 | /* Must be a directory. */ |
| 10841 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10842 | #endif |
| 10843 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10844 | /* Do not follow links. */ |
| 10845 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10846 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 10847 | #ifdef O_NOLINKS |
| 10848 | /* Fails if link count of the named file is greater than 1 */ |
| 10849 | if (ins(d, "O_NOLINKS", (long)O_NOLINKS)) return -1; |
| 10850 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 10851 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10852 | /* Do not update the access time. */ |
| 10853 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 10854 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10855 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10856 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10857 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10858 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10859 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10860 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10861 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10862 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10863 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10864 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10865 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10866 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10867 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10868 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10869 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10870 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10871 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10872 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10873 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10874 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10875 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10876 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10877 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10878 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10879 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10880 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10881 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10882 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10883 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10884 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10885 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10886 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10887 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10888 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10889 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10890 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10891 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10892 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10893 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10894 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10895 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10896 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10897 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10898 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10899 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10900 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10901 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10902 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10903 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10904 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10905 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10906 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10907 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10908 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 10909 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 10910 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 10911 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 10912 | #endif /* ST_RDONLY */ |
| 10913 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 10914 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 10915 | #endif /* ST_NOSUID */ |
| 10916 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10917 | /* FreeBSD sendfile() constants */ |
| 10918 | #ifdef SF_NODISKIO |
| 10919 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 10920 | #endif |
| 10921 | #ifdef SF_MNOWAIT |
| 10922 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 10923 | #endif |
| 10924 | #ifdef SF_SYNC |
| 10925 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 10926 | #endif |
| 10927 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10928 | /* constants for posix_fadvise */ |
| 10929 | #ifdef POSIX_FADV_NORMAL |
| 10930 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 10931 | #endif |
| 10932 | #ifdef POSIX_FADV_SEQUENTIAL |
| 10933 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 10934 | #endif |
| 10935 | #ifdef POSIX_FADV_RANDOM |
| 10936 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 10937 | #endif |
| 10938 | #ifdef POSIX_FADV_NOREUSE |
| 10939 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 10940 | #endif |
| 10941 | #ifdef POSIX_FADV_WILLNEED |
| 10942 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 10943 | #endif |
| 10944 | #ifdef POSIX_FADV_DONTNEED |
| 10945 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 10946 | #endif |
| 10947 | |
| 10948 | /* constants for waitid */ |
| 10949 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 10950 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 10951 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 10952 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 10953 | #endif |
| 10954 | #ifdef WEXITED |
| 10955 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 10956 | #endif |
| 10957 | #ifdef WNOWAIT |
| 10958 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 10959 | #endif |
| 10960 | #ifdef WSTOPPED |
| 10961 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 10962 | #endif |
| 10963 | #ifdef CLD_EXITED |
| 10964 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 10965 | #endif |
| 10966 | #ifdef CLD_DUMPED |
| 10967 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 10968 | #endif |
| 10969 | #ifdef CLD_TRAPPED |
| 10970 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 10971 | #endif |
| 10972 | #ifdef CLD_CONTINUED |
| 10973 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 10974 | #endif |
| 10975 | |
| 10976 | /* constants for lockf */ |
| 10977 | #ifdef F_LOCK |
| 10978 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 10979 | #endif |
| 10980 | #ifdef F_TLOCK |
| 10981 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 10982 | #endif |
| 10983 | #ifdef F_ULOCK |
| 10984 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 10985 | #endif |
| 10986 | #ifdef F_TEST |
| 10987 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 10988 | #endif |
| 10989 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 10990 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10991 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 10992 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 10993 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 10994 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 10995 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 10996 | #endif |
| 10997 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10998 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 10999 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11000 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11001 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11002 | #ifdef SCHED_SPORADIC |
| 11003 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11004 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11005 | #ifdef SCHED_BATCH |
| 11006 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11007 | #endif |
| 11008 | #ifdef SCHED_IDLE |
| 11009 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11010 | #endif |
| 11011 | #ifdef SCHED_RESET_ON_FORK |
| 11012 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11013 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11014 | #ifdef SCHED_SYS |
| 11015 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11016 | #endif |
| 11017 | #ifdef SCHED_IA |
| 11018 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11019 | #endif |
| 11020 | #ifdef SCHED_FSS |
| 11021 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11022 | #endif |
| 11023 | #ifdef SCHED_FX |
| 11024 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11025 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11026 | #endif |
| 11027 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11028 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11029 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11030 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11031 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11032 | #endif |
| 11033 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11034 | #ifdef RTLD_LAZY |
| 11035 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11036 | #endif |
| 11037 | #ifdef RTLD_NOW |
| 11038 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11039 | #endif |
| 11040 | #ifdef RTLD_GLOBAL |
| 11041 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11042 | #endif |
| 11043 | #ifdef RTLD_LOCAL |
| 11044 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11045 | #endif |
| 11046 | #ifdef RTLD_NODELETE |
| 11047 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11048 | #endif |
| 11049 | #ifdef RTLD_NOLOAD |
| 11050 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11051 | #endif |
| 11052 | #ifdef RTLD_DEEPBIND |
| 11053 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11054 | #endif |
| 11055 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11056 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11057 | } |
| 11058 | |
| 11059 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11060 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11061 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11062 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11063 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11064 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11065 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11066 | #define MODNAME "posix" |
| 11067 | #endif |
| 11068 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11069 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | PyModuleDef_HEAD_INIT, |
| 11071 | MODNAME, |
| 11072 | posix__doc__, |
| 11073 | -1, |
| 11074 | posix_methods, |
| 11075 | NULL, |
| 11076 | NULL, |
| 11077 | NULL, |
| 11078 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11079 | }; |
| 11080 | |
| 11081 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11082 | static char *have_functions[] = { |
| 11083 | |
| 11084 | #ifdef HAVE_FACCESSAT |
| 11085 | "HAVE_FACCESSAT", |
| 11086 | #endif |
| 11087 | |
| 11088 | #ifdef HAVE_FCHDIR |
| 11089 | "HAVE_FCHDIR", |
| 11090 | #endif |
| 11091 | |
| 11092 | #ifdef HAVE_FCHMOD |
| 11093 | "HAVE_FCHMOD", |
| 11094 | #endif |
| 11095 | |
| 11096 | #ifdef HAVE_FCHMODAT |
| 11097 | "HAVE_FCHMODAT", |
| 11098 | #endif |
| 11099 | |
| 11100 | #ifdef HAVE_FCHOWN |
| 11101 | "HAVE_FCHOWN", |
| 11102 | #endif |
| 11103 | |
| 11104 | #ifdef HAVE_FEXECVE |
| 11105 | "HAVE_FEXECVE", |
| 11106 | #endif |
| 11107 | |
| 11108 | #ifdef HAVE_FDOPENDIR |
| 11109 | "HAVE_FDOPENDIR", |
| 11110 | #endif |
| 11111 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11112 | #ifdef HAVE_FPATHCONF |
| 11113 | "HAVE_FPATHCONF", |
| 11114 | #endif |
| 11115 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11116 | #ifdef HAVE_FSTATAT |
| 11117 | "HAVE_FSTATAT", |
| 11118 | #endif |
| 11119 | |
| 11120 | #ifdef HAVE_FSTATVFS |
| 11121 | "HAVE_FSTATVFS", |
| 11122 | #endif |
| 11123 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11124 | #ifdef HAVE_FTRUNCATE |
| 11125 | "HAVE_FTRUNCATE", |
| 11126 | #endif |
| 11127 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11128 | #ifdef HAVE_FUTIMENS |
| 11129 | "HAVE_FUTIMENS", |
| 11130 | #endif |
| 11131 | |
| 11132 | #ifdef HAVE_FUTIMES |
| 11133 | "HAVE_FUTIMES", |
| 11134 | #endif |
| 11135 | |
| 11136 | #ifdef HAVE_FUTIMESAT |
| 11137 | "HAVE_FUTIMESAT", |
| 11138 | #endif |
| 11139 | |
| 11140 | #ifdef HAVE_LINKAT |
| 11141 | "HAVE_LINKAT", |
| 11142 | #endif |
| 11143 | |
| 11144 | #ifdef HAVE_LCHFLAGS |
| 11145 | "HAVE_LCHFLAGS", |
| 11146 | #endif |
| 11147 | |
| 11148 | #ifdef HAVE_LCHMOD |
| 11149 | "HAVE_LCHMOD", |
| 11150 | #endif |
| 11151 | |
| 11152 | #ifdef HAVE_LCHOWN |
| 11153 | "HAVE_LCHOWN", |
| 11154 | #endif |
| 11155 | |
| 11156 | #ifdef HAVE_LSTAT |
| 11157 | "HAVE_LSTAT", |
| 11158 | #endif |
| 11159 | |
| 11160 | #ifdef HAVE_LUTIMES |
| 11161 | "HAVE_LUTIMES", |
| 11162 | #endif |
| 11163 | |
| 11164 | #ifdef HAVE_MKDIRAT |
| 11165 | "HAVE_MKDIRAT", |
| 11166 | #endif |
| 11167 | |
| 11168 | #ifdef HAVE_MKFIFOAT |
| 11169 | "HAVE_MKFIFOAT", |
| 11170 | #endif |
| 11171 | |
| 11172 | #ifdef HAVE_MKNODAT |
| 11173 | "HAVE_MKNODAT", |
| 11174 | #endif |
| 11175 | |
| 11176 | #ifdef HAVE_OPENAT |
| 11177 | "HAVE_OPENAT", |
| 11178 | #endif |
| 11179 | |
| 11180 | #ifdef HAVE_READLINKAT |
| 11181 | "HAVE_READLINKAT", |
| 11182 | #endif |
| 11183 | |
| 11184 | #ifdef HAVE_RENAMEAT |
| 11185 | "HAVE_RENAMEAT", |
| 11186 | #endif |
| 11187 | |
| 11188 | #ifdef HAVE_SYMLINKAT |
| 11189 | "HAVE_SYMLINKAT", |
| 11190 | #endif |
| 11191 | |
| 11192 | #ifdef HAVE_UNLINKAT |
| 11193 | "HAVE_UNLINKAT", |
| 11194 | #endif |
| 11195 | |
| 11196 | #ifdef HAVE_UTIMENSAT |
| 11197 | "HAVE_UTIMENSAT", |
| 11198 | #endif |
| 11199 | |
| 11200 | #ifdef MS_WINDOWS |
| 11201 | "MS_WINDOWS", |
| 11202 | #endif |
| 11203 | |
| 11204 | NULL |
| 11205 | }; |
| 11206 | |
| 11207 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11208 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11209 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11210 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11211 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11212 | PyObject *list; |
| 11213 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11214 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11215 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11216 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11217 | #endif |
| 11218 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11219 | m = PyModule_Create(&posixmodule); |
| 11220 | if (m == NULL) |
| 11221 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11222 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11223 | /* Initialize environ dictionary */ |
| 11224 | v = convertenviron(); |
| 11225 | Py_XINCREF(v); |
| 11226 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11227 | return NULL; |
| 11228 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11229 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11230 | if (all_ins(m)) |
| 11231 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11232 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11233 | if (setup_confname_tables(m)) |
| 11234 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11235 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11236 | Py_INCREF(PyExc_OSError); |
| 11237 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11238 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11239 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11240 | if (posix_putenv_garbage == NULL) |
| 11241 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11242 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11243 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11244 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11245 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11246 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11247 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11248 | #endif |
| 11249 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11250 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11251 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11252 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11253 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11254 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11255 | structseq_new = StatResultType.tp_new; |
| 11256 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11257 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11258 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11259 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11260 | #ifdef NEED_TICKS_PER_SECOND |
| 11261 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11263 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11264 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11265 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11266 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11267 | # endif |
| 11268 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11269 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11270 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11271 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11272 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11273 | SchedParamType.tp_new = sched_param_new; |
| 11274 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11275 | |
| 11276 | /* initialize TerminalSize_info */ |
| 11277 | PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11278 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11279 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11280 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11281 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11282 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11283 | Py_INCREF((PyObject*) &StatResultType); |
| 11284 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11285 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11286 | PyModule_AddObject(m, "statvfs_result", |
| 11287 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11288 | |
| 11289 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11290 | Py_INCREF(&SchedParamType); |
| 11291 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11292 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11293 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 11294 | times_result_desc.name = MODNAME ".times_result"; |
| 11295 | PyStructSequence_InitType(&TimesResultType, ×_result_desc); |
| 11296 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 11297 | |
| 11298 | uname_result_desc.name = MODNAME ".uname_result"; |
| 11299 | PyStructSequence_InitType(&UnameResultType, &uname_result_desc); |
| 11300 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 11301 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11302 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11303 | /* |
| 11304 | * Step 2 of weak-linking support on Mac OS X. |
| 11305 | * |
| 11306 | * The code below removes functions that are not available on the |
| 11307 | * currently active platform. |
| 11308 | * |
| 11309 | * 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] | 11310 | * 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] | 11311 | * OSX 10.4. |
| 11312 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11313 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | if (fstatvfs == NULL) { |
| 11315 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11316 | return NULL; |
| 11317 | } |
| 11318 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11319 | #endif /* HAVE_FSTATVFS */ |
| 11320 | |
| 11321 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11322 | if (statvfs == NULL) { |
| 11323 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11324 | return NULL; |
| 11325 | } |
| 11326 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11327 | #endif /* HAVE_STATVFS */ |
| 11328 | |
| 11329 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11330 | if (lchown == NULL) { |
| 11331 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11332 | return NULL; |
| 11333 | } |
| 11334 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11335 | #endif /* HAVE_LCHOWN */ |
| 11336 | |
| 11337 | |
| 11338 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11339 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 11340 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11341 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 11342 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 11343 | billion = PyLong_FromLong(1000000000); |
| 11344 | if (!billion) |
| 11345 | return NULL; |
| 11346 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11347 | /* suppress "function not used" warnings */ |
| 11348 | { |
| 11349 | int ignored; |
| 11350 | fd_specified("", -1); |
| 11351 | follow_symlinks_specified("", 1); |
| 11352 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 11353 | dir_fd_converter(Py_None, &ignored); |
| 11354 | dir_fd_unavailable(Py_None, &ignored); |
| 11355 | } |
| 11356 | |
| 11357 | /* |
| 11358 | * provide list of locally available functions |
| 11359 | * so os.py can populate support_* lists |
| 11360 | */ |
| 11361 | list = PyList_New(0); |
| 11362 | if (!list) |
| 11363 | return NULL; |
| 11364 | for (trace = have_functions; *trace; trace++) { |
| 11365 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 11366 | if (!unicode) |
| 11367 | return NULL; |
| 11368 | if (PyList_Append(list, unicode)) |
| 11369 | return NULL; |
| 11370 | Py_DECREF(unicode); |
| 11371 | } |
| 11372 | PyModule_AddObject(m, "_have_functions", list); |
| 11373 | |
| 11374 | initialized = 1; |
| 11375 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11376 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11377 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11378 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11379 | |
| 11380 | #ifdef __cplusplus |
| 11381 | } |
| 11382 | #endif |