Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14 | #ifdef __APPLE__ |
| 15 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 16 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 17 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 18 | * at the end of this file for more information. |
| 19 | */ |
| 20 | # pragma weak lchown |
| 21 | # pragma weak statvfs |
| 22 | # pragma weak fstatvfs |
| 23 | |
| 24 | #endif /* __APPLE__ */ |
| 25 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 26 | #define PY_SSIZE_T_CLEAN |
| 27 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 28 | #include "Python.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 30 | #if defined(__VMS) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 31 | # error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4" |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 32 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #endif /* defined(__VMS) */ |
| 34 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 39 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 40 | "This module provides access to operating system functionality that is\n\ |
| 41 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 42 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 43 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 46 | #if defined(PYOS_OS2) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 47 | #error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #define INCL_DOS |
| 49 | #define INCL_DOSERRORS |
| 50 | #define INCL_DOSPROCESS |
| 51 | #define INCL_NOPMAPI |
| 52 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 53 | #if defined(PYCC_GCC) |
| 54 | #include <ctype.h> |
| 55 | #include <io.h> |
| 56 | #include <stdio.h> |
| 57 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 58 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 59 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 62 | #ifdef HAVE_SYS_UIO_H |
| 63 | #include <sys/uio.h> |
| 64 | #endif |
| 65 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #endif /* HAVE_SYS_TYPES_H */ |
| 69 | |
| 70 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 75 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 79 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | #ifdef HAVE_FCNTL_H |
| 83 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 84 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 86 | #ifdef HAVE_GRP_H |
| 87 | #include <grp.h> |
| 88 | #endif |
| 89 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 90 | #ifdef HAVE_SYSEXITS_H |
| 91 | #include <sysexits.h> |
| 92 | #endif /* HAVE_SYSEXITS_H */ |
| 93 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 94 | #ifdef HAVE_SYS_LOADAVG_H |
| 95 | #include <sys/loadavg.h> |
| 96 | #endif |
| 97 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 98 | #ifdef HAVE_LANGINFO_H |
| 99 | #include <langinfo.h> |
| 100 | #endif |
| 101 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 102 | #ifdef HAVE_SYS_SENDFILE_H |
| 103 | #include <sys/sendfile.h> |
| 104 | #endif |
| 105 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 106 | #ifdef HAVE_SCHED_H |
| 107 | #include <sched.h> |
| 108 | #endif |
| 109 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 110 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 111 | #undef HAVE_SCHED_SETAFFINITY |
| 112 | #endif |
| 113 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 114 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) |
| 115 | #define USE_XATTRS |
| 116 | #endif |
| 117 | |
| 118 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 119 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 120 | #endif |
| 121 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 122 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 123 | #ifdef HAVE_SYS_SOCKET_H |
| 124 | #include <sys/socket.h> |
| 125 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 126 | #endif |
| 127 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 128 | #ifdef HAVE_DLFCN_H |
| 129 | #include <dlfcn.h> |
| 130 | #endif |
| 131 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 132 | #if defined(MS_WINDOWS) |
| 133 | # define TERMSIZE_USE_CONIO |
| 134 | #elif defined(HAVE_SYS_IOCTL_H) |
| 135 | # include <sys/ioctl.h> |
| 136 | # if defined(HAVE_TERMIOS_H) |
| 137 | # include <termios.h> |
| 138 | # endif |
| 139 | # if defined(TIOCGWINSZ) |
| 140 | # define TERMSIZE_USE_IOCTL |
| 141 | # endif |
| 142 | #endif /* MS_WINDOWS */ |
| 143 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 144 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 145 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 146 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 147 | #include <process.h> |
| 148 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 149 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #define HAVE_GETCWD 1 |
| 151 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 152 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 153 | #if defined(__OS2__) |
| 154 | #define HAVE_EXECV 1 |
| 155 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 156 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 157 | #include <process.h> |
| 158 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 159 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 160 | #define HAVE_EXECV 1 |
| 161 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 162 | #define HAVE_OPENDIR 1 |
| 163 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 164 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 165 | #define HAVE_WAIT 1 |
| 166 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 167 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 168 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 169 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 170 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 171 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 172 | #define HAVE_EXECV 1 |
| 173 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 174 | #define HAVE_SYSTEM 1 |
| 175 | #define HAVE_CWAIT 1 |
| 176 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 177 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 178 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 179 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 180 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 181 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 182 | /* Unix functions that the configure script doesn't check for */ |
| 183 | #define HAVE_EXECV 1 |
| 184 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 185 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 186 | #define HAVE_FORK1 1 |
| 187 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 188 | #define HAVE_GETCWD 1 |
| 189 | #define HAVE_GETEGID 1 |
| 190 | #define HAVE_GETEUID 1 |
| 191 | #define HAVE_GETGID 1 |
| 192 | #define HAVE_GETPPID 1 |
| 193 | #define HAVE_GETUID 1 |
| 194 | #define HAVE_KILL 1 |
| 195 | #define HAVE_OPENDIR 1 |
| 196 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 197 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 198 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 199 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 200 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 201 | #endif /* _MSC_VER */ |
| 202 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 203 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 204 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 205 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 206 | |
| 207 | |
| 208 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 209 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 210 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 211 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 212 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 213 | (default) */ |
| 214 | extern char *ctermid_r(char *); |
| 215 | #endif |
| 216 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 217 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 218 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 219 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 220 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 221 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 222 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 223 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 224 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 225 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 226 | #endif |
| 227 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 228 | extern int chdir(char *); |
| 229 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 230 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 231 | extern int chdir(const char *); |
| 232 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 233 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 234 | #ifdef __BORLANDC__ |
| 235 | extern int chmod(const char *, int); |
| 236 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 237 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 238 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 239 | /*#ifdef HAVE_FCHMOD |
| 240 | extern int fchmod(int, mode_t); |
| 241 | #endif*/ |
| 242 | /*#ifdef HAVE_LCHMOD |
| 243 | extern int lchmod(const char *, mode_t); |
| 244 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 245 | extern int chown(const char *, uid_t, gid_t); |
| 246 | extern char *getcwd(char *, int); |
| 247 | extern char *strerror(int); |
| 248 | extern int link(const char *, const char *); |
| 249 | extern int rename(const char *, const char *); |
| 250 | extern int stat(const char *, struct stat *); |
| 251 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 253 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 254 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 256 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 257 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 260 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | #ifdef HAVE_UTIME_H |
| 263 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 264 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 266 | #ifdef HAVE_SYS_UTIME_H |
| 267 | #include <sys/utime.h> |
| 268 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 269 | #endif /* HAVE_SYS_UTIME_H */ |
| 270 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | #ifdef HAVE_SYS_TIMES_H |
| 272 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 273 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | |
| 275 | #ifdef HAVE_SYS_PARAM_H |
| 276 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 277 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | |
| 279 | #ifdef HAVE_SYS_UTSNAME_H |
| 280 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 281 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 283 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 285 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 286 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 287 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 288 | #include <direct.h> |
| 289 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 290 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 291 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 292 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 293 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 294 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 295 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 296 | #endif |
| 297 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 298 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 299 | #endif |
| 300 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 301 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 302 | #endif |
| 303 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 305 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 306 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 307 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 308 | #endif |
| 309 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 310 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 311 | #endif |
| 312 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 313 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 314 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 315 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 316 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 317 | #endif |
| 318 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 319 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 320 | #endif |
| 321 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 322 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 323 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 324 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 325 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 326 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 327 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 328 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 329 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 330 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 331 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 332 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 333 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 334 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 335 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 336 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 337 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 339 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 340 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 341 | #define MAXPATHLEN PATH_MAX |
| 342 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 343 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 344 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 345 | #endif /* MAXPATHLEN */ |
| 346 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 347 | #ifdef UNION_WAIT |
| 348 | /* Emulate some macros on systems that have a union instead of macros */ |
| 349 | |
| 350 | #ifndef WIFEXITED |
| 351 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 352 | #endif |
| 353 | |
| 354 | #ifndef WEXITSTATUS |
| 355 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 356 | #endif |
| 357 | |
| 358 | #ifndef WTERMSIG |
| 359 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 360 | #endif |
| 361 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 362 | #define WAIT_TYPE union wait |
| 363 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 364 | |
| 365 | #else /* !UNION_WAIT */ |
| 366 | #define WAIT_TYPE int |
| 367 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 368 | #endif /* UNION_WAIT */ |
| 369 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 370 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 371 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 372 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 373 | #define USE_CTERMID_R |
| 374 | #endif |
| 375 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 376 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 377 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 378 | #undef FSTAT |
| 379 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 380 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 381 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 382 | # define LSTAT win32_lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 383 | # define FSTAT win32_fstat |
| 384 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 385 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 386 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 387 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 388 | # define FSTAT fstat |
| 389 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 390 | #endif |
| 391 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 392 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 393 | #include <sys/mkdev.h> |
| 394 | #else |
| 395 | #if defined(MAJOR_IN_SYSMACROS) |
| 396 | #include <sys/sysmacros.h> |
| 397 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 398 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 399 | #include <sys/mkdev.h> |
| 400 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 401 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 402 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 403 | |
| 404 | #ifdef MS_WINDOWS |
| 405 | static int |
| 406 | win32_warn_bytes_api() |
| 407 | { |
| 408 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 409 | "The Windows bytes API has been deprecated, " |
| 410 | "use Unicode filenames instead", |
| 411 | 1); |
| 412 | } |
| 413 | #endif |
| 414 | |
| 415 | |
| 416 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 417 | /* |
| 418 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 419 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 420 | * which doesn't play nicely with all the initializer lines in this file that |
| 421 | * look like this: |
| 422 | * int dir_fd = DEFAULT_DIR_FD; |
| 423 | */ |
| 424 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 425 | #else |
| 426 | #define DEFAULT_DIR_FD (-100) |
| 427 | #endif |
| 428 | |
| 429 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 430 | _fd_converter(PyObject *o, int *p, const char *allowed) |
| 431 | { |
| 432 | int overflow; |
| 433 | long long_value = PyLong_AsLongAndOverflow(o, &overflow); |
| 434 | if (PyFloat_Check(o) || |
| 435 | (long_value == -1 && !overflow && PyErr_Occurred())) { |
| 436 | PyErr_Clear(); |
| 437 | PyErr_Format(PyExc_TypeError, |
| 438 | "argument should be %s, not %.200s", |
| 439 | allowed, Py_TYPE(o)->tp_name); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 440 | return 0; |
| 441 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 442 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 443 | PyErr_SetString(PyExc_OverflowError, |
| 444 | "signed integer is greater than maximum"); |
| 445 | return 0; |
| 446 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 447 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 448 | PyErr_SetString(PyExc_OverflowError, |
| 449 | "signed integer is less than minimum"); |
| 450 | return 0; |
| 451 | } |
| 452 | *p = (int)long_value; |
| 453 | return 1; |
| 454 | } |
| 455 | |
| 456 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 457 | dir_fd_converter(PyObject *o, void *p) |
| 458 | { |
| 459 | if (o == Py_None) { |
| 460 | *(int *)p = DEFAULT_DIR_FD; |
| 461 | return 1; |
| 462 | } |
| 463 | return _fd_converter(o, (int *)p, "integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | /* |
| 469 | * A PyArg_ParseTuple "converter" function |
| 470 | * that handles filesystem paths in the manner |
| 471 | * preferred by the os module. |
| 472 | * |
| 473 | * path_converter accepts (Unicode) strings and their |
| 474 | * subclasses, and bytes and their subclasses. What |
| 475 | * it does with the argument depends on the platform: |
| 476 | * |
| 477 | * * On Windows, if we get a (Unicode) string we |
| 478 | * extract the wchar_t * and return it; if we get |
| 479 | * bytes we extract the char * and return that. |
| 480 | * |
| 481 | * * On all other platforms, strings are encoded |
| 482 | * to bytes using PyUnicode_FSConverter, then we |
| 483 | * extract the char * from the bytes object and |
| 484 | * return that. |
| 485 | * |
| 486 | * path_converter also optionally accepts signed |
| 487 | * integers (representing open file descriptors) instead |
| 488 | * of path strings. |
| 489 | * |
| 490 | * Input fields: |
| 491 | * path.nullable |
| 492 | * If nonzero, the path is permitted to be None. |
| 493 | * path.allow_fd |
| 494 | * If nonzero, the path is permitted to be a file handle |
| 495 | * (a signed int) instead of a string. |
| 496 | * path.function_name |
| 497 | * If non-NULL, path_converter will use that as the name |
| 498 | * of the function in error messages. |
| 499 | * (If path.argument_name is NULL it omits the function name.) |
| 500 | * path.argument_name |
| 501 | * If non-NULL, path_converter will use that as the name |
| 502 | * of the parameter in error messages. |
| 503 | * (If path.argument_name is NULL it uses "path".) |
| 504 | * |
| 505 | * Output fields: |
| 506 | * path.wide |
| 507 | * Points to the path if it was expressed as Unicode |
| 508 | * and was not encoded. (Only used on Windows.) |
| 509 | * path.narrow |
| 510 | * Points to the path if it was expressed as bytes, |
| 511 | * or it was Unicode and was encoded to bytes. |
| 512 | * path.fd |
| 513 | * Contains a file descriptor if path.accept_fd was true |
| 514 | * and the caller provided a signed integer instead of any |
| 515 | * sort of string. |
| 516 | * |
| 517 | * WARNING: if your "path" parameter is optional, and is |
| 518 | * unspecified, path_converter will never get called. |
| 519 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 520 | * yourself! |
| 521 | * path.length |
| 522 | * The length of the path in characters, if specified as |
| 523 | * a string. |
| 524 | * path.object |
| 525 | * The original object passed in. |
| 526 | * path.cleanup |
| 527 | * For internal use only. May point to a temporary object. |
| 528 | * (Pay no attention to the man behind the curtain.) |
| 529 | * |
| 530 | * At most one of path.wide or path.narrow will be non-NULL. |
| 531 | * If path was None and path.nullable was set, |
| 532 | * or if path was an integer and path.allow_fd was set, |
| 533 | * both path.wide and path.narrow will be NULL |
| 534 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 535 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 536 | * path_converter takes care to not write to the path_t |
| 537 | * unless it's successful. However it must reset the |
| 538 | * "cleanup" field each time it's called. |
| 539 | * |
| 540 | * Use as follows: |
| 541 | * path_t path; |
| 542 | * memset(&path, 0, sizeof(path)); |
| 543 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 544 | * // ... use values from path ... |
| 545 | * path_cleanup(&path); |
| 546 | * |
| 547 | * (Note that if PyArg_Parse fails you don't need to call |
| 548 | * path_cleanup(). However it is safe to do so.) |
| 549 | */ |
| 550 | typedef struct { |
| 551 | char *function_name; |
| 552 | char *argument_name; |
| 553 | int nullable; |
| 554 | int allow_fd; |
| 555 | wchar_t *wide; |
| 556 | char *narrow; |
| 557 | int fd; |
| 558 | Py_ssize_t length; |
| 559 | PyObject *object; |
| 560 | PyObject *cleanup; |
| 561 | } path_t; |
| 562 | |
| 563 | static void |
| 564 | path_cleanup(path_t *path) { |
| 565 | if (path->cleanup) { |
| 566 | Py_DECREF(path->cleanup); |
| 567 | path->cleanup = NULL; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | static int |
| 572 | path_converter(PyObject *o, void *p) { |
| 573 | path_t *path = (path_t *)p; |
| 574 | PyObject *unicode, *bytes; |
| 575 | Py_ssize_t length; |
| 576 | char *narrow; |
| 577 | |
| 578 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 579 | PyErr_Format(exc, "%s%s" fmt, \ |
| 580 | path->function_name ? path->function_name : "", \ |
| 581 | path->function_name ? ": " : "", \ |
| 582 | path->argument_name ? path->argument_name : "path") |
| 583 | |
| 584 | /* Py_CLEANUP_SUPPORTED support */ |
| 585 | if (o == NULL) { |
| 586 | path_cleanup(path); |
| 587 | return 1; |
| 588 | } |
| 589 | |
| 590 | /* ensure it's always safe to call path_cleanup() */ |
| 591 | path->cleanup = NULL; |
| 592 | |
| 593 | if (o == Py_None) { |
| 594 | if (!path->nullable) { |
| 595 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 596 | "can't specify None for %s argument"); |
| 597 | return 0; |
| 598 | } |
| 599 | path->wide = NULL; |
| 600 | path->narrow = NULL; |
| 601 | path->length = 0; |
| 602 | path->object = o; |
| 603 | path->fd = -1; |
| 604 | return 1; |
| 605 | } |
| 606 | |
| 607 | unicode = PyUnicode_FromObject(o); |
| 608 | if (unicode) { |
| 609 | #ifdef MS_WINDOWS |
| 610 | wchar_t *wide; |
| 611 | length = PyUnicode_GET_SIZE(unicode); |
| 612 | if (length > 32767) { |
| 613 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 614 | Py_DECREF(unicode); |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | wide = PyUnicode_AsUnicode(unicode); |
| 619 | if (!wide) { |
| 620 | Py_DECREF(unicode); |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | path->wide = wide; |
| 625 | path->narrow = NULL; |
| 626 | path->length = length; |
| 627 | path->object = o; |
| 628 | path->fd = -1; |
| 629 | path->cleanup = unicode; |
| 630 | return Py_CLEANUP_SUPPORTED; |
| 631 | #else |
| 632 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 633 | Py_DECREF(unicode); |
| 634 | if (!converted) |
| 635 | bytes = NULL; |
| 636 | #endif |
| 637 | } |
| 638 | else { |
| 639 | PyErr_Clear(); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 640 | if (PyObject_CheckBuffer(o)) |
| 641 | bytes = PyBytes_FromObject(o); |
| 642 | else |
| 643 | bytes = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 644 | if (!bytes) { |
| 645 | PyErr_Clear(); |
| 646 | if (path->allow_fd) { |
| 647 | int fd; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 648 | int result = _fd_converter(o, &fd, |
| 649 | "string, bytes or integer"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 650 | if (result) { |
| 651 | path->wide = NULL; |
| 652 | path->narrow = NULL; |
| 653 | path->length = 0; |
| 654 | path->object = o; |
| 655 | path->fd = fd; |
| 656 | return result; |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (!bytes) { |
| 663 | if (!PyErr_Occurred()) |
| 664 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | #ifdef MS_WINDOWS |
| 669 | if (win32_warn_bytes_api()) { |
| 670 | Py_DECREF(bytes); |
| 671 | return 0; |
| 672 | } |
| 673 | #endif |
| 674 | |
| 675 | length = PyBytes_GET_SIZE(bytes); |
| 676 | #ifdef MS_WINDOWS |
| 677 | if (length > MAX_PATH) { |
| 678 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 679 | Py_DECREF(bytes); |
| 680 | return 0; |
| 681 | } |
| 682 | #endif |
| 683 | |
| 684 | narrow = PyBytes_AS_STRING(bytes); |
| 685 | if (length != strlen(narrow)) { |
| 686 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); |
| 687 | Py_DECREF(bytes); |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | path->wide = NULL; |
| 692 | path->narrow = narrow; |
| 693 | path->length = length; |
| 694 | path->object = o; |
| 695 | path->fd = -1; |
| 696 | path->cleanup = bytes; |
| 697 | return Py_CLEANUP_SUPPORTED; |
| 698 | } |
| 699 | |
| 700 | static void |
| 701 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 702 | PyErr_Format(PyExc_NotImplementedError, |
| 703 | "%s%s%s unavailable on this platform", |
| 704 | (function_name != NULL) ? function_name : "", |
| 705 | (function_name != NULL) ? ": ": "", |
| 706 | argument_name); |
| 707 | } |
| 708 | |
| 709 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 710 | dir_fd_unavailable(PyObject *o, void *p) |
| 711 | { |
| 712 | int dir_fd; |
| 713 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 714 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 715 | if (dir_fd != DEFAULT_DIR_FD) { |
| 716 | argument_unavailable_error(NULL, "dir_fd"); |
| 717 | return 0; |
| 718 | } |
| 719 | *(int *)p = dir_fd; |
| 720 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | static int |
| 724 | fd_specified(char *function_name, int fd) { |
| 725 | if (fd == -1) |
| 726 | return 0; |
| 727 | |
| 728 | argument_unavailable_error(function_name, "fd"); |
| 729 | return 1; |
| 730 | } |
| 731 | |
| 732 | static int |
| 733 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 734 | if (follow_symlinks) |
| 735 | return 0; |
| 736 | |
| 737 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 738 | return 1; |
| 739 | } |
| 740 | |
| 741 | static int |
| 742 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 743 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 744 | PyErr_Format(PyExc_ValueError, |
| 745 | "%s: can't specify dir_fd without matching path", |
| 746 | function_name); |
| 747 | return 1; |
| 748 | } |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int |
| 753 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 754 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 755 | PyErr_Format(PyExc_ValueError, |
| 756 | "%s: can't specify both dir_fd and fd", |
| 757 | function_name); |
| 758 | return 1; |
| 759 | } |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static int |
| 764 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 765 | int follow_symlinks) { |
| 766 | if ((fd > 0) && (!follow_symlinks)) { |
| 767 | PyErr_Format(PyExc_ValueError, |
| 768 | "%s: cannot use fd and follow_symlinks together", |
| 769 | function_name); |
| 770 | return 1; |
| 771 | } |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static int |
| 776 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 777 | int follow_symlinks) { |
| 778 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 779 | PyErr_Format(PyExc_ValueError, |
| 780 | "%s: cannot use dir_fd and follow_symlinks together", |
| 781 | function_name); |
| 782 | return 1; |
| 783 | } |
| 784 | return 0; |
| 785 | } |
| 786 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 787 | /* A helper used by a number of POSIX-only functions */ |
| 788 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 789 | static int |
| 790 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 791 | { |
| 792 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 793 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 794 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 795 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 796 | #endif |
| 797 | if (PyErr_Occurred()) |
| 798 | return 0; |
| 799 | return 1; |
| 800 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 801 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 802 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 803 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 804 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 805 | * valid and raise an assertion if it isn't. |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 806 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 807 | * an assertion can be useful, but it does contradict the POSIX standard |
| 808 | * which for write(2) states: |
| 809 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 810 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 811 | * writing." |
| 812 | * Furthermore, python allows the user to enter any old integer |
| 813 | * as a fd and should merely raise a python exception on error. |
| 814 | * The Microsoft CRT doesn't provide an official way to check for the |
| 815 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 816 | * 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] | 817 | * internal structures involved. |
| 818 | * The structures below must be updated for each version of visual studio |
| 819 | * according to the file internal.h in the CRT source, until MS comes |
| 820 | * up with a less hacky way to do this. |
| 821 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 822 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 823 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 824 | /* The actual size of the structure is determined at runtime. |
| 825 | * Only the first items must be present. |
| 826 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 827 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 828 | intptr_t osfhnd; |
| 829 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 830 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 831 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 832 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 833 | #define IOINFO_L2E 5 |
| 834 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 835 | #define IOINFO_ARRAYS 64 |
| 836 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 837 | #define FOPEN 0x01 |
| 838 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 839 | |
| 840 | /* This function emulates what the windows CRT does to validate file handles */ |
| 841 | int |
| 842 | _PyVerify_fd(int fd) |
| 843 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 844 | const int i1 = fd >> IOINFO_L2E; |
| 845 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 847 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 848 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 849 | /* Determine the actual size of the ioinfo structure, |
| 850 | * as used by the CRT loaded in memory |
| 851 | */ |
| 852 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 853 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 854 | } |
| 855 | if (sizeof_ioinfo == 0) { |
| 856 | /* This should not happen... */ |
| 857 | goto fail; |
| 858 | } |
| 859 | |
| 860 | /* See that it isn't a special CLEAR fileno */ |
| 861 | if (fd != _NO_CONSOLE_FILENO) { |
| 862 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 863 | * we check pointer validity and other info |
| 864 | */ |
| 865 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 866 | /* finally, check that the file is open */ |
| 867 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 868 | if (info->osfile & FOPEN) { |
| 869 | return 1; |
| 870 | } |
| 871 | } |
| 872 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 873 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 874 | errno = EBADF; |
| 875 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 879 | static int |
| 880 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 881 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 882 | if (!_PyVerify_fd(fd1)) |
| 883 | return 0; |
| 884 | if (fd2 == _NO_CONSOLE_FILENO) |
| 885 | return 0; |
| 886 | if ((unsigned)fd2 < _NHANDLE_) |
| 887 | return 1; |
| 888 | else |
| 889 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 890 | } |
| 891 | #else |
| 892 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 893 | #define _PyVerify_fd_dup2(A, B) (1) |
| 894 | #endif |
| 895 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 896 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 897 | /* The following structure was copied from |
| 898 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 899 | include doesn't seem to be present in the Windows SDK (at least as included |
| 900 | with Visual Studio Express). */ |
| 901 | typedef struct _REPARSE_DATA_BUFFER { |
| 902 | ULONG ReparseTag; |
| 903 | USHORT ReparseDataLength; |
| 904 | USHORT Reserved; |
| 905 | union { |
| 906 | struct { |
| 907 | USHORT SubstituteNameOffset; |
| 908 | USHORT SubstituteNameLength; |
| 909 | USHORT PrintNameOffset; |
| 910 | USHORT PrintNameLength; |
| 911 | ULONG Flags; |
| 912 | WCHAR PathBuffer[1]; |
| 913 | } SymbolicLinkReparseBuffer; |
| 914 | |
| 915 | struct { |
| 916 | USHORT SubstituteNameOffset; |
| 917 | USHORT SubstituteNameLength; |
| 918 | USHORT PrintNameOffset; |
| 919 | USHORT PrintNameLength; |
| 920 | WCHAR PathBuffer[1]; |
| 921 | } MountPointReparseBuffer; |
| 922 | |
| 923 | struct { |
| 924 | UCHAR DataBuffer[1]; |
| 925 | } GenericReparseBuffer; |
| 926 | }; |
| 927 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 928 | |
| 929 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 930 | GenericReparseBuffer) |
| 931 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 932 | |
| 933 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 934 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 935 | { |
| 936 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 937 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 938 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 939 | |
| 940 | if (0 == DeviceIoControl( |
| 941 | reparse_point_handle, |
| 942 | FSCTL_GET_REPARSE_POINT, |
| 943 | NULL, 0, /* in buffer */ |
| 944 | target_buffer, sizeof(target_buffer), |
| 945 | &n_bytes_returned, |
| 946 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 947 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 948 | |
| 949 | if (reparse_tag) |
| 950 | *reparse_tag = rdb->ReparseTag; |
| 951 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 952 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 953 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 954 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 955 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 956 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 957 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 958 | #ifdef WITH_NEXT_FRAMEWORK |
| 959 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 960 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 961 | */ |
| 962 | #include <crt_externs.h> |
| 963 | static char **environ; |
| 964 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 965 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 966 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 967 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 968 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 969 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 970 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 971 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 972 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 973 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 974 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 975 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 976 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 977 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 978 | APIRET rc; |
| 979 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 980 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 981 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 982 | d = PyDict_New(); |
| 983 | if (d == NULL) |
| 984 | return NULL; |
| 985 | #ifdef WITH_NEXT_FRAMEWORK |
| 986 | if (environ == NULL) |
| 987 | environ = *_NSGetEnviron(); |
| 988 | #endif |
| 989 | #ifdef MS_WINDOWS |
| 990 | /* _wenviron must be initialized in this way if the program is started |
| 991 | through main() instead of wmain(). */ |
| 992 | _wgetenv(L""); |
| 993 | if (_wenviron == NULL) |
| 994 | return d; |
| 995 | /* This part ignores errors */ |
| 996 | for (e = _wenviron; *e != NULL; e++) { |
| 997 | PyObject *k; |
| 998 | PyObject *v; |
| 999 | wchar_t *p = wcschr(*e, L'='); |
| 1000 | if (p == NULL) |
| 1001 | continue; |
| 1002 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1003 | if (k == NULL) { |
| 1004 | PyErr_Clear(); |
| 1005 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1006 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1007 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1008 | if (v == NULL) { |
| 1009 | PyErr_Clear(); |
| 1010 | Py_DECREF(k); |
| 1011 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1012 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1013 | if (PyDict_GetItem(d, k) == NULL) { |
| 1014 | if (PyDict_SetItem(d, k, v) != 0) |
| 1015 | PyErr_Clear(); |
| 1016 | } |
| 1017 | Py_DECREF(k); |
| 1018 | Py_DECREF(v); |
| 1019 | } |
| 1020 | #else |
| 1021 | if (environ == NULL) |
| 1022 | return d; |
| 1023 | /* This part ignores errors */ |
| 1024 | for (e = environ; *e != NULL; e++) { |
| 1025 | PyObject *k; |
| 1026 | PyObject *v; |
| 1027 | char *p = strchr(*e, '='); |
| 1028 | if (p == NULL) |
| 1029 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1030 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1031 | if (k == NULL) { |
| 1032 | PyErr_Clear(); |
| 1033 | continue; |
| 1034 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1035 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1036 | if (v == NULL) { |
| 1037 | PyErr_Clear(); |
| 1038 | Py_DECREF(k); |
| 1039 | continue; |
| 1040 | } |
| 1041 | if (PyDict_GetItem(d, k) == NULL) { |
| 1042 | if (PyDict_SetItem(d, k, v) != 0) |
| 1043 | PyErr_Clear(); |
| 1044 | } |
| 1045 | Py_DECREF(k); |
| 1046 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1047 | } |
| 1048 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1049 | #if defined(PYOS_OS2) |
| 1050 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 1051 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 1052 | PyObject *v = PyBytes_FromString(buffer); |
| 1053 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 1054 | Py_DECREF(v); |
| 1055 | } |
| 1056 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 1057 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 1058 | PyObject *v = PyBytes_FromString(buffer); |
| 1059 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 1060 | Py_DECREF(v); |
| 1061 | } |
| 1062 | #endif |
| 1063 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1066 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1067 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1068 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1069 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1070 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1071 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1072 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1073 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1074 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1075 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1076 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1079 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1080 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1081 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1082 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 1083 | PyObject *name_str, *rc; |
| 1084 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 1085 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1086 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 1087 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 1088 | name_str); |
| 1089 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1090 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1093 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1094 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1095 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1096 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1097 | /* XXX We should pass the function name along in the future. |
| 1098 | (winreg.c also wants to pass the function name.) |
| 1099 | This would however require an additional param to the |
| 1100 | Windows error object, which is non-trivial. |
| 1101 | */ |
| 1102 | errno = GetLastError(); |
| 1103 | if (filename) |
| 1104 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1105 | else |
| 1106 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1107 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1108 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1109 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1110 | win32_error_unicode(char* function, wchar_t* filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1111 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1112 | /* XXX - see win32_error for comments on 'function' */ |
| 1113 | errno = GetLastError(); |
| 1114 | if (filename) |
| 1115 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 1116 | else |
| 1117 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1120 | static PyObject * |
| 1121 | win32_error_object(char* function, PyObject* filename) |
| 1122 | { |
| 1123 | /* XXX - see win32_error for comments on 'function' */ |
| 1124 | errno = GetLastError(); |
| 1125 | if (filename) |
| 1126 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1127 | PyExc_WindowsError, |
| 1128 | errno, |
| 1129 | filename); |
| 1130 | else |
| 1131 | return PyErr_SetFromWindowsErr(errno); |
| 1132 | } |
| 1133 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1134 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1135 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1136 | /* |
| 1137 | * Some functions return Win32 errors, others only ever use posix_error |
| 1138 | * (this is for backwards compatibility with exceptions) |
| 1139 | */ |
| 1140 | static PyObject * |
| 1141 | path_posix_error(char *function_name, path_t *path) |
| 1142 | { |
| 1143 | if (path->narrow) |
| 1144 | return posix_error_with_filename(path->narrow); |
| 1145 | return posix_error(); |
| 1146 | } |
| 1147 | |
| 1148 | static PyObject * |
| 1149 | path_error(char *function_name, path_t *path) |
| 1150 | { |
| 1151 | #ifdef MS_WINDOWS |
| 1152 | if (path->narrow) |
| 1153 | return win32_error(function_name, path->narrow); |
| 1154 | if (path->wide) |
| 1155 | return win32_error_unicode(function_name, path->wide); |
| 1156 | return win32_error(function_name, NULL); |
| 1157 | #else |
| 1158 | return path_posix_error(function_name, path); |
| 1159 | #endif |
| 1160 | } |
| 1161 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1162 | #if defined(PYOS_OS2) |
| 1163 | /********************************************************************** |
| 1164 | * Helper Function to Trim and Format OS/2 Messages |
| 1165 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1166 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1167 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 1168 | { |
| 1169 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 1170 | |
| 1171 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 1172 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 1173 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 1174 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1175 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 1176 | } |
| 1177 | |
| 1178 | /* Add Optional Reason Text */ |
| 1179 | if (reason) { |
| 1180 | strcat(msgbuf, " : "); |
| 1181 | strcat(msgbuf, reason); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | /********************************************************************** |
| 1186 | * Decode an OS/2 Operating System Error Code |
| 1187 | * |
| 1188 | * A convenience function to lookup an OS/2 error code and return a |
| 1189 | * text message we can use to raise a Python exception. |
| 1190 | * |
| 1191 | * Notes: |
| 1192 | * The messages for errors returned from the OS/2 kernel reside in |
| 1193 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 1194 | * |
| 1195 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1196 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1197 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 1198 | { |
| 1199 | APIRET rc; |
| 1200 | ULONG msglen; |
| 1201 | |
| 1202 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 1203 | Py_BEGIN_ALLOW_THREADS |
| 1204 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 1205 | errorcode, "oso001.msg", &msglen); |
| 1206 | Py_END_ALLOW_THREADS |
| 1207 | |
| 1208 | if (rc == NO_ERROR) |
| 1209 | os2_formatmsg(msgbuf, msglen, reason); |
| 1210 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 1211 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1212 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1213 | |
| 1214 | return msgbuf; |
| 1215 | } |
| 1216 | |
| 1217 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 1218 | errors are not in a global variable e.g. 'errno' nor are |
| 1219 | they congruent with posix error numbers. */ |
| 1220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1221 | static PyObject * |
| 1222 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1223 | { |
| 1224 | char text[1024]; |
| 1225 | PyObject *v; |
| 1226 | |
| 1227 | os2_strerror(text, sizeof(text), code, ""); |
| 1228 | |
| 1229 | v = Py_BuildValue("(is)", code, text); |
| 1230 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 1231 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1232 | Py_DECREF(v); |
| 1233 | } |
| 1234 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 1235 | } |
| 1236 | |
| 1237 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1238 | |
| 1239 | /* POSIX generic methods */ |
| 1240 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1241 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1242 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1243 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1244 | int fd; |
| 1245 | int res; |
| 1246 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1247 | if (fd < 0) |
| 1248 | return NULL; |
| 1249 | if (!_PyVerify_fd(fd)) |
| 1250 | return posix_error(); |
| 1251 | Py_BEGIN_ALLOW_THREADS |
| 1252 | res = (*func)(fd); |
| 1253 | Py_END_ALLOW_THREADS |
| 1254 | if (res < 0) |
| 1255 | return posix_error(); |
| 1256 | Py_INCREF(Py_None); |
| 1257 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1258 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1259 | |
| 1260 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1261 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1262 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1263 | PyObject *opath1 = NULL; |
| 1264 | char *path1; |
| 1265 | int res; |
| 1266 | if (!PyArg_ParseTuple(args, format, |
| 1267 | PyUnicode_FSConverter, &opath1)) |
| 1268 | return NULL; |
| 1269 | path1 = PyBytes_AsString(opath1); |
| 1270 | Py_BEGIN_ALLOW_THREADS |
| 1271 | res = (*func)(path1); |
| 1272 | Py_END_ALLOW_THREADS |
| 1273 | if (res < 0) |
| 1274 | return posix_error_with_allocated_filename(opath1); |
| 1275 | Py_DECREF(opath1); |
| 1276 | Py_INCREF(Py_None); |
| 1277 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1280 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1281 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1282 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1283 | win32_1str(PyObject* args, char* func, |
| 1284 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 1285 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1286 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1287 | PyObject *uni; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1288 | const char *ansi; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1289 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1290 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1291 | if (PyArg_ParseTuple(args, wformat, &uni)) |
| 1292 | { |
| 1293 | wchar_t *wstr = PyUnicode_AsUnicode(uni); |
| 1294 | if (wstr == NULL) |
| 1295 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1296 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1297 | result = funcW(wstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1298 | Py_END_ALLOW_THREADS |
| 1299 | if (!result) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1300 | return win32_error_object(func, uni); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1301 | Py_INCREF(Py_None); |
| 1302 | return Py_None; |
| 1303 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1304 | PyErr_Clear(); |
| 1305 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1306 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 1307 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1308 | if (win32_warn_bytes_api()) |
| 1309 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1310 | Py_BEGIN_ALLOW_THREADS |
| 1311 | result = funcA(ansi); |
| 1312 | Py_END_ALLOW_THREADS |
| 1313 | if (!result) |
| 1314 | return win32_error(func, ansi); |
| 1315 | Py_INCREF(Py_None); |
| 1316 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1317 | |
| 1318 | } |
| 1319 | |
| 1320 | /* This is a reimplementation of the C library's chdir function, |
| 1321 | but one that produces Win32 errors instead of DOS error codes. |
| 1322 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1323 | it also needs to set "magic" environment variables indicating |
| 1324 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1325 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1326 | win32_chdir(LPCSTR path) |
| 1327 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1328 | char new_path[MAX_PATH+1]; |
| 1329 | int result; |
| 1330 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1331 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1332 | if(!SetCurrentDirectoryA(path)) |
| 1333 | return FALSE; |
| 1334 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 1335 | if (!result) |
| 1336 | return FALSE; |
| 1337 | /* In the ANSI API, there should not be any paths longer |
| 1338 | than MAX_PATH. */ |
| 1339 | assert(result <= MAX_PATH+1); |
| 1340 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1341 | strncmp(new_path, "//", 2) == 0) |
| 1342 | /* UNC path, nothing to do. */ |
| 1343 | return TRUE; |
| 1344 | env[1] = new_path[0]; |
| 1345 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /* The Unicode version differs from the ANSI version |
| 1349 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1350 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1351 | win32_wchdir(LPCWSTR path) |
| 1352 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1353 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 1354 | int result; |
| 1355 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1357 | if(!SetCurrentDirectoryW(path)) |
| 1358 | return FALSE; |
| 1359 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 1360 | if (!result) |
| 1361 | return FALSE; |
| 1362 | if (result > MAX_PATH+1) { |
| 1363 | new_path = malloc(result * sizeof(wchar_t)); |
| 1364 | if (!new_path) { |
| 1365 | SetLastError(ERROR_OUTOFMEMORY); |
| 1366 | return FALSE; |
| 1367 | } |
| 1368 | result = GetCurrentDirectoryW(result, new_path); |
| 1369 | if (!result) { |
| 1370 | free(new_path); |
| 1371 | return FALSE; |
| 1372 | } |
| 1373 | } |
| 1374 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1375 | wcsncmp(new_path, L"//", 2) == 0) |
| 1376 | /* UNC path, nothing to do. */ |
| 1377 | return TRUE; |
| 1378 | env[1] = new_path[0]; |
| 1379 | result = SetEnvironmentVariableW(env, new_path); |
| 1380 | if (new_path != _new_path) |
| 1381 | free(new_path); |
| 1382 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1383 | } |
| 1384 | #endif |
| 1385 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1386 | #ifdef MS_WINDOWS |
| 1387 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1388 | - time stamps are restricted to second resolution |
| 1389 | - file modification times suffer from forth-and-back conversions between |
| 1390 | UTC and local time |
| 1391 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1392 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1393 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1394 | |
| 1395 | struct win32_stat{ |
| 1396 | int st_dev; |
| 1397 | __int64 st_ino; |
| 1398 | unsigned short st_mode; |
| 1399 | int st_nlink; |
| 1400 | int st_uid; |
| 1401 | int st_gid; |
| 1402 | int st_rdev; |
| 1403 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1404 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1405 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1406 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1407 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1408 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1409 | int st_ctime_nsec; |
| 1410 | }; |
| 1411 | |
| 1412 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1413 | |
| 1414 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1415 | 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] | 1416 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1417 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1418 | /* Cannot simply cast and dereference in_ptr, |
| 1419 | since it might not be aligned properly */ |
| 1420 | __int64 in; |
| 1421 | memcpy(&in, in_ptr, sizeof(in)); |
| 1422 | *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] | 1423 | *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] | 1424 | } |
| 1425 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1426 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1427 | 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] | 1428 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1429 | /* XXX endianness */ |
| 1430 | __int64 out; |
| 1431 | out = time_in + secs_between_epochs; |
| 1432 | out = out * 10000000 + nsec_in / 100; |
| 1433 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1436 | /* Below, we *know* that ugo+r is 0444 */ |
| 1437 | #if _S_IREAD != 0400 |
| 1438 | #error Unsupported C library |
| 1439 | #endif |
| 1440 | static int |
| 1441 | attributes_to_mode(DWORD attr) |
| 1442 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | int m = 0; |
| 1444 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1445 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1446 | else |
| 1447 | m |= _S_IFREG; |
| 1448 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1449 | m |= 0444; |
| 1450 | else |
| 1451 | m |= 0666; |
| 1452 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1456 | 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] | 1457 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1458 | memset(result, 0, sizeof(*result)); |
| 1459 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1460 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1461 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1462 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1463 | 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] | 1464 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1465 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1466 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1467 | /* first clear the S_IFMT bits */ |
| 1468 | result->st_mode ^= (result->st_mode & 0170000); |
| 1469 | /* now set the bits that make this a symlink */ |
| 1470 | result->st_mode |= 0120000; |
| 1471 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1472 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1473 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1476 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1477 | 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] | 1478 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1479 | HANDLE hFindFile; |
| 1480 | WIN32_FIND_DATAA FileData; |
| 1481 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1482 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1483 | return FALSE; |
| 1484 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1485 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1486 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1487 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1488 | info->ftCreationTime = FileData.ftCreationTime; |
| 1489 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1490 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1491 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1492 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1493 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1494 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1495 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1496 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1500 | 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] | 1501 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1502 | HANDLE hFindFile; |
| 1503 | WIN32_FIND_DATAW FileData; |
| 1504 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1505 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1506 | return FALSE; |
| 1507 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1508 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1509 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1510 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1511 | info->ftCreationTime = FileData.ftCreationTime; |
| 1512 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1513 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1514 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1515 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1516 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1517 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1518 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1519 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1522 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1523 | static int has_GetFinalPathNameByHandle = 0; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1524 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1525 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1526 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1527 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1528 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1529 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1530 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1531 | DWORD); |
| 1532 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1533 | /* only recheck */ |
| 1534 | if (!has_GetFinalPathNameByHandle) |
| 1535 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1536 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1537 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1538 | "GetFinalPathNameByHandleA"); |
| 1539 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1540 | "GetFinalPathNameByHandleW"); |
| 1541 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1542 | Py_GetFinalPathNameByHandleW; |
| 1543 | } |
| 1544 | return has_GetFinalPathNameByHandle; |
| 1545 | } |
| 1546 | |
| 1547 | static BOOL |
| 1548 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1549 | { |
| 1550 | int buf_size, result_length; |
| 1551 | wchar_t *buf; |
| 1552 | |
| 1553 | /* We have a good handle to the target, use it to determine |
| 1554 | the target path name (then we'll call lstat on it). */ |
| 1555 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1556 | VOLUME_NAME_DOS); |
| 1557 | if(!buf_size) |
| 1558 | return FALSE; |
| 1559 | |
| 1560 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1561 | if (!buf) { |
| 1562 | SetLastError(ERROR_OUTOFMEMORY); |
| 1563 | return FALSE; |
| 1564 | } |
| 1565 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1566 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1567 | buf, buf_size, VOLUME_NAME_DOS); |
| 1568 | |
| 1569 | if(!result_length) { |
| 1570 | free(buf); |
| 1571 | return FALSE; |
| 1572 | } |
| 1573 | |
| 1574 | if(!CloseHandle(hdl)) { |
| 1575 | free(buf); |
| 1576 | return FALSE; |
| 1577 | } |
| 1578 | |
| 1579 | buf[result_length] = 0; |
| 1580 | |
| 1581 | *target_path = buf; |
| 1582 | return TRUE; |
| 1583 | } |
| 1584 | |
| 1585 | static int |
| 1586 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1587 | BOOL traverse); |
| 1588 | static int |
| 1589 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1590 | BOOL traverse) |
| 1591 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1592 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1593 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1594 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1595 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1596 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1597 | const char *dot; |
| 1598 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1599 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1600 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1601 | traverse reparse point. */ |
| 1602 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1605 | hFile = CreateFileA( |
| 1606 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1607 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1608 | 0, /* share mode */ |
| 1609 | NULL, /* security attributes */ |
| 1610 | OPEN_EXISTING, |
| 1611 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1612 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1613 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1614 | the symlink path agin and not the actual final path. */ |
| 1615 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1616 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1617 | NULL); |
| 1618 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1619 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1620 | /* Either the target doesn't exist, or we don't have access to |
| 1621 | get a handle to it. If the former, we need to return an error. |
| 1622 | If the latter, we can use attributes_from_dir. */ |
| 1623 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1624 | return -1; |
| 1625 | /* Could not get attributes on open file. Fall back to |
| 1626 | reading the directory. */ |
| 1627 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1628 | /* Very strange. This should not fail now */ |
| 1629 | return -1; |
| 1630 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1631 | if (traverse) { |
| 1632 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1633 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1634 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1635 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1636 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1637 | } else { |
| 1638 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1639 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1640 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1641 | } |
| 1642 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1643 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1644 | return -1; |
| 1645 | |
| 1646 | /* Close the outer open file handle now that we're about to |
| 1647 | reopen it with different flags. */ |
| 1648 | if (!CloseHandle(hFile)) |
| 1649 | return -1; |
| 1650 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1651 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1652 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1653 | the file without the reparse handling flag set. */ |
| 1654 | hFile2 = CreateFileA( |
| 1655 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1656 | NULL, OPEN_EXISTING, |
| 1657 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1658 | NULL); |
| 1659 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1660 | return -1; |
| 1661 | |
| 1662 | if (!get_target_path(hFile2, &target_path)) |
| 1663 | return -1; |
| 1664 | |
| 1665 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1666 | free(target_path); |
| 1667 | return code; |
| 1668 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1669 | } else |
| 1670 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1671 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1672 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1673 | |
| 1674 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1675 | dot = strrchr(path, '.'); |
| 1676 | if (dot) { |
| 1677 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1678 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1679 | result->st_mode |= 0111; |
| 1680 | } |
| 1681 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1685 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1686 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1687 | { |
| 1688 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1689 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1690 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1691 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1692 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1693 | const wchar_t *dot; |
| 1694 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1695 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1696 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1697 | traverse reparse point. */ |
| 1698 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1701 | hFile = CreateFileW( |
| 1702 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1703 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1704 | 0, /* share mode */ |
| 1705 | NULL, /* security attributes */ |
| 1706 | OPEN_EXISTING, |
| 1707 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1708 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1709 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1710 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1711 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1712 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1713 | NULL); |
| 1714 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1715 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1716 | /* Either the target doesn't exist, or we don't have access to |
| 1717 | get a handle to it. If the former, we need to return an error. |
| 1718 | If the latter, we can use attributes_from_dir. */ |
| 1719 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1720 | return -1; |
| 1721 | /* Could not get attributes on open file. Fall back to |
| 1722 | reading the directory. */ |
| 1723 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1724 | /* Very strange. This should not fail now */ |
| 1725 | return -1; |
| 1726 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1727 | if (traverse) { |
| 1728 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1729 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1730 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1731 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1732 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1733 | } else { |
| 1734 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1735 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1736 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1737 | } |
| 1738 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1739 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1740 | return -1; |
| 1741 | |
| 1742 | /* Close the outer open file handle now that we're about to |
| 1743 | reopen it with different flags. */ |
| 1744 | if (!CloseHandle(hFile)) |
| 1745 | return -1; |
| 1746 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1747 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1748 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1749 | the file without the reparse handling flag set. */ |
| 1750 | hFile2 = CreateFileW( |
| 1751 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1752 | NULL, OPEN_EXISTING, |
| 1753 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1754 | NULL); |
| 1755 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1756 | return -1; |
| 1757 | |
| 1758 | if (!get_target_path(hFile2, &target_path)) |
| 1759 | return -1; |
| 1760 | |
| 1761 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1762 | free(target_path); |
| 1763 | return code; |
| 1764 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1765 | } else |
| 1766 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1767 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1768 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1769 | |
| 1770 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1771 | dot = wcsrchr(path, '.'); |
| 1772 | if (dot) { |
| 1773 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1774 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1775 | result->st_mode |= 0111; |
| 1776 | } |
| 1777 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1781 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1782 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1783 | /* Protocol violation: we explicitly clear errno, instead of |
| 1784 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1785 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1786 | errno = 0; |
| 1787 | return code; |
| 1788 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1789 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1790 | static int |
| 1791 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1792 | { |
| 1793 | /* Protocol violation: we explicitly clear errno, instead of |
| 1794 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1795 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1796 | errno = 0; |
| 1797 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1798 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1799 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1800 | |
| 1801 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1802 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1803 | default does not traverse symlinks and instead returns attributes for |
| 1804 | the symlink. |
| 1805 | |
| 1806 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1807 | win32_stat will first explicitly resolve the symlink target and then will |
| 1808 | call win32_lstat on that result. |
| 1809 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1810 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1811 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1812 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1813 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1814 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1815 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1818 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1819 | 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] | 1820 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1821 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | static int |
| 1825 | win32_stat(const char* path, struct win32_stat *result) |
| 1826 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1827 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1830 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1831 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1832 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1833 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | static int |
| 1837 | win32_fstat(int file_number, struct win32_stat *result) |
| 1838 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1839 | BY_HANDLE_FILE_INFORMATION info; |
| 1840 | HANDLE h; |
| 1841 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1842 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 1843 | if (!_PyVerify_fd(file_number)) |
| 1844 | h = INVALID_HANDLE_VALUE; |
| 1845 | else |
| 1846 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1847 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1848 | /* Protocol violation: we explicitly clear errno, instead of |
| 1849 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1850 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1851 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1852 | if (h == INVALID_HANDLE_VALUE) { |
| 1853 | /* This is really a C library error (invalid file handle). |
| 1854 | We set the Win32 error to the closes one matching. */ |
| 1855 | SetLastError(ERROR_INVALID_HANDLE); |
| 1856 | return -1; |
| 1857 | } |
| 1858 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1859 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1860 | type = GetFileType(h); |
| 1861 | if (type == FILE_TYPE_UNKNOWN) { |
| 1862 | DWORD error = GetLastError(); |
| 1863 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1864 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1865 | } |
| 1866 | /* else: valid but unknown file */ |
| 1867 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1868 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1869 | if (type != FILE_TYPE_DISK) { |
| 1870 | if (type == FILE_TYPE_CHAR) |
| 1871 | result->st_mode = _S_IFCHR; |
| 1872 | else if (type == FILE_TYPE_PIPE) |
| 1873 | result->st_mode = _S_IFIFO; |
| 1874 | return 0; |
| 1875 | } |
| 1876 | |
| 1877 | if (!GetFileInformationByHandle(h, &info)) { |
| 1878 | return -1; |
| 1879 | } |
| 1880 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1881 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1882 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1883 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1884 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | #endif /* MS_WINDOWS */ |
| 1888 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1889 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1890 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1891 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1892 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1893 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1894 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1895 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1896 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1897 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1898 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1899 | |
| 1900 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1901 | {"st_mode", "protection bits"}, |
| 1902 | {"st_ino", "inode"}, |
| 1903 | {"st_dev", "device"}, |
| 1904 | {"st_nlink", "number of hard links"}, |
| 1905 | {"st_uid", "user ID of owner"}, |
| 1906 | {"st_gid", "group ID of owner"}, |
| 1907 | {"st_size", "total size, in bytes"}, |
| 1908 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1909 | {NULL, "integer time of last access"}, |
| 1910 | {NULL, "integer time of last modification"}, |
| 1911 | {NULL, "integer time of last change"}, |
| 1912 | {"st_atime", "time of last access"}, |
| 1913 | {"st_mtime", "time of last modification"}, |
| 1914 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1915 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1916 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1917 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1918 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1919 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1920 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1921 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1922 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1923 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1924 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1925 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1926 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1927 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1928 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1929 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1930 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1931 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1932 | #endif |
| 1933 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1934 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1935 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1936 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1937 | }; |
| 1938 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1939 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1940 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1941 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1942 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1943 | #endif |
| 1944 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1945 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1946 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1947 | #else |
| 1948 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1949 | #endif |
| 1950 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1951 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1952 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1953 | #else |
| 1954 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1955 | #endif |
| 1956 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1957 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1958 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1959 | #else |
| 1960 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1961 | #endif |
| 1962 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1963 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1964 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1965 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1966 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1967 | #endif |
| 1968 | |
| 1969 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1970 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1971 | #else |
| 1972 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1973 | #endif |
| 1974 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1975 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1976 | "stat_result", /* name */ |
| 1977 | stat_result__doc__, /* doc */ |
| 1978 | stat_result_fields, |
| 1979 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1980 | }; |
| 1981 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1982 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1983 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1984 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1985 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1986 | 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] | 1987 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1988 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1989 | |
| 1990 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1991 | {"f_bsize", }, |
| 1992 | {"f_frsize", }, |
| 1993 | {"f_blocks", }, |
| 1994 | {"f_bfree", }, |
| 1995 | {"f_bavail", }, |
| 1996 | {"f_files", }, |
| 1997 | {"f_ffree", }, |
| 1998 | {"f_favail", }, |
| 1999 | {"f_flag", }, |
| 2000 | {"f_namemax",}, |
| 2001 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2002 | }; |
| 2003 | |
| 2004 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2005 | "statvfs_result", /* name */ |
| 2006 | statvfs_result__doc__, /* doc */ |
| 2007 | statvfs_result_fields, |
| 2008 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2009 | }; |
| 2010 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2011 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2012 | PyDoc_STRVAR(waitid_result__doc__, |
| 2013 | "waitid_result: Result from waitid.\n\n\ |
| 2014 | This object may be accessed either as a tuple of\n\ |
| 2015 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2016 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2017 | \n\ |
| 2018 | See os.waitid for more information."); |
| 2019 | |
| 2020 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2021 | {"si_pid", }, |
| 2022 | {"si_uid", }, |
| 2023 | {"si_signo", }, |
| 2024 | {"si_status", }, |
| 2025 | {"si_code", }, |
| 2026 | {0} |
| 2027 | }; |
| 2028 | |
| 2029 | static PyStructSequence_Desc waitid_result_desc = { |
| 2030 | "waitid_result", /* name */ |
| 2031 | waitid_result__doc__, /* doc */ |
| 2032 | waitid_result_fields, |
| 2033 | 5 |
| 2034 | }; |
| 2035 | static PyTypeObject WaitidResultType; |
| 2036 | #endif |
| 2037 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2038 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2039 | static PyTypeObject StatResultType; |
| 2040 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2041 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 2042 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2043 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2044 | static newfunc structseq_new; |
| 2045 | |
| 2046 | static PyObject * |
| 2047 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2048 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2049 | PyStructSequence *result; |
| 2050 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2051 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2052 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2053 | if (!result) |
| 2054 | return NULL; |
| 2055 | /* If we have been initialized from a tuple, |
| 2056 | st_?time might be set to None. Initialize it |
| 2057 | from the int slots. */ |
| 2058 | for (i = 7; i <= 9; i++) { |
| 2059 | if (result->ob_item[i+3] == Py_None) { |
| 2060 | Py_DECREF(Py_None); |
| 2061 | Py_INCREF(result->ob_item[i]); |
| 2062 | result->ob_item[i+3] = result->ob_item[i]; |
| 2063 | } |
| 2064 | } |
| 2065 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | |
| 2069 | |
| 2070 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 2071 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2072 | |
| 2073 | PyDoc_STRVAR(stat_float_times__doc__, |
| 2074 | "stat_float_times([newval]) -> oldval\n\n\ |
| 2075 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 2076 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 2077 | future calls return ints. \n\ |
| 2078 | If newval is omitted, return the current setting.\n"); |
| 2079 | |
| 2080 | static PyObject* |
| 2081 | stat_float_times(PyObject* self, PyObject *args) |
| 2082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2083 | int newval = -1; |
| 2084 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 2085 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 2086 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2087 | "stat_float_times() is deprecated", |
| 2088 | 1)) |
| 2089 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2090 | if (newval == -1) |
| 2091 | /* Return old value */ |
| 2092 | return PyBool_FromLong(_stat_float_times); |
| 2093 | _stat_float_times = newval; |
| 2094 | Py_INCREF(Py_None); |
| 2095 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2096 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2097 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2098 | static PyObject *billion = NULL; |
| 2099 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2100 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2101 | 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] | 2102 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2103 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2104 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2105 | PyObject *s_in_ns = NULL; |
| 2106 | PyObject *ns_total = NULL; |
| 2107 | PyObject *float_s = NULL; |
| 2108 | |
| 2109 | if (!(s && ns_fractional)) |
| 2110 | goto exit; |
| 2111 | |
| 2112 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2113 | if (!s_in_ns) |
| 2114 | goto exit; |
| 2115 | |
| 2116 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2117 | if (!ns_total) |
| 2118 | goto exit; |
| 2119 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2120 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2121 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2122 | if (!float_s) |
| 2123 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2124 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2125 | else { |
| 2126 | float_s = s; |
| 2127 | Py_INCREF(float_s); |
| 2128 | } |
| 2129 | |
| 2130 | PyStructSequence_SET_ITEM(v, index, s); |
| 2131 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2132 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2133 | s = NULL; |
| 2134 | float_s = NULL; |
| 2135 | ns_total = NULL; |
| 2136 | exit: |
| 2137 | Py_XDECREF(s); |
| 2138 | Py_XDECREF(ns_fractional); |
| 2139 | Py_XDECREF(s_in_ns); |
| 2140 | Py_XDECREF(ns_total); |
| 2141 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2144 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2145 | (used by posix_stat() and posix_fstat()) */ |
| 2146 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2147 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2148 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2149 | unsigned long ansec, mnsec, cnsec; |
| 2150 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2151 | if (v == NULL) |
| 2152 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2153 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2154 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2155 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2156 | PyStructSequence_SET_ITEM(v, 1, |
| 2157 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2158 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2159 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2160 | #endif |
| 2161 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2162 | PyStructSequence_SET_ITEM(v, 2, |
| 2163 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2164 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2165 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2166 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2167 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 2168 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 2169 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2170 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2171 | PyStructSequence_SET_ITEM(v, 6, |
| 2172 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2173 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2174 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2175 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2176 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2177 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2178 | ansec = st->st_atim.tv_nsec; |
| 2179 | mnsec = st->st_mtim.tv_nsec; |
| 2180 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2181 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2182 | ansec = st->st_atimespec.tv_nsec; |
| 2183 | mnsec = st->st_mtimespec.tv_nsec; |
| 2184 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2185 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2186 | ansec = st->st_atime_nsec; |
| 2187 | mnsec = st->st_mtime_nsec; |
| 2188 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2189 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2190 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2191 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2192 | fill_time(v, 7, st->st_atime, ansec); |
| 2193 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2194 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2195 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2196 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2197 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2198 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2199 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2200 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2201 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2202 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2203 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2204 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2205 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2206 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2207 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2208 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2209 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2210 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2211 | #endif |
| 2212 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2213 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2214 | PyObject *val; |
| 2215 | unsigned long bsec,bnsec; |
| 2216 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2217 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2218 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2219 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2220 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2221 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2222 | if (_stat_float_times) { |
| 2223 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2224 | } else { |
| 2225 | val = PyLong_FromLong((long)bsec); |
| 2226 | } |
| 2227 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2228 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2229 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2230 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2231 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2232 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2233 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2234 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2235 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2236 | if (PyErr_Occurred()) { |
| 2237 | Py_DECREF(v); |
| 2238 | return NULL; |
| 2239 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2240 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2241 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2244 | /* POSIX methods */ |
| 2245 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2246 | |
| 2247 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2248 | posix_do_stat(char *function_name, path_t *path, |
| 2249 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2250 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2251 | STRUCT_STAT st; |
| 2252 | int result; |
| 2253 | |
| 2254 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2255 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2256 | return NULL; |
| 2257 | #endif |
| 2258 | |
| 2259 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2260 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2261 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2262 | return NULL; |
| 2263 | |
| 2264 | Py_BEGIN_ALLOW_THREADS |
| 2265 | if (path->fd != -1) |
| 2266 | result = FSTAT(path->fd, &st); |
| 2267 | else |
| 2268 | #ifdef MS_WINDOWS |
| 2269 | if (path->wide) { |
| 2270 | if (follow_symlinks) |
| 2271 | result = win32_stat_w(path->wide, &st); |
| 2272 | else |
| 2273 | result = win32_lstat_w(path->wide, &st); |
| 2274 | } |
| 2275 | else |
| 2276 | #endif |
| 2277 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2278 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2279 | result = LSTAT(path->narrow, &st); |
| 2280 | else |
| 2281 | #endif |
| 2282 | #ifdef HAVE_FSTATAT |
| 2283 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2284 | result = fstatat(dir_fd, path->narrow, &st, |
| 2285 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2286 | else |
| 2287 | #endif |
| 2288 | result = STAT(path->narrow, &st); |
| 2289 | Py_END_ALLOW_THREADS |
| 2290 | |
| 2291 | if (result != 0) |
| 2292 | return path_error("stat", path); |
| 2293 | |
| 2294 | return _pystat_fromstructstat(&st); |
| 2295 | } |
| 2296 | |
| 2297 | PyDoc_STRVAR(posix_stat__doc__, |
| 2298 | "stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result\n\n\ |
| 2299 | Perform a stat system call on the given path.\n\ |
| 2300 | \n\ |
| 2301 | path may be specified as either a string or as an open file descriptor.\n\ |
| 2302 | \n\ |
| 2303 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2304 | and path should be relative; path will then be relative to that directory.\n\ |
| 2305 | dir_fd may not be supported on your platform; if it is unavailable, using\n\ |
| 2306 | it will raise a NotImplementedError.\n\ |
| 2307 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2308 | link, stat will examine the symbolic link itself instead of the file the\n\ |
| 2309 | link points to.\n\ |
| 2310 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2311 | an open file descriptor."); |
| 2312 | |
| 2313 | static PyObject * |
| 2314 | posix_stat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2315 | { |
| 2316 | static char *keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2317 | path_t path; |
| 2318 | int dir_fd = DEFAULT_DIR_FD; |
| 2319 | int follow_symlinks = 1; |
| 2320 | PyObject *return_value; |
| 2321 | |
| 2322 | memset(&path, 0, sizeof(path)); |
| 2323 | path.allow_fd = 1; |
| 2324 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords, |
| 2325 | path_converter, &path, |
| 2326 | #ifdef HAVE_FSTATAT |
| 2327 | dir_fd_converter, &dir_fd, |
| 2328 | #else |
| 2329 | dir_fd_unavailable, &dir_fd, |
| 2330 | #endif |
| 2331 | &follow_symlinks)) |
| 2332 | return NULL; |
| 2333 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2334 | path_cleanup(&path); |
| 2335 | return return_value; |
| 2336 | } |
| 2337 | |
| 2338 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2339 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2340 | Like stat(), but do not follow symbolic links.\n\ |
| 2341 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2342 | |
| 2343 | static PyObject * |
| 2344 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2345 | { |
| 2346 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2347 | path_t path; |
| 2348 | int dir_fd = DEFAULT_DIR_FD; |
| 2349 | int follow_symlinks = 0; |
| 2350 | PyObject *return_value; |
| 2351 | |
| 2352 | memset(&path, 0, sizeof(path)); |
| 2353 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2354 | path_converter, &path, |
| 2355 | #ifdef HAVE_FSTATAT |
| 2356 | dir_fd_converter, &dir_fd |
| 2357 | #else |
| 2358 | dir_fd_unavailable, &dir_fd |
| 2359 | #endif |
| 2360 | )) |
| 2361 | return NULL; |
| 2362 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2363 | path_cleanup(&path); |
| 2364 | return return_value; |
| 2365 | } |
| 2366 | |
| 2367 | PyDoc_STRVAR(posix_access__doc__, |
| 2368 | "access(path, mode, *, dir_fd=None, effective_ids=False,\ |
| 2369 | follow_symlinks=True)\n\n\ |
| 2370 | Use the real uid/gid to test for access to a path. Returns True if granted,\n\ |
| 2371 | False otherwise.\n\ |
| 2372 | \n\ |
| 2373 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2374 | and path should be relative; path will then be relative to that directory.\n\ |
| 2375 | If effective_ids is True, access will use the effective uid/gid instead of\n\ |
| 2376 | the real uid/gid.\n\ |
| 2377 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2378 | link, access will examine the symbolic link itself instead of the file the\n\ |
| 2379 | link points to.\n\ |
| 2380 | dir_fd, effective_ids, and follow_symlinks may not be implemented\n\ |
| 2381 | on your platform. If they are unavailable, using them will raise a\n\ |
| 2382 | NotImplementedError.\n\ |
| 2383 | \n\ |
| 2384 | Note that most operations will use the effective uid/gid, therefore this\n\ |
| 2385 | routine can be used in a suid/sgid environment to test if the invoking user\n\ |
| 2386 | has the specified access to the path.\n\ |
| 2387 | The mode argument can be F_OK to test existence, or the inclusive-OR\n\ |
| 2388 | of R_OK, W_OK, and X_OK."); |
| 2389 | |
| 2390 | static PyObject * |
| 2391 | posix_access(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2392 | { |
| 2393 | static char *keywords[] = {"path", "mode", "dir_fd", "effective_ids", |
| 2394 | "follow_symlinks", NULL}; |
| 2395 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2396 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2397 | int dir_fd = DEFAULT_DIR_FD; |
| 2398 | int effective_ids = 0; |
| 2399 | int follow_symlinks = 1; |
| 2400 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2401 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2402 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2403 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2404 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2405 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2406 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2407 | |
| 2408 | memset(&path, 0, sizeof(path)); |
| 2409 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", keywords, |
| 2410 | path_converter, &path, &mode, |
| 2411 | #ifdef HAVE_FACCESSAT |
| 2412 | dir_fd_converter, &dir_fd, |
| 2413 | #else |
| 2414 | dir_fd_unavailable, &dir_fd, |
| 2415 | #endif |
| 2416 | &effective_ids, &follow_symlinks)) |
| 2417 | return NULL; |
| 2418 | |
| 2419 | #ifndef HAVE_FACCESSAT |
| 2420 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2421 | goto exit; |
| 2422 | |
| 2423 | if (effective_ids) { |
| 2424 | argument_unavailable_error("access", "effective_ids"); |
| 2425 | goto exit; |
| 2426 | } |
| 2427 | #endif |
| 2428 | |
| 2429 | #ifdef MS_WINDOWS |
| 2430 | Py_BEGIN_ALLOW_THREADS |
| 2431 | if (path.wide != NULL) |
| 2432 | attr = GetFileAttributesW(path.wide); |
| 2433 | else |
| 2434 | attr = GetFileAttributesA(path.narrow); |
| 2435 | Py_END_ALLOW_THREADS |
| 2436 | |
| 2437 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2438 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2439 | * * we didn't get a -1, and |
| 2440 | * * write access wasn't requested, |
| 2441 | * * or the file isn't read-only, |
| 2442 | * * or it's a directory. |
| 2443 | * (Directories cannot be read-only on Windows.) |
| 2444 | */ |
| 2445 | return_value = PyBool_FromLong( |
| 2446 | (attr != 0xFFFFFFFF) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2447 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2448 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2449 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2450 | #else |
| 2451 | |
| 2452 | Py_BEGIN_ALLOW_THREADS |
| 2453 | #ifdef HAVE_FACCESSAT |
| 2454 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2455 | effective_ids || |
| 2456 | !follow_symlinks) { |
| 2457 | int flags = 0; |
| 2458 | if (!follow_symlinks) |
| 2459 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2460 | if (effective_ids) |
| 2461 | flags |= AT_EACCESS; |
| 2462 | result = faccessat(dir_fd, path.narrow, mode, flags); |
| 2463 | } |
| 2464 | else |
| 2465 | #endif |
| 2466 | result = access(path.narrow, mode); |
| 2467 | Py_END_ALLOW_THREADS |
| 2468 | return_value = PyBool_FromLong(!result); |
| 2469 | #endif |
| 2470 | |
| 2471 | #ifndef HAVE_FACCESSAT |
| 2472 | exit: |
| 2473 | #endif |
| 2474 | path_cleanup(&path); |
| 2475 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2478 | #ifndef F_OK |
| 2479 | #define F_OK 0 |
| 2480 | #endif |
| 2481 | #ifndef R_OK |
| 2482 | #define R_OK 4 |
| 2483 | #endif |
| 2484 | #ifndef W_OK |
| 2485 | #define W_OK 2 |
| 2486 | #endif |
| 2487 | #ifndef X_OK |
| 2488 | #define X_OK 1 |
| 2489 | #endif |
| 2490 | |
| 2491 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2492 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2493 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2494 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2495 | |
| 2496 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2497 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2498 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2499 | int id; |
| 2500 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2501 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2502 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 2503 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2504 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2505 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2506 | /* file descriptor 0 only, the default input device (stdin) */ |
| 2507 | if (id == 0) { |
| 2508 | ret = ttyname(); |
| 2509 | } |
| 2510 | else { |
| 2511 | ret = NULL; |
| 2512 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2513 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2514 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2515 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2516 | if (ret == NULL) |
| 2517 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2518 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2519 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2520 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2521 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2522 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2523 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2524 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2525 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2526 | |
| 2527 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2528 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2529 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2530 | char *ret; |
| 2531 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2532 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2533 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2534 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2535 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2536 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2537 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2538 | if (ret == NULL) |
| 2539 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2540 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2541 | } |
| 2542 | #endif |
| 2543 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2544 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2545 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2546 | Change the current working directory to the specified path.\n\ |
| 2547 | \n\ |
| 2548 | path may always be specified as a string.\n\ |
| 2549 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2550 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2551 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2552 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2553 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2554 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2555 | path_t path; |
| 2556 | int result; |
| 2557 | PyObject *return_value = NULL; |
| 2558 | static char *keywords[] = {"path", NULL}; |
| 2559 | |
| 2560 | memset(&path, 0, sizeof(path)); |
| 2561 | #ifdef HAVE_FCHDIR |
| 2562 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2563 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2564 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2565 | path_converter, &path |
| 2566 | )) |
| 2567 | return NULL; |
| 2568 | |
| 2569 | Py_BEGIN_ALLOW_THREADS |
| 2570 | #ifdef MS_WINDOWS |
| 2571 | if (path.wide) |
| 2572 | result = win32_wchdir(path.wide); |
| 2573 | else |
| 2574 | result = win32_chdir(path.narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2575 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2576 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2577 | result = _chdir2(path.narrow); |
| 2578 | #else |
| 2579 | #ifdef HAVE_FCHDIR |
| 2580 | if (path.fd != -1) |
| 2581 | result = fchdir(path.fd); |
| 2582 | else |
| 2583 | #endif |
| 2584 | result = chdir(path.narrow); |
| 2585 | #endif |
| 2586 | Py_END_ALLOW_THREADS |
| 2587 | |
| 2588 | if (result) { |
| 2589 | return_value = path_error("chdir", &path); |
| 2590 | goto exit; |
| 2591 | } |
| 2592 | |
| 2593 | return_value = Py_None; |
| 2594 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2595 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2596 | exit: |
| 2597 | path_cleanup(&path); |
| 2598 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2601 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2602 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2603 | "fchdir(fd)\n\n\ |
| 2604 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2605 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2606 | |
| 2607 | static PyObject * |
| 2608 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2609 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2610 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2611 | } |
| 2612 | #endif /* HAVE_FCHDIR */ |
| 2613 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2615 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2616 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2617 | Change the access permissions of a file.\n\ |
| 2618 | \n\ |
| 2619 | path may always be specified as a string.\n\ |
| 2620 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2621 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2622 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2623 | and path should be relative; path will then be relative to that directory.\n\ |
| 2624 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2625 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2626 | link points to.\n\ |
| 2627 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2628 | an open file descriptor.\n\ |
| 2629 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2630 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2631 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2632 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2633 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2634 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2635 | path_t path; |
| 2636 | int mode; |
| 2637 | int dir_fd = DEFAULT_DIR_FD; |
| 2638 | int follow_symlinks = 1; |
| 2639 | int result; |
| 2640 | PyObject *return_value = NULL; |
| 2641 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2642 | "follow_symlinks", NULL}; |
| 2643 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2644 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2645 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2646 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2647 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2648 | #ifdef HAVE_FCHMODAT |
| 2649 | int fchmodat_nofollow_unsupported = 0; |
| 2650 | #endif |
| 2651 | |
| 2652 | memset(&path, 0, sizeof(path)); |
| 2653 | #ifdef HAVE_FCHMOD |
| 2654 | path.allow_fd = 1; |
| 2655 | #endif |
| 2656 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2657 | path_converter, &path, |
| 2658 | &mode, |
| 2659 | #ifdef HAVE_FCHMODAT |
| 2660 | dir_fd_converter, &dir_fd, |
| 2661 | #else |
| 2662 | dir_fd_unavailable, &dir_fd, |
| 2663 | #endif |
| 2664 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2665 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2666 | |
| 2667 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2668 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2669 | goto exit; |
| 2670 | #endif |
| 2671 | |
| 2672 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2673 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2674 | if (path.wide) |
| 2675 | attr = GetFileAttributesW(path.wide); |
| 2676 | else |
| 2677 | attr = GetFileAttributesA(path.narrow); |
| 2678 | if (attr == 0xFFFFFFFF) |
| 2679 | result = 0; |
| 2680 | else { |
| 2681 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2682 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2683 | else |
| 2684 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2685 | if (path.wide) |
| 2686 | result = SetFileAttributesW(path.wide, attr); |
| 2687 | else |
| 2688 | result = SetFileAttributesA(path.narrow, attr); |
| 2689 | } |
| 2690 | Py_END_ALLOW_THREADS |
| 2691 | |
| 2692 | if (!result) { |
| 2693 | return_value = win32_error_object("chmod", path.object); |
| 2694 | goto exit; |
| 2695 | } |
| 2696 | #else /* MS_WINDOWS */ |
| 2697 | Py_BEGIN_ALLOW_THREADS |
| 2698 | #ifdef HAVE_FCHMOD |
| 2699 | if (path.fd != -1) |
| 2700 | result = fchmod(path.fd, mode); |
| 2701 | else |
| 2702 | #endif |
| 2703 | #ifdef HAVE_LCHMOD |
| 2704 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2705 | result = lchmod(path.narrow, mode); |
| 2706 | else |
| 2707 | #endif |
| 2708 | #ifdef HAVE_FCHMODAT |
| 2709 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2710 | /* |
| 2711 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2712 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2713 | * and then says it isn't implemented yet. |
| 2714 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2715 | * |
| 2716 | * Once it is supported, os.chmod will automatically |
| 2717 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2718 | * Until then, we need to be careful what exception we raise. |
| 2719 | */ |
| 2720 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2721 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2722 | /* |
| 2723 | * But wait! We can't throw the exception without allowing threads, |
| 2724 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2725 | */ |
| 2726 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2727 | result && |
| 2728 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2729 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2730 | } |
| 2731 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2732 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2733 | result = chmod(path.narrow, mode); |
| 2734 | Py_END_ALLOW_THREADS |
| 2735 | |
| 2736 | if (result) { |
| 2737 | #ifdef HAVE_FCHMODAT |
| 2738 | if (fchmodat_nofollow_unsupported) { |
| 2739 | if (dir_fd != DEFAULT_DIR_FD) |
| 2740 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2741 | dir_fd, follow_symlinks); |
| 2742 | else |
| 2743 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2744 | } |
| 2745 | else |
| 2746 | #endif |
| 2747 | return_value = path_error("chmod", &path); |
| 2748 | goto exit; |
| 2749 | } |
| 2750 | #endif |
| 2751 | |
| 2752 | Py_INCREF(Py_None); |
| 2753 | return_value = Py_None; |
| 2754 | exit: |
| 2755 | path_cleanup(&path); |
| 2756 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2759 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2760 | #ifdef HAVE_FCHMOD |
| 2761 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2762 | "fchmod(fd, mode)\n\n\ |
| 2763 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2764 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2765 | |
| 2766 | static PyObject * |
| 2767 | posix_fchmod(PyObject *self, PyObject *args) |
| 2768 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2769 | int fd, mode, res; |
| 2770 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2771 | return NULL; |
| 2772 | Py_BEGIN_ALLOW_THREADS |
| 2773 | res = fchmod(fd, mode); |
| 2774 | Py_END_ALLOW_THREADS |
| 2775 | if (res < 0) |
| 2776 | return posix_error(); |
| 2777 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2778 | } |
| 2779 | #endif /* HAVE_FCHMOD */ |
| 2780 | |
| 2781 | #ifdef HAVE_LCHMOD |
| 2782 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2783 | "lchmod(path, mode)\n\n\ |
| 2784 | 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] | 2785 | affects the link itself rather than the target.\n\ |
| 2786 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2787 | |
| 2788 | static PyObject * |
| 2789 | posix_lchmod(PyObject *self, PyObject *args) |
| 2790 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2791 | PyObject *opath; |
| 2792 | char *path; |
| 2793 | int i; |
| 2794 | int res; |
| 2795 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2796 | &opath, &i)) |
| 2797 | return NULL; |
| 2798 | path = PyBytes_AsString(opath); |
| 2799 | Py_BEGIN_ALLOW_THREADS |
| 2800 | res = lchmod(path, i); |
| 2801 | Py_END_ALLOW_THREADS |
| 2802 | if (res < 0) |
| 2803 | return posix_error_with_allocated_filename(opath); |
| 2804 | Py_DECREF(opath); |
| 2805 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2806 | } |
| 2807 | #endif /* HAVE_LCHMOD */ |
| 2808 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2809 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2810 | #ifdef HAVE_CHFLAGS |
| 2811 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2812 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 2813 | Set file flags.\n\ |
| 2814 | \n\ |
| 2815 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2816 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 2817 | file the link points to.\n\ |
| 2818 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 2819 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2820 | |
| 2821 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2822 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2823 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2824 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2825 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2826 | int follow_symlinks = 1; |
| 2827 | int result; |
| 2828 | PyObject *return_value; |
| 2829 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 2830 | |
| 2831 | memset(&path, 0, sizeof(path)); |
| 2832 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 2833 | path_converter, &path, |
| 2834 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2835 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2836 | |
| 2837 | #ifndef HAVE_LCHFLAGS |
| 2838 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 2839 | goto exit; |
| 2840 | #endif |
| 2841 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2842 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2843 | #ifdef HAVE_LCHFLAGS |
| 2844 | if (!follow_symlinks) |
| 2845 | result = lchflags(path.narrow, flags); |
| 2846 | else |
| 2847 | #endif |
| 2848 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2849 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2850 | |
| 2851 | if (result) { |
| 2852 | return_value = path_posix_error("chflags", &path); |
| 2853 | goto exit; |
| 2854 | } |
| 2855 | |
| 2856 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2857 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2858 | |
| 2859 | exit: |
| 2860 | path_cleanup(&path); |
| 2861 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2862 | } |
| 2863 | #endif /* HAVE_CHFLAGS */ |
| 2864 | |
| 2865 | #ifdef HAVE_LCHFLAGS |
| 2866 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2867 | "lchflags(path, flags)\n\n\ |
| 2868 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2869 | This function will not follow symbolic links.\n\ |
| 2870 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2871 | |
| 2872 | static PyObject * |
| 2873 | posix_lchflags(PyObject *self, PyObject *args) |
| 2874 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2875 | PyObject *opath; |
| 2876 | char *path; |
| 2877 | unsigned long flags; |
| 2878 | int res; |
| 2879 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2880 | PyUnicode_FSConverter, &opath, &flags)) |
| 2881 | return NULL; |
| 2882 | path = PyBytes_AsString(opath); |
| 2883 | Py_BEGIN_ALLOW_THREADS |
| 2884 | res = lchflags(path, flags); |
| 2885 | Py_END_ALLOW_THREADS |
| 2886 | if (res < 0) |
| 2887 | return posix_error_with_allocated_filename(opath); |
| 2888 | Py_DECREF(opath); |
| 2889 | Py_INCREF(Py_None); |
| 2890 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2891 | } |
| 2892 | #endif /* HAVE_LCHFLAGS */ |
| 2893 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2894 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2895 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2896 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2897 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2898 | |
| 2899 | static PyObject * |
| 2900 | posix_chroot(PyObject *self, PyObject *args) |
| 2901 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2902 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2903 | } |
| 2904 | #endif |
| 2905 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2906 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2907 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2908 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2909 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2910 | |
| 2911 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2912 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2913 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2914 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2915 | } |
| 2916 | #endif /* HAVE_FSYNC */ |
| 2917 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2918 | #ifdef HAVE_SYNC |
| 2919 | PyDoc_STRVAR(posix_sync__doc__, |
| 2920 | "sync()\n\n\ |
| 2921 | Force write of everything to disk."); |
| 2922 | |
| 2923 | static PyObject * |
| 2924 | posix_sync(PyObject *self, PyObject *noargs) |
| 2925 | { |
| 2926 | Py_BEGIN_ALLOW_THREADS |
| 2927 | sync(); |
| 2928 | Py_END_ALLOW_THREADS |
| 2929 | Py_RETURN_NONE; |
| 2930 | } |
| 2931 | #endif |
| 2932 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2933 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2934 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2935 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2936 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2937 | #endif |
| 2938 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2939 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2940 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2941 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2942 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2943 | |
| 2944 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2945 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2946 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2947 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2948 | } |
| 2949 | #endif /* HAVE_FDATASYNC */ |
| 2950 | |
| 2951 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2952 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2953 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2954 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2955 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2956 | \n\ |
| 2957 | path may always be specified as a string.\n\ |
| 2958 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2959 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2960 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2961 | and path should be relative; path will then be relative to that directory.\n\ |
| 2962 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2963 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 2964 | link points to.\n\ |
| 2965 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2966 | an open file descriptor.\n\ |
| 2967 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2968 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2969 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2970 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2971 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2972 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2973 | path_t path; |
| 2974 | long uid_l, gid_l; |
| 2975 | uid_t uid; |
| 2976 | gid_t gid; |
| 2977 | int dir_fd = DEFAULT_DIR_FD; |
| 2978 | int follow_symlinks = 1; |
| 2979 | int result; |
| 2980 | PyObject *return_value = NULL; |
| 2981 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 2982 | "follow_symlinks", NULL}; |
| 2983 | |
| 2984 | memset(&path, 0, sizeof(path)); |
| 2985 | #ifdef HAVE_FCHOWN |
| 2986 | path.allow_fd = 1; |
| 2987 | #endif |
| 2988 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&ll|$O&p:chown", keywords, |
| 2989 | path_converter, &path, |
| 2990 | &uid_l, &gid_l, |
| 2991 | #ifdef HAVE_FCHOWNAT |
| 2992 | dir_fd_converter, &dir_fd, |
| 2993 | #else |
| 2994 | dir_fd_unavailable, &dir_fd, |
| 2995 | #endif |
| 2996 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2997 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2998 | |
| 2999 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3000 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 3001 | goto exit; |
| 3002 | #endif |
| 3003 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 3004 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 3005 | goto exit; |
| 3006 | |
| 3007 | #ifdef __APPLE__ |
| 3008 | /* |
| 3009 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3010 | * (But we still have an lchown symbol because of weak-linking.) |
| 3011 | * It doesn't have fchownat either. So there's no possibility |
| 3012 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3013 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3014 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3015 | follow_symlinks_specified("chown", follow_symlinks); |
| 3016 | goto exit; |
| 3017 | } |
| 3018 | #endif |
| 3019 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3020 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3021 | uid = (uid_t)uid_l; |
| 3022 | gid = (uid_t)gid_l; |
| 3023 | #ifdef HAVE_FCHOWN |
| 3024 | if (path.fd != -1) |
| 3025 | result = fchown(path.fd, uid, gid); |
| 3026 | else |
| 3027 | #endif |
| 3028 | #ifdef HAVE_LCHOWN |
| 3029 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 3030 | result = lchown(path.narrow, uid, gid); |
| 3031 | else |
| 3032 | #endif |
| 3033 | #ifdef HAVE_FCHOWNAT |
| 3034 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 3035 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 3036 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3037 | else |
| 3038 | #endif |
| 3039 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3040 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3041 | |
| 3042 | if (result) { |
| 3043 | return_value = path_posix_error("chown", &path); |
| 3044 | goto exit; |
| 3045 | } |
| 3046 | |
| 3047 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3048 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3049 | |
| 3050 | exit: |
| 3051 | path_cleanup(&path); |
| 3052 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3053 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3054 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3055 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3056 | #ifdef HAVE_FCHOWN |
| 3057 | PyDoc_STRVAR(posix_fchown__doc__, |
| 3058 | "fchown(fd, uid, gid)\n\n\ |
| 3059 | 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] | 3060 | 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] | 3061 | |
| 3062 | static PyObject * |
| 3063 | posix_fchown(PyObject *self, PyObject *args) |
| 3064 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3065 | int fd; |
| 3066 | long uid, gid; |
| 3067 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 3068 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3069 | return NULL; |
| 3070 | Py_BEGIN_ALLOW_THREADS |
| 3071 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 3072 | Py_END_ALLOW_THREADS |
| 3073 | if (res < 0) |
| 3074 | return posix_error(); |
| 3075 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3076 | } |
| 3077 | #endif /* HAVE_FCHOWN */ |
| 3078 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3079 | #ifdef HAVE_LCHOWN |
| 3080 | PyDoc_STRVAR(posix_lchown__doc__, |
| 3081 | "lchown(path, uid, gid)\n\n\ |
| 3082 | 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] | 3083 | This function will not follow symbolic links.\n\ |
| 3084 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3085 | |
| 3086 | static PyObject * |
| 3087 | posix_lchown(PyObject *self, PyObject *args) |
| 3088 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3089 | PyObject *opath; |
| 3090 | char *path; |
| 3091 | long uid, gid; |
| 3092 | int res; |
| 3093 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 3094 | PyUnicode_FSConverter, &opath, |
| 3095 | &uid, &gid)) |
| 3096 | return NULL; |
| 3097 | path = PyBytes_AsString(opath); |
| 3098 | Py_BEGIN_ALLOW_THREADS |
| 3099 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 3100 | Py_END_ALLOW_THREADS |
| 3101 | if (res < 0) |
| 3102 | return posix_error_with_allocated_filename(opath); |
| 3103 | Py_DECREF(opath); |
| 3104 | Py_INCREF(Py_None); |
| 3105 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3106 | } |
| 3107 | #endif /* HAVE_LCHOWN */ |
| 3108 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3109 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3110 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3111 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3112 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3113 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3114 | char buf[1026]; |
| 3115 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3116 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3117 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3118 | if (!use_bytes) { |
| 3119 | wchar_t wbuf[1026]; |
| 3120 | wchar_t *wbuf2 = wbuf; |
| 3121 | PyObject *resobj; |
| 3122 | DWORD len; |
| 3123 | Py_BEGIN_ALLOW_THREADS |
| 3124 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 3125 | /* If the buffer is large enough, len does not include the |
| 3126 | terminating \0. If the buffer is too small, len includes |
| 3127 | the space needed for the terminator. */ |
| 3128 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 3129 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 3130 | if (wbuf2) |
| 3131 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3132 | } |
| 3133 | Py_END_ALLOW_THREADS |
| 3134 | if (!wbuf2) { |
| 3135 | PyErr_NoMemory(); |
| 3136 | return NULL; |
| 3137 | } |
| 3138 | if (!len) { |
| 3139 | if (wbuf2 != wbuf) free(wbuf2); |
| 3140 | return win32_error("getcwdu", NULL); |
| 3141 | } |
| 3142 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3143 | if (wbuf2 != wbuf) free(wbuf2); |
| 3144 | return resobj; |
| 3145 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3146 | |
| 3147 | if (win32_warn_bytes_api()) |
| 3148 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3149 | #endif |
| 3150 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3151 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3152 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3153 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3154 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3155 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3156 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3157 | Py_END_ALLOW_THREADS |
| 3158 | if (res == NULL) |
| 3159 | return posix_error(); |
| 3160 | if (use_bytes) |
| 3161 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3162 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3163 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3164 | |
| 3165 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 3166 | "getcwd() -> path\n\n\ |
| 3167 | Return a unicode string representing the current working directory."); |
| 3168 | |
| 3169 | static PyObject * |
| 3170 | posix_getcwd_unicode(PyObject *self) |
| 3171 | { |
| 3172 | return posix_getcwd(0); |
| 3173 | } |
| 3174 | |
| 3175 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 3176 | "getcwdb() -> path\n\n\ |
| 3177 | Return a bytes string representing the current working directory."); |
| 3178 | |
| 3179 | static PyObject * |
| 3180 | posix_getcwd_bytes(PyObject *self) |
| 3181 | { |
| 3182 | return posix_getcwd(1); |
| 3183 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3184 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3185 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3186 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3187 | #define HAVE_LINK 1 |
| 3188 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3189 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3190 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3191 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3192 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 3193 | Create a hard link to a file.\n\ |
| 3194 | \n\ |
| 3195 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3196 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3197 | should be relative; the path will then be relative to that directory.\n\ |
| 3198 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3199 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3200 | file the link points to.\n\ |
| 3201 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3202 | platform. If they are unavailable, using them will raise a\n\ |
| 3203 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3204 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3205 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3206 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3207 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3208 | path_t src, dst; |
| 3209 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3210 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3211 | int follow_symlinks = 1; |
| 3212 | PyObject *return_value = NULL; |
| 3213 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3214 | "follow_symlinks", NULL}; |
| 3215 | #ifdef MS_WINDOWS |
| 3216 | BOOL result; |
| 3217 | #else |
| 3218 | int result; |
| 3219 | #endif |
| 3220 | |
| 3221 | memset(&src, 0, sizeof(src)); |
| 3222 | memset(&dst, 0, sizeof(dst)); |
| 3223 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3224 | path_converter, &src, |
| 3225 | path_converter, &dst, |
| 3226 | dir_fd_converter, &src_dir_fd, |
| 3227 | dir_fd_converter, &dst_dir_fd, |
| 3228 | &follow_symlinks)) |
| 3229 | return NULL; |
| 3230 | |
| 3231 | #ifndef HAVE_LINKAT |
| 3232 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3233 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3234 | goto exit; |
| 3235 | } |
| 3236 | #endif |
| 3237 | |
| 3238 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3239 | PyErr_SetString(PyExc_NotImplementedError, |
| 3240 | "link: src and dst must be the same type"); |
| 3241 | goto exit; |
| 3242 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3243 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3244 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3245 | Py_BEGIN_ALLOW_THREADS |
| 3246 | if (src.wide) |
| 3247 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3248 | else |
| 3249 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3250 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3251 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3252 | if (!result) { |
| 3253 | return_value = win32_error_object("link", dst.object); |
| 3254 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3255 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3256 | #else |
| 3257 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3258 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3259 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3260 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3261 | (!follow_symlinks)) |
| 3262 | result = linkat(src_dir_fd, src.narrow, |
| 3263 | dst_dir_fd, dst.narrow, |
| 3264 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3265 | else |
| 3266 | #endif |
| 3267 | result = link(src.narrow, dst.narrow); |
| 3268 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3269 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3270 | if (result) { |
| 3271 | return_value = path_error("link", &dst); |
| 3272 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3273 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3274 | #endif |
| 3275 | |
| 3276 | return_value = Py_None; |
| 3277 | Py_INCREF(Py_None); |
| 3278 | |
| 3279 | exit: |
| 3280 | path_cleanup(&src); |
| 3281 | path_cleanup(&dst); |
| 3282 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3283 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3284 | #endif |
| 3285 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3286 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3287 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3288 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3289 | "listdir(path='.') -> list_of_filenames\n\n\ |
| 3290 | 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] | 3291 | 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] | 3292 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3293 | \n\ |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3294 | path can be specified as either str or bytes. If path is bytes,\n\ |
| 3295 | the filenames returned will also be bytes; in all other circumstances\n\ |
| 3296 | the filenames returned will be str.\n\ |
| 3297 | On some platforms, path may also be specified as an open file descriptor;\n\ |
| 3298 | the file descriptor must refer to a directory.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3299 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3300 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3301 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3302 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3303 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3304 | path_t path; |
| 3305 | PyObject *list = NULL; |
| 3306 | static char *keywords[] = {"path", NULL}; |
| 3307 | int fd = -1; |
| 3308 | |
| 3309 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3310 | PyObject *v; |
| 3311 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3312 | BOOL result; |
| 3313 | WIN32_FIND_DATA FileData; |
| 3314 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 3315 | char *bufptr = namebuf; |
| 3316 | /* only claim to have space for MAX_PATH */ |
| 3317 | Py_ssize_t len = sizeof(namebuf)-5; |
| 3318 | PyObject *po = NULL; |
| 3319 | wchar_t *wnamebuf = NULL; |
| 3320 | #elif defined(PYOS_OS2) |
| 3321 | #ifndef MAX_PATH |
| 3322 | #define MAX_PATH CCHMAXPATH |
| 3323 | #endif |
| 3324 | char *pt; |
| 3325 | PyObject *v; |
| 3326 | char namebuf[MAX_PATH+5]; |
| 3327 | HDIR hdir = 1; |
| 3328 | ULONG srchcnt = 1; |
| 3329 | FILEFINDBUF3 ep; |
| 3330 | APIRET rc; |
| 3331 | #else |
| 3332 | PyObject *v; |
| 3333 | DIR *dirp = NULL; |
| 3334 | struct dirent *ep; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3335 | int return_str; /* if false, return bytes */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3336 | #endif |
| 3337 | |
| 3338 | memset(&path, 0, sizeof(path)); |
| 3339 | path.nullable = 1; |
| 3340 | #ifdef HAVE_FDOPENDIR |
| 3341 | path.allow_fd = 1; |
| 3342 | path.fd = -1; |
| 3343 | #endif |
| 3344 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3345 | path_converter, &path |
| 3346 | )) |
| 3347 | return NULL; |
| 3348 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3349 | /* XXX Should redo this putting the (now four) versions of opendir |
| 3350 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3351 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3352 | if (!path.narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3353 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3354 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3355 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3356 | if (!path.wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3357 | po_wchars = L"."; |
| 3358 | len = 1; |
| 3359 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3360 | po_wchars = path.wide; |
| 3361 | len = wcslen(path.wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3362 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3363 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3364 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 3365 | if (!wnamebuf) { |
| 3366 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3367 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3368 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3369 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3370 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3371 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3372 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 3373 | wnamebuf[len++] = L'\\'; |
| 3374 | wcscpy(wnamebuf + len, L"*.*"); |
| 3375 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3376 | if ((list = PyList_New(0)) == NULL) { |
| 3377 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3378 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3379 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3380 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3381 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3382 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3383 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3384 | if (error == ERROR_FILE_NOT_FOUND) |
| 3385 | goto exit; |
| 3386 | Py_DECREF(list); |
| 3387 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3388 | win32_error_unicode("FindFirstFileW", wnamebuf); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3389 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3390 | } |
| 3391 | do { |
| 3392 | /* Skip over . and .. */ |
| 3393 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3394 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3395 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3396 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3397 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3398 | Py_DECREF(list); |
| 3399 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3400 | break; |
| 3401 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3402 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3403 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3404 | Py_DECREF(list); |
| 3405 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3406 | break; |
| 3407 | } |
| 3408 | Py_DECREF(v); |
| 3409 | } |
| 3410 | Py_BEGIN_ALLOW_THREADS |
| 3411 | result = FindNextFileW(hFindFile, &wFileData); |
| 3412 | Py_END_ALLOW_THREADS |
| 3413 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3414 | it got to the end of the directory. */ |
| 3415 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3416 | Py_DECREF(list); |
| 3417 | list = win32_error_unicode("FindNextFileW", wnamebuf); |
| 3418 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3419 | } |
| 3420 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3421 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3422 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3423 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3424 | strcpy(namebuf, path.narrow); |
| 3425 | len = path.length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3426 | if (len > 0) { |
| 3427 | char ch = namebuf[len-1]; |
| 3428 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 3429 | namebuf[len++] = '/'; |
| 3430 | strcpy(namebuf + len, "*.*"); |
| 3431 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3432 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3433 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3434 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3435 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3436 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3437 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3438 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3439 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3440 | int error = GetLastError(); |
| 3441 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3442 | goto exit; |
| 3443 | Py_DECREF(list); |
| 3444 | list = win32_error("FindFirstFile", namebuf); |
| 3445 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3446 | } |
| 3447 | do { |
| 3448 | /* Skip over . and .. */ |
| 3449 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3450 | strcmp(FileData.cFileName, "..") != 0) { |
| 3451 | v = PyBytes_FromString(FileData.cFileName); |
| 3452 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3453 | Py_DECREF(list); |
| 3454 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3455 | break; |
| 3456 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3457 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3458 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3459 | Py_DECREF(list); |
| 3460 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3461 | break; |
| 3462 | } |
| 3463 | Py_DECREF(v); |
| 3464 | } |
| 3465 | Py_BEGIN_ALLOW_THREADS |
| 3466 | result = FindNextFile(hFindFile, &FileData); |
| 3467 | Py_END_ALLOW_THREADS |
| 3468 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3469 | it got to the end of the directory. */ |
| 3470 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3471 | Py_DECREF(list); |
| 3472 | list = win32_error("FindNextFile", namebuf); |
| 3473 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3474 | } |
| 3475 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3476 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3477 | exit: |
| 3478 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3479 | if (FindClose(hFindFile) == FALSE) { |
| 3480 | if (list != NULL) { |
| 3481 | Py_DECREF(list); |
| 3482 | list = win32_error_object("FindClose", path.object); |
| 3483 | } |
| 3484 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3485 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3486 | if (wnamebuf) |
| 3487 | free(wnamebuf); |
| 3488 | path_cleanup(&path); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3489 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3490 | return list; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3491 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 3492 | #elif defined(PYOS_OS2) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3493 | if (path.length >= MAX_PATH) { |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 3494 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3495 | goto exit; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3496 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3497 | strcpy(namebuf, path.narrow); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3498 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3499 | if (*pt == ALTSEP) |
| 3500 | *pt = SEP; |
| 3501 | if (namebuf[len-1] != SEP) |
| 3502 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3503 | strcpy(namebuf + len, "*.*"); |
| 3504 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3505 | if ((list = PyList_New(0)) == NULL) { |
| 3506 | goto exit; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 3507 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3508 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3509 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 3510 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3511 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3512 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 3513 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 3514 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3515 | |
| 3516 | if (rc != NO_ERROR) { |
| 3517 | errno = ENOENT; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3518 | Py_DECREF(list); |
| 3519 | list = posix_error_with_filename(path.narrow); |
| 3520 | goto exit; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3523 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3524 | do { |
| 3525 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3526 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3527 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3528 | |
| 3529 | strcpy(namebuf, ep.achName); |
| 3530 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3531 | /* Leave Case of Name Alone -- In Native Form */ |
| 3532 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3533 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3534 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3535 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3536 | Py_DECREF(list); |
| 3537 | list = NULL; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3538 | break; |
| 3539 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3540 | if (PyList_Append(list, v) != 0) { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3541 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3542 | Py_DECREF(list); |
| 3543 | list = NULL; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3544 | break; |
| 3545 | } |
| 3546 | Py_DECREF(v); |
| 3547 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 3548 | } |
| 3549 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3550 | exit: |
| 3551 | path_cleanup(&path); |
| 3552 | |
| 3553 | return list; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3554 | #else |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 3555 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3556 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3557 | #ifdef HAVE_FDOPENDIR |
| 3558 | if (path.fd != -1) { |
| 3559 | /* closedir() closes the FD, so we duplicate it */ |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3560 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3561 | fd = dup(path.fd); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3562 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3563 | |
| 3564 | if (fd == -1) { |
| 3565 | list = posix_error(); |
| 3566 | goto exit; |
| 3567 | } |
| 3568 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3569 | return_str = 1; |
| 3570 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3571 | Py_BEGIN_ALLOW_THREADS |
| 3572 | dirp = fdopendir(fd); |
| 3573 | Py_END_ALLOW_THREADS |
| 3574 | } |
| 3575 | else |
| 3576 | #endif |
| 3577 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3578 | char *name; |
| 3579 | if (path.narrow) { |
| 3580 | name = path.narrow; |
| 3581 | /* only return bytes if they specified a bytes object */ |
| 3582 | return_str = !(PyBytes_Check(path.object)); |
| 3583 | } |
| 3584 | else { |
| 3585 | name = "."; |
| 3586 | return_str = 1; |
| 3587 | } |
| 3588 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3589 | Py_BEGIN_ALLOW_THREADS |
| 3590 | dirp = opendir(name); |
| 3591 | Py_END_ALLOW_THREADS |
| 3592 | } |
| 3593 | |
| 3594 | if (dirp == NULL) { |
| 3595 | list = path_error("listdir", &path); |
| 3596 | goto exit; |
| 3597 | } |
| 3598 | if ((list = PyList_New(0)) == NULL) { |
| 3599 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3600 | } |
| 3601 | for (;;) { |
| 3602 | errno = 0; |
| 3603 | Py_BEGIN_ALLOW_THREADS |
| 3604 | ep = readdir(dirp); |
| 3605 | Py_END_ALLOW_THREADS |
| 3606 | if (ep == NULL) { |
| 3607 | if (errno == 0) { |
| 3608 | break; |
| 3609 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3610 | Py_DECREF(list); |
| 3611 | list = path_error("listdir", &path); |
| 3612 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3613 | } |
| 3614 | } |
| 3615 | if (ep->d_name[0] == '.' && |
| 3616 | (NAMLEN(ep) == 1 || |
| 3617 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3618 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3619 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3620 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3621 | else |
| 3622 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3623 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3624 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3625 | break; |
| 3626 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3627 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3628 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3629 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3630 | break; |
| 3631 | } |
| 3632 | Py_DECREF(v); |
| 3633 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3634 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3635 | exit: |
| 3636 | if (dirp != NULL) { |
| 3637 | Py_BEGIN_ALLOW_THREADS |
| 3638 | if (fd > -1) |
| 3639 | rewinddir(dirp); |
| 3640 | closedir(dirp); |
| 3641 | Py_END_ALLOW_THREADS |
| 3642 | } |
| 3643 | |
| 3644 | path_cleanup(&path); |
| 3645 | |
| 3646 | return list; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3647 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 3648 | #endif /* which OS */ |
| 3649 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3650 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3651 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3652 | /* A helper function for abspath on win32 */ |
| 3653 | static PyObject * |
| 3654 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3655 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3656 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3657 | char outbuf[MAX_PATH*2]; |
| 3658 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3659 | PyObject *po; |
| 3660 | |
| 3661 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3662 | { |
| 3663 | wchar_t *wpath; |
| 3664 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 3665 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3666 | DWORD result; |
| 3667 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3668 | |
| 3669 | wpath = PyUnicode_AsUnicode(po); |
| 3670 | if (wpath == NULL) |
| 3671 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3672 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3673 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3674 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3675 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3676 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3677 | if (!woutbufp) |
| 3678 | return PyErr_NoMemory(); |
| 3679 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3680 | } |
| 3681 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3682 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3683 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3684 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3685 | if (woutbufp != woutbuf) |
| 3686 | free(woutbufp); |
| 3687 | return v; |
| 3688 | } |
| 3689 | /* Drop the argument parsing error as narrow strings |
| 3690 | are also valid. */ |
| 3691 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3692 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3693 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3694 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3695 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3696 | if (win32_warn_bytes_api()) |
| 3697 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3698 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3699 | outbuf, &temp)) { |
| 3700 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3701 | return NULL; |
| 3702 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3703 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3704 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3705 | Py_FileSystemDefaultEncoding, NULL); |
| 3706 | } |
| 3707 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3708 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3709 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3710 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3711 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3712 | /* A helper function for samepath on windows */ |
| 3713 | static PyObject * |
| 3714 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3715 | { |
| 3716 | HANDLE hFile; |
| 3717 | int buf_size; |
| 3718 | wchar_t *target_path; |
| 3719 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3720 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3721 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3722 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3723 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3724 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3725 | path = PyUnicode_AsUnicode(po); |
| 3726 | if (path == NULL) |
| 3727 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3728 | |
| 3729 | if(!check_GetFinalPathNameByHandle()) { |
| 3730 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3731 | NotImplementedError. */ |
| 3732 | return PyErr_Format(PyExc_NotImplementedError, |
| 3733 | "GetFinalPathNameByHandle not available on this platform"); |
| 3734 | } |
| 3735 | |
| 3736 | hFile = CreateFileW( |
| 3737 | path, |
| 3738 | 0, /* desired access */ |
| 3739 | 0, /* share mode */ |
| 3740 | NULL, /* security attributes */ |
| 3741 | OPEN_EXISTING, |
| 3742 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3743 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3744 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3745 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3746 | if(hFile == INVALID_HANDLE_VALUE) |
| 3747 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3748 | |
| 3749 | /* We have a good handle to the target, use it to determine the |
| 3750 | target path name. */ |
| 3751 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3752 | |
| 3753 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3754 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3755 | |
| 3756 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3757 | if(!target_path) |
| 3758 | return PyErr_NoMemory(); |
| 3759 | |
| 3760 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3761 | buf_size, VOLUME_NAME_DOS); |
| 3762 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3763 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3764 | |
| 3765 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3766 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3767 | |
| 3768 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3769 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3770 | free(target_path); |
| 3771 | return result; |
| 3772 | |
| 3773 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3774 | |
| 3775 | static PyObject * |
| 3776 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3777 | { |
| 3778 | HANDLE hFile; |
| 3779 | BY_HANDLE_FILE_INFORMATION info; |
| 3780 | int fd; |
| 3781 | |
| 3782 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3783 | return NULL; |
| 3784 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3785 | if (!_PyVerify_fd(fd)) |
| 3786 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3787 | |
| 3788 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3789 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3790 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3791 | |
| 3792 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3793 | return win32_error("_getfileinformation", NULL); |
| 3794 | |
| 3795 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3796 | info.nFileIndexHigh, |
| 3797 | info.nFileIndexLow); |
| 3798 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3799 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3800 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3801 | "Return true if the pathname refers to an existing directory."); |
| 3802 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3803 | static PyObject * |
| 3804 | posix__isdir(PyObject *self, PyObject *args) |
| 3805 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3806 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3807 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3808 | DWORD attributes; |
| 3809 | |
| 3810 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3811 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3812 | if (wpath == NULL) |
| 3813 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3814 | |
| 3815 | attributes = GetFileAttributesW(wpath); |
| 3816 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3817 | Py_RETURN_FALSE; |
| 3818 | goto check; |
| 3819 | } |
| 3820 | /* Drop the argument parsing error as narrow strings |
| 3821 | are also valid. */ |
| 3822 | PyErr_Clear(); |
| 3823 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3824 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3825 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3826 | if (win32_warn_bytes_api()) |
| 3827 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3828 | attributes = GetFileAttributesA(path); |
| 3829 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3830 | Py_RETURN_FALSE; |
| 3831 | |
| 3832 | check: |
| 3833 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3834 | Py_RETURN_TRUE; |
| 3835 | else |
| 3836 | Py_RETURN_FALSE; |
| 3837 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3838 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3839 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3840 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3841 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3842 | Create a directory.\n\ |
| 3843 | \n\ |
| 3844 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3845 | and path should be relative; path will then be relative to that directory.\n\ |
| 3846 | dir_fd may not be implemented on your platform.\n\ |
| 3847 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 3848 | \n\ |
| 3849 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3850 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3851 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3852 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3853 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3854 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3855 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3856 | int dir_fd = DEFAULT_DIR_FD; |
| 3857 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 3858 | PyObject *return_value = NULL; |
| 3859 | int result; |
| 3860 | |
| 3861 | memset(&path, 0, sizeof(path)); |
| 3862 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 3863 | path_converter, &path, &mode, |
| 3864 | #ifdef HAVE_MKDIRAT |
| 3865 | dir_fd_converter, &dir_fd |
| 3866 | #else |
| 3867 | dir_fd_unavailable, &dir_fd |
| 3868 | #endif |
| 3869 | )) |
| 3870 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3871 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3872 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3873 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3874 | if (path.wide) |
| 3875 | result = CreateDirectoryW(path.wide, NULL); |
| 3876 | else |
| 3877 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3878 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3879 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3880 | if (!result) { |
| 3881 | return_value = win32_error_object("mkdir", path.object); |
| 3882 | goto exit; |
| 3883 | } |
| 3884 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3885 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3886 | #if HAVE_MKDIRAT |
| 3887 | if (dir_fd != DEFAULT_DIR_FD) |
| 3888 | result = mkdirat(dir_fd, path.narrow, mode); |
| 3889 | else |
| 3890 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3891 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3892 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3893 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3894 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3895 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3896 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3897 | if (result < 0) { |
| 3898 | return_value = path_error("mkdir", &path); |
| 3899 | goto exit; |
| 3900 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3901 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3902 | return_value = Py_None; |
| 3903 | Py_INCREF(Py_None); |
| 3904 | exit: |
| 3905 | path_cleanup(&path); |
| 3906 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3909 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3910 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3911 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3912 | #include <sys/resource.h> |
| 3913 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3914 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3915 | |
| 3916 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3917 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3918 | "nice(inc) -> new_priority\n\n\ |
| 3919 | 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] | 3920 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3921 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3922 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3923 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3924 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3925 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3926 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3927 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3928 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3929 | /* There are two flavours of 'nice': one that returns the new |
| 3930 | priority (as required by almost all standards out there) and the |
| 3931 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3932 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3933 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3934 | If we are of the nice family that returns the new priority, we |
| 3935 | need to clear errno before the call, and check if errno is filled |
| 3936 | before calling posix_error() on a returnvalue of -1, because the |
| 3937 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3938 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3939 | errno = 0; |
| 3940 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3941 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3942 | if (value == 0) |
| 3943 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3944 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3945 | if (value == -1 && errno != 0) |
| 3946 | /* either nice() or getpriority() returned an error */ |
| 3947 | return posix_error(); |
| 3948 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3949 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3950 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3951 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3952 | |
| 3953 | #ifdef HAVE_GETPRIORITY |
| 3954 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3955 | "getpriority(which, who) -> current_priority\n\n\ |
| 3956 | Get program scheduling priority."); |
| 3957 | |
| 3958 | static PyObject * |
| 3959 | posix_getpriority(PyObject *self, PyObject *args) |
| 3960 | { |
| 3961 | int which, who, retval; |
| 3962 | |
| 3963 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3964 | return NULL; |
| 3965 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3966 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3967 | if (errno != 0) |
| 3968 | return posix_error(); |
| 3969 | return PyLong_FromLong((long)retval); |
| 3970 | } |
| 3971 | #endif /* HAVE_GETPRIORITY */ |
| 3972 | |
| 3973 | |
| 3974 | #ifdef HAVE_SETPRIORITY |
| 3975 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3976 | "setpriority(which, who, prio) -> None\n\n\ |
| 3977 | Set program scheduling priority."); |
| 3978 | |
| 3979 | static PyObject * |
| 3980 | posix_setpriority(PyObject *self, PyObject *args) |
| 3981 | { |
| 3982 | int which, who, prio, retval; |
| 3983 | |
| 3984 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3985 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3986 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3987 | if (retval == -1) |
| 3988 | return posix_error(); |
| 3989 | Py_RETURN_NONE; |
| 3990 | } |
| 3991 | #endif /* HAVE_SETPRIORITY */ |
| 3992 | |
| 3993 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3994 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3995 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3996 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3997 | char *function_name = is_replace ? "replace" : "rename"; |
| 3998 | path_t src; |
| 3999 | path_t dst; |
| 4000 | int src_dir_fd = DEFAULT_DIR_FD; |
| 4001 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 4002 | int dir_fd_specified; |
| 4003 | PyObject *return_value = NULL; |
| 4004 | char format[24]; |
| 4005 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 4006 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4007 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4008 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4009 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4010 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4011 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4012 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4013 | |
| 4014 | memset(&src, 0, sizeof(src)); |
| 4015 | memset(&dst, 0, sizeof(dst)); |
| 4016 | strcpy(format, "O&O&|$O&O&:"); |
| 4017 | strcat(format, function_name); |
| 4018 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 4019 | path_converter, &src, |
| 4020 | path_converter, &dst, |
| 4021 | dir_fd_converter, &src_dir_fd, |
| 4022 | dir_fd_converter, &dst_dir_fd)) |
| 4023 | return NULL; |
| 4024 | |
| 4025 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4026 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4027 | #ifndef HAVE_RENAMEAT |
| 4028 | if (dir_fd_specified) { |
| 4029 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 4030 | goto exit; |
| 4031 | } |
| 4032 | #endif |
| 4033 | |
| 4034 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 4035 | PyErr_Format(PyExc_ValueError, |
| 4036 | "%s: src and dst must be the same type", function_name); |
| 4037 | goto exit; |
| 4038 | } |
| 4039 | |
| 4040 | #ifdef MS_WINDOWS |
| 4041 | Py_BEGIN_ALLOW_THREADS |
| 4042 | if (src.wide) |
| 4043 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 4044 | else |
| 4045 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 4046 | Py_END_ALLOW_THREADS |
| 4047 | |
| 4048 | if (!result) { |
| 4049 | return_value = win32_error_object(function_name, dst.object); |
| 4050 | goto exit; |
| 4051 | } |
| 4052 | |
| 4053 | #else |
| 4054 | Py_BEGIN_ALLOW_THREADS |
| 4055 | #ifdef HAVE_RENAMEAT |
| 4056 | if (dir_fd_specified) |
| 4057 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 4058 | else |
| 4059 | #endif |
| 4060 | result = rename(src.narrow, dst.narrow); |
| 4061 | Py_END_ALLOW_THREADS |
| 4062 | |
| 4063 | if (result) { |
| 4064 | return_value = path_error(function_name, &dst); |
| 4065 | goto exit; |
| 4066 | } |
| 4067 | #endif |
| 4068 | |
| 4069 | Py_INCREF(Py_None); |
| 4070 | return_value = Py_None; |
| 4071 | exit: |
| 4072 | path_cleanup(&src); |
| 4073 | path_cleanup(&dst); |
| 4074 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4077 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4078 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4079 | Rename a file or directory.\n\ |
| 4080 | \n\ |
| 4081 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4082 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4083 | should be relative; the path will then be relative to that directory.\n\ |
| 4084 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4085 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4086 | |
| 4087 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4088 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4089 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4090 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4094 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4095 | Rename a file or directory, overwriting the destination.\n\ |
| 4096 | \n\ |
| 4097 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4098 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4099 | should be relative; the path will then be relative to that directory.\n\ |
| 4100 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4101 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4102 | |
| 4103 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4104 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4105 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4106 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4107 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4108 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4109 | PyDoc_STRVAR(posix_rmdir__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4110 | "rmdir(path, *, dir_fd=None)\n\n\ |
| 4111 | Remove a directory.\n\ |
| 4112 | \n\ |
| 4113 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4114 | and path should be relative; path will then be relative to that directory.\n\ |
| 4115 | dir_fd may not be implemented on your platform.\n\ |
| 4116 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4117 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4118 | static PyObject * |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4119 | posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4120 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4121 | path_t path; |
| 4122 | int dir_fd = DEFAULT_DIR_FD; |
| 4123 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 4124 | int result; |
| 4125 | PyObject *return_value = NULL; |
| 4126 | |
| 4127 | memset(&path, 0, sizeof(path)); |
| 4128 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords, |
| 4129 | path_converter, &path, |
| 4130 | #ifdef HAVE_UNLINKAT |
| 4131 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4132 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4133 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4134 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4135 | )) |
| 4136 | return NULL; |
| 4137 | |
| 4138 | Py_BEGIN_ALLOW_THREADS |
| 4139 | #ifdef MS_WINDOWS |
| 4140 | if (path.wide) |
| 4141 | result = RemoveDirectoryW(path.wide); |
| 4142 | else |
| 4143 | result = RemoveDirectoryA(path.narrow); |
| 4144 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4145 | #else |
| 4146 | #ifdef HAVE_UNLINKAT |
| 4147 | if (dir_fd != DEFAULT_DIR_FD) |
| 4148 | result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR); |
| 4149 | else |
| 4150 | #endif |
| 4151 | result = rmdir(path.narrow); |
| 4152 | #endif |
| 4153 | Py_END_ALLOW_THREADS |
| 4154 | |
| 4155 | if (result) { |
| 4156 | return_value = path_error("rmdir", &path); |
| 4157 | goto exit; |
| 4158 | } |
| 4159 | |
| 4160 | return_value = Py_None; |
| 4161 | Py_INCREF(Py_None); |
| 4162 | |
| 4163 | exit: |
| 4164 | path_cleanup(&path); |
| 4165 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4168 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4169 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4170 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4171 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4172 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4173 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4174 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4175 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4176 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4177 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 4178 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4179 | wchar_t *command; |
| 4180 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 4181 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4182 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4183 | Py_BEGIN_ALLOW_THREADS |
| 4184 | sts = _wsystem(command); |
| 4185 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4186 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4187 | PyObject *command_obj; |
| 4188 | char *command; |
| 4189 | if (!PyArg_ParseTuple(args, "O&:system", |
| 4190 | PyUnicode_FSConverter, &command_obj)) |
| 4191 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4192 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4193 | command = PyBytes_AsString(command_obj); |
| 4194 | Py_BEGIN_ALLOW_THREADS |
| 4195 | sts = system(command); |
| 4196 | Py_END_ALLOW_THREADS |
| 4197 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4198 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4199 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4200 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4201 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4202 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4203 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4204 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4205 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4206 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4207 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4208 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4209 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4210 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4211 | int i; |
| 4212 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 4213 | return NULL; |
| 4214 | i = (int)umask(i); |
| 4215 | if (i < 0) |
| 4216 | return posix_error(); |
| 4217 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4220 | #ifdef MS_WINDOWS |
| 4221 | |
| 4222 | /* override the default DeleteFileW behavior so that directory |
| 4223 | symlinks can be removed with this function, the same as with |
| 4224 | Unix symlinks */ |
| 4225 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4226 | { |
| 4227 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4228 | WIN32_FIND_DATAW find_data; |
| 4229 | HANDLE find_data_handle; |
| 4230 | int is_directory = 0; |
| 4231 | int is_link = 0; |
| 4232 | |
| 4233 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4234 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4235 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4236 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4237 | it is a symlink */ |
| 4238 | if(is_directory && |
| 4239 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4240 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4241 | |
| 4242 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 4243 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 4244 | FindClose(find_data_handle); |
| 4245 | } |
| 4246 | } |
| 4247 | } |
| 4248 | |
| 4249 | if (is_directory && is_link) |
| 4250 | return RemoveDirectoryW(lpFileName); |
| 4251 | |
| 4252 | return DeleteFileW(lpFileName); |
| 4253 | } |
| 4254 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4255 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4256 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4257 | "unlink(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4258 | Remove a file (same as remove()).\n\ |
| 4259 | \n\ |
| 4260 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4261 | and path should be relative; path will then be relative to that directory.\n\ |
| 4262 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4263 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4265 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4266 | "remove(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4267 | Remove a file (same as unlink()).\n\ |
| 4268 | \n\ |
| 4269 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4270 | and path should be relative; path will then be relative to that directory.\n\ |
| 4271 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4272 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4273 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4274 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4275 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4276 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4277 | path_t path; |
| 4278 | int dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4279 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4280 | int result; |
| 4281 | PyObject *return_value = NULL; |
| 4282 | |
| 4283 | memset(&path, 0, sizeof(path)); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4284 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4285 | path_converter, &path, |
| 4286 | #ifdef HAVE_UNLINKAT |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4287 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4288 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4289 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4290 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4291 | )) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4292 | return NULL; |
| 4293 | |
| 4294 | Py_BEGIN_ALLOW_THREADS |
| 4295 | #ifdef MS_WINDOWS |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4296 | if (path.wide) |
| 4297 | result = Py_DeleteFileW(path.wide); |
| 4298 | else |
| 4299 | result = DeleteFileA(path.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4300 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4301 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4302 | #ifdef HAVE_UNLINKAT |
| 4303 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4304 | result = unlinkat(dir_fd, path.narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4305 | else |
| 4306 | #endif /* HAVE_UNLINKAT */ |
| 4307 | result = unlink(path.narrow); |
| 4308 | #endif |
| 4309 | Py_END_ALLOW_THREADS |
| 4310 | |
| 4311 | if (result) { |
| 4312 | return_value = path_error("unlink", &path); |
| 4313 | goto exit; |
| 4314 | } |
| 4315 | |
| 4316 | return_value = Py_None; |
| 4317 | Py_INCREF(Py_None); |
| 4318 | |
| 4319 | exit: |
| 4320 | path_cleanup(&path); |
| 4321 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4324 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4325 | PyDoc_STRVAR(posix_uname__doc__, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4326 | "uname() -> uname_result\n\n\ |
| 4327 | Return an object identifying the current operating system.\n\ |
| 4328 | The object behaves like a named tuple with the following fields:\n\ |
| 4329 | (sysname, nodename, release, version, machine)"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4330 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4331 | static PyStructSequence_Field uname_result_fields[] = { |
| 4332 | {"sysname", "operating system name"}, |
| 4333 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4334 | {"release", "operating system release"}, |
| 4335 | {"version", "operating system version"}, |
| 4336 | {"machine", "hardware identifier"}, |
| 4337 | {NULL} |
| 4338 | }; |
| 4339 | |
| 4340 | PyDoc_STRVAR(uname_result__doc__, |
| 4341 | "uname_result: Result from os.uname().\n\n\ |
| 4342 | This object may be accessed either as a tuple of\n\ |
| 4343 | (sysname, nodename, release, version, machine),\n\ |
| 4344 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4345 | \n\ |
| 4346 | See os.uname for more information."); |
| 4347 | |
| 4348 | static PyStructSequence_Desc uname_result_desc = { |
| 4349 | "uname_result", /* name */ |
| 4350 | uname_result__doc__, /* doc */ |
| 4351 | uname_result_fields, |
| 4352 | 5 |
| 4353 | }; |
| 4354 | |
| 4355 | static PyTypeObject UnameResultType; |
| 4356 | |
| 4357 | |
| 4358 | #ifdef HAVE_UNAME |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4359 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4360 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4361 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4362 | struct utsname u; |
| 4363 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4364 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4365 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4366 | Py_BEGIN_ALLOW_THREADS |
| 4367 | res = uname(&u); |
| 4368 | Py_END_ALLOW_THREADS |
| 4369 | if (res < 0) |
| 4370 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4371 | |
| 4372 | value = PyStructSequence_New(&UnameResultType); |
| 4373 | if (value == NULL) |
| 4374 | return NULL; |
| 4375 | |
| 4376 | #define SET(i, field) \ |
| 4377 | { \ |
| 4378 | PyObject *o = PyUnicode_DecodeASCII(field, strlen(field), NULL); \ |
| 4379 | if (!o) { \ |
| 4380 | Py_DECREF(value); \ |
| 4381 | return NULL; \ |
| 4382 | } \ |
| 4383 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4384 | } \ |
| 4385 | |
| 4386 | SET(0, u.sysname); |
| 4387 | SET(1, u.nodename); |
| 4388 | SET(2, u.release); |
| 4389 | SET(3, u.version); |
| 4390 | SET(4, u.machine); |
| 4391 | |
| 4392 | #undef SET |
| 4393 | |
| 4394 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4395 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4396 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4397 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4398 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4399 | PyDoc_STRVAR(posix_utime__doc__, |
| 4400 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4401 | Set the access and modified time of path.\n\ |
| 4402 | \n\ |
| 4403 | path may always be specified as a string.\n\ |
| 4404 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4405 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4406 | \n\ |
| 4407 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4408 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4409 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4410 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4411 | since the epoch.\n\ |
| 4412 | If both times and ns are None, utime uses the current time.\n\ |
| 4413 | Specifying tuples for both times and ns is an error.\n\ |
| 4414 | \n\ |
| 4415 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4416 | and path should be relative; path will then be relative to that directory.\n\ |
| 4417 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4418 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4419 | link points to.\n\ |
| 4420 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4421 | as an open file descriptor.\n\ |
| 4422 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4423 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4424 | |
| 4425 | typedef struct { |
| 4426 | int now; |
| 4427 | time_t atime_s; |
| 4428 | long atime_ns; |
| 4429 | time_t mtime_s; |
| 4430 | long mtime_ns; |
| 4431 | } utime_t; |
| 4432 | |
| 4433 | /* |
| 4434 | * these macros assume that "utime" is a pointer to a utime_t |
| 4435 | * they also intentionally leak the declaration of a pointer named "time" |
| 4436 | */ |
| 4437 | #define UTIME_TO_TIMESPEC \ |
| 4438 | struct timespec ts[2]; \ |
| 4439 | struct timespec *time; \ |
| 4440 | if (utime->now) \ |
| 4441 | time = NULL; \ |
| 4442 | else { \ |
| 4443 | ts[0].tv_sec = utime->atime_s; \ |
| 4444 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4445 | ts[1].tv_sec = utime->mtime_s; \ |
| 4446 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4447 | time = ts; \ |
| 4448 | } \ |
| 4449 | |
| 4450 | #define UTIME_TO_TIMEVAL \ |
| 4451 | struct timeval tv[2]; \ |
| 4452 | struct timeval *time; \ |
| 4453 | if (utime->now) \ |
| 4454 | time = NULL; \ |
| 4455 | else { \ |
| 4456 | tv[0].tv_sec = utime->atime_s; \ |
| 4457 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4458 | tv[1].tv_sec = utime->mtime_s; \ |
| 4459 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4460 | time = tv; \ |
| 4461 | } \ |
| 4462 | |
| 4463 | #define UTIME_TO_UTIMBUF \ |
| 4464 | struct utimbuf u[2]; \ |
| 4465 | struct utimbuf *time; \ |
| 4466 | if (utime->now) \ |
| 4467 | time = NULL; \ |
| 4468 | else { \ |
| 4469 | u.actime = utime->atime_s; \ |
| 4470 | u.modtime = utime->mtime_s; \ |
| 4471 | time = u; \ |
| 4472 | } |
| 4473 | |
| 4474 | #define UTIME_TO_TIME_T \ |
| 4475 | time_t timet[2]; \ |
| 4476 | struct timet time; \ |
| 4477 | if (utime->now) \ |
| 4478 | time = NULL; \ |
| 4479 | else { \ |
| 4480 | timet[0] = utime->atime_s; \ |
| 4481 | timet[1] = utime->mtime_s; \ |
| 4482 | time = &timet; \ |
| 4483 | } \ |
| 4484 | |
| 4485 | |
| 4486 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4487 | |
| 4488 | #if UTIME_HAVE_DIR_FD |
| 4489 | |
| 4490 | static int |
| 4491 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4492 | { |
| 4493 | #ifdef HAVE_UTIMENSAT |
| 4494 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4495 | UTIME_TO_TIMESPEC; |
| 4496 | return utimensat(dir_fd, path, time, flags); |
| 4497 | #elif defined(HAVE_FUTIMESAT) |
| 4498 | UTIME_TO_TIMEVAL; |
| 4499 | /* |
| 4500 | * follow_symlinks will never be false here; |
| 4501 | * we only allow !follow_symlinks and dir_fd together |
| 4502 | * if we have utimensat() |
| 4503 | */ |
| 4504 | assert(follow_symlinks); |
| 4505 | return futimesat(dir_fd, path, time); |
| 4506 | #endif |
| 4507 | } |
| 4508 | |
| 4509 | #endif |
| 4510 | |
| 4511 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4512 | |
| 4513 | #if UTIME_HAVE_FD |
| 4514 | |
| 4515 | static int |
| 4516 | utime_fd(utime_t *utime, int fd) |
| 4517 | { |
| 4518 | #ifdef HAVE_FUTIMENS |
| 4519 | UTIME_TO_TIMESPEC; |
| 4520 | return futimens(fd, time); |
| 4521 | #else |
| 4522 | UTIME_TO_TIMEVAL; |
| 4523 | return futimes(fd, time); |
| 4524 | #endif |
| 4525 | } |
| 4526 | |
| 4527 | #endif |
| 4528 | |
| 4529 | |
| 4530 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4531 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4532 | |
| 4533 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4534 | |
| 4535 | static int |
| 4536 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4537 | { |
| 4538 | #ifdef HAVE_UTIMENSAT |
| 4539 | UTIME_TO_TIMESPEC; |
| 4540 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4541 | #else |
| 4542 | UTIME_TO_TIMEVAL; |
| 4543 | return lutimes(path, time); |
| 4544 | #endif |
| 4545 | } |
| 4546 | |
| 4547 | #endif |
| 4548 | |
| 4549 | #ifndef MS_WINDOWS |
| 4550 | |
| 4551 | static int |
| 4552 | utime_default(utime_t *utime, char *path) |
| 4553 | { |
| 4554 | #ifdef HAVE_UTIMENSAT |
| 4555 | UTIME_TO_TIMESPEC; |
| 4556 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4557 | #elif defined(HAVE_UTIMES) |
| 4558 | UTIME_TO_TIMEVAL; |
| 4559 | return utimes(path, time); |
| 4560 | #elif defined(HAVE_UTIME_H) |
| 4561 | UTIME_TO_UTIMBUF; |
| 4562 | return utime(path, time); |
| 4563 | #else |
| 4564 | UTIME_TO_TIME_T; |
| 4565 | return utime(path, time); |
| 4566 | #endif |
| 4567 | } |
| 4568 | |
| 4569 | #endif |
| 4570 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4571 | static int |
| 4572 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4573 | { |
| 4574 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4575 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4576 | divmod = PyNumber_Divmod(py_long, billion); |
| 4577 | if (!divmod) |
| 4578 | goto exit; |
| 4579 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4580 | if ((*s == -1) && PyErr_Occurred()) |
| 4581 | goto exit; |
| 4582 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4583 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4584 | goto exit; |
| 4585 | |
| 4586 | result = 1; |
| 4587 | exit: |
| 4588 | Py_XDECREF(divmod); |
| 4589 | return result; |
| 4590 | } |
| 4591 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4592 | static PyObject * |
| 4593 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4594 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4595 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4596 | PyObject *times = NULL; |
| 4597 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4598 | int dir_fd = DEFAULT_DIR_FD; |
| 4599 | int follow_symlinks = 1; |
| 4600 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4601 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4602 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4603 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4604 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4605 | #ifdef MS_WINDOWS |
| 4606 | HANDLE hFile; |
| 4607 | FILETIME atime, mtime; |
| 4608 | #else |
| 4609 | int result; |
| 4610 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4611 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4612 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4613 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4614 | memset(&path, 0, sizeof(path)); |
| 4615 | #if UTIME_HAVE_FD |
| 4616 | path.allow_fd = 1; |
| 4617 | #endif |
| 4618 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4619 | "O&|O$OO&p:utime", keywords, |
| 4620 | path_converter, &path, |
| 4621 | ×, &ns, |
| 4622 | #if UTIME_HAVE_DIR_FD |
| 4623 | dir_fd_converter, &dir_fd, |
| 4624 | #else |
| 4625 | dir_fd_unavailable, &dir_fd, |
| 4626 | #endif |
| 4627 | &follow_symlinks |
| 4628 | )) |
| 4629 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4630 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4631 | if (times && (times != Py_None) && ns) { |
| 4632 | PyErr_SetString(PyExc_ValueError, |
| 4633 | "utime: you may specify either 'times'" |
| 4634 | " or 'ns' but not both"); |
| 4635 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | if (times && (times != Py_None)) { |
| 4639 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4640 | PyErr_SetString(PyExc_TypeError, |
| 4641 | "utime: 'times' must be either" |
| 4642 | " a tuple of two ints or None"); |
| 4643 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4644 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4645 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4646 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4647 | &utime.atime_s, &utime.atime_ns) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4648 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4649 | &utime.mtime_s, &utime.mtime_ns) == -1) { |
| 4650 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4651 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4652 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4653 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4654 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4655 | PyErr_SetString(PyExc_TypeError, |
| 4656 | "utime: 'ns' must be a tuple of two ints"); |
| 4657 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4658 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4659 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4660 | 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] | 4661 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4662 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4663 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4664 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4665 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4666 | } |
| 4667 | else { |
| 4668 | /* times and ns are both None/unspecified. use "now". */ |
| 4669 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4670 | } |
| 4671 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4672 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4673 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4674 | goto exit; |
| 4675 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4676 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4677 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4678 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4679 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4680 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4681 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4682 | #if !defined(HAVE_UTIMENSAT) |
| 4683 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4684 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4685 | "utime: cannot use dir_fd and follow_symlinks " |
| 4686 | "together on this platform"); |
| 4687 | goto exit; |
| 4688 | } |
| 4689 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4690 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4691 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4692 | Py_BEGIN_ALLOW_THREADS |
| 4693 | if (path.wide) |
| 4694 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4695 | NULL, OPEN_EXISTING, |
| 4696 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4697 | else |
| 4698 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4699 | NULL, OPEN_EXISTING, |
| 4700 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4701 | Py_END_ALLOW_THREADS |
| 4702 | if (hFile == INVALID_HANDLE_VALUE) { |
| 4703 | win32_error_object("utime", path.object); |
| 4704 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4705 | } |
| 4706 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4707 | if (utime.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4708 | SYSTEMTIME now; |
| 4709 | GetSystemTime(&now); |
| 4710 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 4711 | !SystemTimeToFileTime(&now, &atime)) { |
| 4712 | win32_error("utime", NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4713 | goto exit; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4714 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4715 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4716 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4717 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4718 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4719 | } |
| 4720 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4721 | /* Avoid putting the file name into the error here, |
| 4722 | as that may confuse the user into believing that |
| 4723 | something is wrong with the file, when it also |
| 4724 | could be the time stamp that gives a problem. */ |
| 4725 | win32_error("utime", NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4726 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4727 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4728 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4729 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4730 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4731 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4732 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4733 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4734 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4735 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4736 | |
| 4737 | #if UTIME_HAVE_DIR_FD |
| 4738 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4739 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4740 | else |
| 4741 | #endif |
| 4742 | |
| 4743 | #if UTIME_HAVE_FD |
| 4744 | if (path.fd != -1) |
| 4745 | result = utime_fd(&utime, path.fd); |
| 4746 | else |
| 4747 | #endif |
| 4748 | |
| 4749 | result = utime_default(&utime, path.narrow); |
| 4750 | |
| 4751 | Py_END_ALLOW_THREADS |
| 4752 | |
| 4753 | if (result < 0) { |
| 4754 | /* see previous comment about not putting filename in error here */ |
| 4755 | return_value = posix_error(); |
| 4756 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4757 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4758 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4759 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4760 | |
| 4761 | Py_INCREF(Py_None); |
| 4762 | return_value = Py_None; |
| 4763 | |
| 4764 | exit: |
| 4765 | path_cleanup(&path); |
| 4766 | #ifdef MS_WINDOWS |
| 4767 | if (hFile != INVALID_HANDLE_VALUE) |
| 4768 | CloseHandle(hFile); |
| 4769 | #endif |
| 4770 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4773 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4774 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4775 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4776 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4777 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4778 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4779 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4780 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4781 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4782 | int sts; |
| 4783 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 4784 | return NULL; |
| 4785 | _exit(sts); |
| 4786 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4789 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4790 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4791 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4792 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4793 | Py_ssize_t i; |
| 4794 | for (i = 0; i < count; i++) |
| 4795 | PyMem_Free(array[i]); |
| 4796 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4797 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4798 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4799 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4800 | int fsconvert_strdup(PyObject *o, char**out) |
| 4801 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4802 | PyObject *bytes; |
| 4803 | Py_ssize_t size; |
| 4804 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4805 | return 0; |
| 4806 | size = PyBytes_GET_SIZE(bytes); |
| 4807 | *out = PyMem_Malloc(size+1); |
| 4808 | if (!*out) |
| 4809 | return 0; |
| 4810 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4811 | Py_DECREF(bytes); |
| 4812 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4813 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4814 | #endif |
| 4815 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4816 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4817 | static char** |
| 4818 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4819 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4820 | char **envlist; |
| 4821 | Py_ssize_t i, pos, envc; |
| 4822 | PyObject *keys=NULL, *vals=NULL; |
| 4823 | PyObject *key, *val, *key2, *val2; |
| 4824 | char *p, *k, *v; |
| 4825 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4826 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4827 | i = PyMapping_Size(env); |
| 4828 | if (i < 0) |
| 4829 | return NULL; |
| 4830 | envlist = PyMem_NEW(char *, i + 1); |
| 4831 | if (envlist == NULL) { |
| 4832 | PyErr_NoMemory(); |
| 4833 | return NULL; |
| 4834 | } |
| 4835 | envc = 0; |
| 4836 | keys = PyMapping_Keys(env); |
| 4837 | vals = PyMapping_Values(env); |
| 4838 | if (!keys || !vals) |
| 4839 | goto error; |
| 4840 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4841 | PyErr_Format(PyExc_TypeError, |
| 4842 | "env.keys() or env.values() is not a list"); |
| 4843 | goto error; |
| 4844 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4845 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4846 | for (pos = 0; pos < i; pos++) { |
| 4847 | key = PyList_GetItem(keys, pos); |
| 4848 | val = PyList_GetItem(vals, pos); |
| 4849 | if (!key || !val) |
| 4850 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4851 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4852 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4853 | goto error; |
| 4854 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4855 | Py_DECREF(key2); |
| 4856 | goto error; |
| 4857 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4858 | |
| 4859 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4860 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 4861 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4862 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4863 | k = PyBytes_AsString(key2); |
| 4864 | v = PyBytes_AsString(val2); |
| 4865 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4866 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4867 | p = PyMem_NEW(char, len); |
| 4868 | if (p == NULL) { |
| 4869 | PyErr_NoMemory(); |
| 4870 | Py_DECREF(key2); |
| 4871 | Py_DECREF(val2); |
| 4872 | goto error; |
| 4873 | } |
| 4874 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4875 | envlist[envc++] = p; |
| 4876 | Py_DECREF(key2); |
| 4877 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4878 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4879 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4880 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4881 | } |
| 4882 | Py_DECREF(vals); |
| 4883 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4884 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4885 | envlist[envc] = 0; |
| 4886 | *envc_ptr = envc; |
| 4887 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4888 | |
| 4889 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4890 | Py_XDECREF(keys); |
| 4891 | Py_XDECREF(vals); |
| 4892 | while (--envc >= 0) |
| 4893 | PyMem_DEL(envlist[envc]); |
| 4894 | PyMem_DEL(envlist); |
| 4895 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4896 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4897 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4898 | static char** |
| 4899 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4900 | { |
| 4901 | int i; |
| 4902 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4903 | if (argvlist == NULL) { |
| 4904 | PyErr_NoMemory(); |
| 4905 | return NULL; |
| 4906 | } |
| 4907 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4908 | PyObject* item = PySequence_ITEM(argv, i); |
| 4909 | if (item == NULL) |
| 4910 | goto fail; |
| 4911 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4912 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4913 | goto fail; |
| 4914 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4915 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4916 | } |
| 4917 | argvlist[*argc] = NULL; |
| 4918 | return argvlist; |
| 4919 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4920 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4921 | free_string_array(argvlist, *argc); |
| 4922 | return NULL; |
| 4923 | } |
| 4924 | #endif |
| 4925 | |
| 4926 | #ifdef HAVE_EXECV |
| 4927 | PyDoc_STRVAR(posix_execv__doc__, |
| 4928 | "execv(path, args)\n\n\ |
| 4929 | Execute an executable path with arguments, replacing current process.\n\ |
| 4930 | \n\ |
| 4931 | path: path of executable file\n\ |
| 4932 | args: tuple or list of strings"); |
| 4933 | |
| 4934 | static PyObject * |
| 4935 | posix_execv(PyObject *self, PyObject *args) |
| 4936 | { |
| 4937 | PyObject *opath; |
| 4938 | char *path; |
| 4939 | PyObject *argv; |
| 4940 | char **argvlist; |
| 4941 | Py_ssize_t argc; |
| 4942 | |
| 4943 | /* execv has two arguments: (path, argv), where |
| 4944 | argv is a list or tuple of strings. */ |
| 4945 | |
| 4946 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4947 | PyUnicode_FSConverter, |
| 4948 | &opath, &argv)) |
| 4949 | return NULL; |
| 4950 | path = PyBytes_AsString(opath); |
| 4951 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4952 | PyErr_SetString(PyExc_TypeError, |
| 4953 | "execv() arg 2 must be a tuple or list"); |
| 4954 | Py_DECREF(opath); |
| 4955 | return NULL; |
| 4956 | } |
| 4957 | argc = PySequence_Size(argv); |
| 4958 | if (argc < 1) { |
| 4959 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4960 | Py_DECREF(opath); |
| 4961 | return NULL; |
| 4962 | } |
| 4963 | |
| 4964 | argvlist = parse_arglist(argv, &argc); |
| 4965 | if (argvlist == NULL) { |
| 4966 | Py_DECREF(opath); |
| 4967 | return NULL; |
| 4968 | } |
| 4969 | |
| 4970 | execv(path, argvlist); |
| 4971 | |
| 4972 | /* If we get here it's definitely an error */ |
| 4973 | |
| 4974 | free_string_array(argvlist, argc); |
| 4975 | Py_DECREF(opath); |
| 4976 | return posix_error(); |
| 4977 | } |
| 4978 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4979 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4980 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4981 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4982 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4983 | path: path of executable file\n\ |
| 4984 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4985 | env: dictionary of strings mapping to strings\n\ |
| 4986 | \n\ |
| 4987 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 4988 | execve will execute the program the file descriptor is open to.\n\ |
| 4989 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4990 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4991 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4992 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4993 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4994 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4995 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4996 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4997 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4998 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4999 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5000 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5001 | /* execve has three arguments: (path, argv, env), where |
| 5002 | argv is a list or tuple of strings and env is a dictionary |
| 5003 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5004 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5005 | memset(&path, 0, sizeof(path)); |
| 5006 | #ifdef HAVE_FEXECVE |
| 5007 | path.allow_fd = 1; |
| 5008 | #endif |
| 5009 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 5010 | path_converter, &path, |
| 5011 | &argv, &env |
| 5012 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5013 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5014 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5015 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5016 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5017 | "execve: argv must be a tuple or list"); |
| 5018 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5019 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5020 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5021 | if (!PyMapping_Check(env)) { |
| 5022 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5023 | "execve: environment must be a mapping object"); |
| 5024 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5025 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5026 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5027 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5028 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5029 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5030 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5032 | envlist = parse_envlist(env, &envc); |
| 5033 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5034 | goto fail; |
| 5035 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5036 | #ifdef HAVE_FEXECVE |
| 5037 | if (path.fd > -1) |
| 5038 | fexecve(path.fd, argvlist, envlist); |
| 5039 | else |
| 5040 | #endif |
| 5041 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5042 | |
| 5043 | /* If we get here it's definitely an error */ |
| 5044 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5045 | path_posix_error("execve", &path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5046 | |
| 5047 | while (--envc >= 0) |
| 5048 | PyMem_DEL(envlist[envc]); |
| 5049 | PyMem_DEL(envlist); |
| 5050 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5051 | if (argvlist) |
| 5052 | free_string_array(argvlist, argc); |
| 5053 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5054 | return NULL; |
| 5055 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5056 | #endif /* HAVE_EXECV */ |
| 5057 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5058 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5059 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5060 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5061 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5062 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5063 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5064 | mode: mode of process creation\n\ |
| 5065 | path: path of executable file\n\ |
| 5066 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5067 | |
| 5068 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5069 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5070 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5071 | PyObject *opath; |
| 5072 | char *path; |
| 5073 | PyObject *argv; |
| 5074 | char **argvlist; |
| 5075 | int mode, i; |
| 5076 | Py_ssize_t argc; |
| 5077 | Py_intptr_t spawnval; |
| 5078 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5079 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5080 | /* spawnv has three arguments: (mode, path, argv), where |
| 5081 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5082 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5083 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 5084 | PyUnicode_FSConverter, |
| 5085 | &opath, &argv)) |
| 5086 | return NULL; |
| 5087 | path = PyBytes_AsString(opath); |
| 5088 | if (PyList_Check(argv)) { |
| 5089 | argc = PyList_Size(argv); |
| 5090 | getitem = PyList_GetItem; |
| 5091 | } |
| 5092 | else if (PyTuple_Check(argv)) { |
| 5093 | argc = PyTuple_Size(argv); |
| 5094 | getitem = PyTuple_GetItem; |
| 5095 | } |
| 5096 | else { |
| 5097 | PyErr_SetString(PyExc_TypeError, |
| 5098 | "spawnv() arg 2 must be a tuple or list"); |
| 5099 | Py_DECREF(opath); |
| 5100 | return NULL; |
| 5101 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5102 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5103 | argvlist = PyMem_NEW(char *, argc+1); |
| 5104 | if (argvlist == NULL) { |
| 5105 | Py_DECREF(opath); |
| 5106 | return PyErr_NoMemory(); |
| 5107 | } |
| 5108 | for (i = 0; i < argc; i++) { |
| 5109 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5110 | &argvlist[i])) { |
| 5111 | free_string_array(argvlist, i); |
| 5112 | PyErr_SetString( |
| 5113 | PyExc_TypeError, |
| 5114 | "spawnv() arg 2 must contain only strings"); |
| 5115 | Py_DECREF(opath); |
| 5116 | return NULL; |
| 5117 | } |
| 5118 | } |
| 5119 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5120 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5121 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5122 | Py_BEGIN_ALLOW_THREADS |
| 5123 | spawnval = spawnv(mode, path, argvlist); |
| 5124 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5125 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5126 | if (mode == _OLD_P_OVERLAY) |
| 5127 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5128 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5129 | Py_BEGIN_ALLOW_THREADS |
| 5130 | spawnval = _spawnv(mode, path, argvlist); |
| 5131 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5132 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5133 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5134 | free_string_array(argvlist, argc); |
| 5135 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5136 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5137 | if (spawnval == -1) |
| 5138 | return posix_error(); |
| 5139 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 5140 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5141 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5142 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5143 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5144 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5148 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5149 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5150 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5151 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5152 | mode: mode of process creation\n\ |
| 5153 | path: path of executable file\n\ |
| 5154 | args: tuple or list of arguments\n\ |
| 5155 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5156 | |
| 5157 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5158 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5159 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5160 | PyObject *opath; |
| 5161 | char *path; |
| 5162 | PyObject *argv, *env; |
| 5163 | char **argvlist; |
| 5164 | char **envlist; |
| 5165 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5166 | int mode; |
| 5167 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5168 | Py_intptr_t spawnval; |
| 5169 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5170 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5171 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5172 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5173 | argv is a list or tuple of strings and env is a dictionary |
| 5174 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5175 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5176 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 5177 | PyUnicode_FSConverter, |
| 5178 | &opath, &argv, &env)) |
| 5179 | return NULL; |
| 5180 | path = PyBytes_AsString(opath); |
| 5181 | if (PyList_Check(argv)) { |
| 5182 | argc = PyList_Size(argv); |
| 5183 | getitem = PyList_GetItem; |
| 5184 | } |
| 5185 | else if (PyTuple_Check(argv)) { |
| 5186 | argc = PyTuple_Size(argv); |
| 5187 | getitem = PyTuple_GetItem; |
| 5188 | } |
| 5189 | else { |
| 5190 | PyErr_SetString(PyExc_TypeError, |
| 5191 | "spawnve() arg 2 must be a tuple or list"); |
| 5192 | goto fail_0; |
| 5193 | } |
| 5194 | if (!PyMapping_Check(env)) { |
| 5195 | PyErr_SetString(PyExc_TypeError, |
| 5196 | "spawnve() arg 3 must be a mapping object"); |
| 5197 | goto fail_0; |
| 5198 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5199 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5200 | argvlist = PyMem_NEW(char *, argc+1); |
| 5201 | if (argvlist == NULL) { |
| 5202 | PyErr_NoMemory(); |
| 5203 | goto fail_0; |
| 5204 | } |
| 5205 | for (i = 0; i < argc; i++) { |
| 5206 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5207 | &argvlist[i])) |
| 5208 | { |
| 5209 | lastarg = i; |
| 5210 | goto fail_1; |
| 5211 | } |
| 5212 | } |
| 5213 | lastarg = argc; |
| 5214 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5215 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5216 | envlist = parse_envlist(env, &envc); |
| 5217 | if (envlist == NULL) |
| 5218 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5219 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5220 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5221 | Py_BEGIN_ALLOW_THREADS |
| 5222 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 5223 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5224 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5225 | if (mode == _OLD_P_OVERLAY) |
| 5226 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5227 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5228 | Py_BEGIN_ALLOW_THREADS |
| 5229 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 5230 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5231 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5232 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5233 | if (spawnval == -1) |
| 5234 | (void) posix_error(); |
| 5235 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 5236 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5237 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5238 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5239 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5240 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5241 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5242 | while (--envc >= 0) |
| 5243 | PyMem_DEL(envlist[envc]); |
| 5244 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5245 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5246 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5247 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5248 | Py_DECREF(opath); |
| 5249 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5250 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5251 | |
| 5252 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 5253 | #if defined(PYOS_OS2) |
| 5254 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 5255 | "spawnvp(mode, file, args)\n\n\ |
| 5256 | Execute the program 'file' in a new process, using the environment\n\ |
| 5257 | search path to find the file.\n\ |
| 5258 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5259 | mode: mode of process creation\n\ |
| 5260 | file: executable file name\n\ |
| 5261 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5262 | |
| 5263 | static PyObject * |
| 5264 | posix_spawnvp(PyObject *self, PyObject *args) |
| 5265 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5266 | PyObject *opath; |
| 5267 | char *path; |
| 5268 | PyObject *argv; |
| 5269 | char **argvlist; |
| 5270 | int mode, i, argc; |
| 5271 | Py_intptr_t spawnval; |
| 5272 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5273 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5274 | /* spawnvp has three arguments: (mode, path, argv), where |
| 5275 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5276 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5277 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 5278 | PyUnicode_FSConverter, |
| 5279 | &opath, &argv)) |
| 5280 | return NULL; |
| 5281 | path = PyBytes_AsString(opath); |
| 5282 | if (PyList_Check(argv)) { |
| 5283 | argc = PyList_Size(argv); |
| 5284 | getitem = PyList_GetItem; |
| 5285 | } |
| 5286 | else if (PyTuple_Check(argv)) { |
| 5287 | argc = PyTuple_Size(argv); |
| 5288 | getitem = PyTuple_GetItem; |
| 5289 | } |
| 5290 | else { |
| 5291 | PyErr_SetString(PyExc_TypeError, |
| 5292 | "spawnvp() arg 2 must be a tuple or list"); |
| 5293 | Py_DECREF(opath); |
| 5294 | return NULL; |
| 5295 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5296 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5297 | argvlist = PyMem_NEW(char *, argc+1); |
| 5298 | if (argvlist == NULL) { |
| 5299 | Py_DECREF(opath); |
| 5300 | return PyErr_NoMemory(); |
| 5301 | } |
| 5302 | for (i = 0; i < argc; i++) { |
| 5303 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5304 | &argvlist[i])) { |
| 5305 | free_string_array(argvlist, i); |
| 5306 | PyErr_SetString( |
| 5307 | PyExc_TypeError, |
| 5308 | "spawnvp() arg 2 must contain only strings"); |
| 5309 | Py_DECREF(opath); |
| 5310 | return NULL; |
| 5311 | } |
| 5312 | } |
| 5313 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5314 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5315 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5316 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5317 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5318 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5319 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5320 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5321 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5322 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5323 | free_string_array(argvlist, argc); |
| 5324 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5325 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5326 | if (spawnval == -1) |
| 5327 | return posix_error(); |
| 5328 | else |
| 5329 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
| 5332 | |
| 5333 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 5334 | "spawnvpe(mode, file, args, env)\n\n\ |
| 5335 | Execute the program 'file' in a new process, using the environment\n\ |
| 5336 | search path to find the file.\n\ |
| 5337 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5338 | mode: mode of process creation\n\ |
| 5339 | file: executable file name\n\ |
| 5340 | args: tuple or list of arguments\n\ |
| 5341 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5342 | |
| 5343 | static PyObject * |
| 5344 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 5345 | { |
Ross Lagerwall | dcfde5a | 2011-11-04 07:09:14 +0200 | [diff] [blame] | 5346 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5347 | char *path; |
| 5348 | PyObject *argv, *env; |
| 5349 | char **argvlist; |
| 5350 | char **envlist; |
| 5351 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5352 | int mode; |
| 5353 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5354 | Py_intptr_t spawnval; |
| 5355 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5356 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5357 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5358 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 5359 | argv is a list or tuple of strings and env is a dictionary |
| 5360 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5361 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5362 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 5363 | PyUnicode_FSConverter, |
| 5364 | &opath, &argv, &env)) |
| 5365 | return NULL; |
| 5366 | path = PyBytes_AsString(opath); |
| 5367 | if (PyList_Check(argv)) { |
| 5368 | argc = PyList_Size(argv); |
| 5369 | getitem = PyList_GetItem; |
| 5370 | } |
| 5371 | else if (PyTuple_Check(argv)) { |
| 5372 | argc = PyTuple_Size(argv); |
| 5373 | getitem = PyTuple_GetItem; |
| 5374 | } |
| 5375 | else { |
| 5376 | PyErr_SetString(PyExc_TypeError, |
| 5377 | "spawnvpe() arg 2 must be a tuple or list"); |
| 5378 | goto fail_0; |
| 5379 | } |
| 5380 | if (!PyMapping_Check(env)) { |
| 5381 | PyErr_SetString(PyExc_TypeError, |
| 5382 | "spawnvpe() arg 3 must be a mapping object"); |
| 5383 | goto fail_0; |
| 5384 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5385 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5386 | argvlist = PyMem_NEW(char *, argc+1); |
| 5387 | if (argvlist == NULL) { |
| 5388 | PyErr_NoMemory(); |
| 5389 | goto fail_0; |
| 5390 | } |
| 5391 | for (i = 0; i < argc; i++) { |
| 5392 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5393 | &argvlist[i])) |
| 5394 | { |
| 5395 | lastarg = i; |
| 5396 | goto fail_1; |
| 5397 | } |
| 5398 | } |
| 5399 | lastarg = argc; |
| 5400 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5402 | envlist = parse_envlist(env, &envc); |
| 5403 | if (envlist == NULL) |
| 5404 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5405 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5406 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5407 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5408 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5409 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5410 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5411 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5412 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5413 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5414 | if (spawnval == -1) |
| 5415 | (void) posix_error(); |
| 5416 | else |
| 5417 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5418 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5419 | while (--envc >= 0) |
| 5420 | PyMem_DEL(envlist[envc]); |
| 5421 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5422 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5423 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5424 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5425 | Py_DECREF(opath); |
| 5426 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5427 | } |
| 5428 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5429 | #endif /* HAVE_SPAWNV */ |
| 5430 | |
| 5431 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5432 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5433 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5434 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5435 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 5436 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5437 | 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] | 5438 | |
| 5439 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5440 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5441 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5442 | pid_t pid; |
| 5443 | int result = 0; |
| 5444 | _PyImport_AcquireLock(); |
| 5445 | pid = fork1(); |
| 5446 | if (pid == 0) { |
| 5447 | /* child: this clobbers and resets the import lock. */ |
| 5448 | PyOS_AfterFork(); |
| 5449 | } else { |
| 5450 | /* parent: release the import lock. */ |
| 5451 | result = _PyImport_ReleaseLock(); |
| 5452 | } |
| 5453 | if (pid == -1) |
| 5454 | return posix_error(); |
| 5455 | if (result < 0) { |
| 5456 | /* Don't clobber the OSError if the fork failed. */ |
| 5457 | PyErr_SetString(PyExc_RuntimeError, |
| 5458 | "not holding the import lock"); |
| 5459 | return NULL; |
| 5460 | } |
| 5461 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5462 | } |
| 5463 | #endif |
| 5464 | |
| 5465 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5466 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5467 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5468 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5469 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5470 | 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] | 5471 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5472 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5473 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5474 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5475 | pid_t pid; |
| 5476 | int result = 0; |
| 5477 | _PyImport_AcquireLock(); |
| 5478 | pid = fork(); |
| 5479 | if (pid == 0) { |
| 5480 | /* child: this clobbers and resets the import lock. */ |
| 5481 | PyOS_AfterFork(); |
| 5482 | } else { |
| 5483 | /* parent: release the import lock. */ |
| 5484 | result = _PyImport_ReleaseLock(); |
| 5485 | } |
| 5486 | if (pid == -1) |
| 5487 | return posix_error(); |
| 5488 | if (result < 0) { |
| 5489 | /* Don't clobber the OSError if the fork failed. */ |
| 5490 | PyErr_SetString(PyExc_RuntimeError, |
| 5491 | "not holding the import lock"); |
| 5492 | return NULL; |
| 5493 | } |
| 5494 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5495 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5496 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5497 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5498 | #ifdef HAVE_SCHED_H |
| 5499 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5500 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5501 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5502 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5503 | "sched_get_priority_max(policy)\n\n\ |
| 5504 | Get the maximum scheduling priority for *policy*."); |
| 5505 | |
| 5506 | static PyObject * |
| 5507 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5508 | { |
| 5509 | int policy, max; |
| 5510 | |
| 5511 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5512 | return NULL; |
| 5513 | max = sched_get_priority_max(policy); |
| 5514 | if (max < 0) |
| 5515 | return posix_error(); |
| 5516 | return PyLong_FromLong(max); |
| 5517 | } |
| 5518 | |
| 5519 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5520 | "sched_get_priority_min(policy)\n\n\ |
| 5521 | Get the minimum scheduling priority for *policy*."); |
| 5522 | |
| 5523 | static PyObject * |
| 5524 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5525 | { |
| 5526 | int policy, min; |
| 5527 | |
| 5528 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5529 | return NULL; |
| 5530 | min = sched_get_priority_min(policy); |
| 5531 | if (min < 0) |
| 5532 | return posix_error(); |
| 5533 | return PyLong_FromLong(min); |
| 5534 | } |
| 5535 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5536 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5537 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5538 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5539 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5540 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5541 | "sched_getscheduler(pid)\n\n\ |
| 5542 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5543 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5544 | |
| 5545 | static PyObject * |
| 5546 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5547 | { |
| 5548 | pid_t pid; |
| 5549 | int policy; |
| 5550 | |
| 5551 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5552 | return NULL; |
| 5553 | policy = sched_getscheduler(pid); |
| 5554 | if (policy < 0) |
| 5555 | return posix_error(); |
| 5556 | return PyLong_FromLong(policy); |
| 5557 | } |
| 5558 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5559 | #endif |
| 5560 | |
| 5561 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5562 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5563 | static PyObject * |
| 5564 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5565 | { |
| 5566 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5567 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5568 | |
| 5569 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5570 | return NULL; |
| 5571 | res = PyStructSequence_New(type); |
| 5572 | if (!res) |
| 5573 | return NULL; |
| 5574 | Py_INCREF(priority); |
| 5575 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5576 | return res; |
| 5577 | } |
| 5578 | |
| 5579 | PyDoc_STRVAR(sched_param__doc__, |
| 5580 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5581 | Current has only one field: sched_priority"); |
| 5582 | |
| 5583 | static PyStructSequence_Field sched_param_fields[] = { |
| 5584 | {"sched_priority", "the scheduling priority"}, |
| 5585 | {0} |
| 5586 | }; |
| 5587 | |
| 5588 | static PyStructSequence_Desc sched_param_desc = { |
| 5589 | "sched_param", /* name */ |
| 5590 | sched_param__doc__, /* doc */ |
| 5591 | sched_param_fields, |
| 5592 | 1 |
| 5593 | }; |
| 5594 | |
| 5595 | static int |
| 5596 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5597 | { |
| 5598 | long priority; |
| 5599 | |
| 5600 | if (Py_TYPE(param) != &SchedParamType) { |
| 5601 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5602 | return 0; |
| 5603 | } |
| 5604 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5605 | if (priority == -1 && PyErr_Occurred()) |
| 5606 | return 0; |
| 5607 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5608 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5609 | return 0; |
| 5610 | } |
| 5611 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5612 | return 1; |
| 5613 | } |
| 5614 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5615 | #endif |
| 5616 | |
| 5617 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5618 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5619 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5620 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5621 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5622 | If *pid* is 0, the calling process is changed.\n\ |
| 5623 | *param* is an instance of sched_param."); |
| 5624 | |
| 5625 | static PyObject * |
| 5626 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5627 | { |
| 5628 | pid_t pid; |
| 5629 | int policy; |
| 5630 | struct sched_param param; |
| 5631 | |
| 5632 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5633 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5634 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5635 | |
| 5636 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5637 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5638 | ** scheduling policy under Solaris/Illumos, and others. |
| 5639 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5640 | */ |
| 5641 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5642 | return posix_error(); |
| 5643 | Py_RETURN_NONE; |
| 5644 | } |
| 5645 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5646 | #endif |
| 5647 | |
| 5648 | #ifdef HAVE_SCHED_SETPARAM |
| 5649 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5650 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5651 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5652 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5653 | sched_param class. A PID of 0 means the calling process."); |
| 5654 | |
| 5655 | static PyObject * |
| 5656 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5657 | { |
| 5658 | pid_t pid; |
| 5659 | struct sched_param param; |
| 5660 | PyObject *res, *priority; |
| 5661 | |
| 5662 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5663 | return NULL; |
| 5664 | if (sched_getparam(pid, ¶m)) |
| 5665 | return posix_error(); |
| 5666 | res = PyStructSequence_New(&SchedParamType); |
| 5667 | if (!res) |
| 5668 | return NULL; |
| 5669 | priority = PyLong_FromLong(param.sched_priority); |
| 5670 | if (!priority) { |
| 5671 | Py_DECREF(res); |
| 5672 | return NULL; |
| 5673 | } |
| 5674 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5675 | return res; |
| 5676 | } |
| 5677 | |
| 5678 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5679 | "sched_setparam(pid, param)\n\n\ |
| 5680 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5681 | A PID of 0 means the calling process."); |
| 5682 | |
| 5683 | static PyObject * |
| 5684 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5685 | { |
| 5686 | pid_t pid; |
| 5687 | struct sched_param param; |
| 5688 | |
| 5689 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5690 | &pid, &convert_sched_param, ¶m)) |
| 5691 | return NULL; |
| 5692 | if (sched_setparam(pid, ¶m)) |
| 5693 | return posix_error(); |
| 5694 | Py_RETURN_NONE; |
| 5695 | } |
| 5696 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5697 | #endif |
| 5698 | |
| 5699 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5700 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5701 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5702 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5703 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5704 | |
| 5705 | static PyObject * |
| 5706 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5707 | { |
| 5708 | pid_t pid; |
| 5709 | struct timespec interval; |
| 5710 | |
| 5711 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5712 | return NULL; |
| 5713 | if (sched_rr_get_interval(pid, &interval)) |
| 5714 | return posix_error(); |
| 5715 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5716 | } |
| 5717 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5718 | #endif |
| 5719 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5720 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5721 | "sched_yield()\n\n\ |
| 5722 | Voluntarily relinquish the CPU."); |
| 5723 | |
| 5724 | static PyObject * |
| 5725 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5726 | { |
| 5727 | if (sched_yield()) |
| 5728 | return posix_error(); |
| 5729 | Py_RETURN_NONE; |
| 5730 | } |
| 5731 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5732 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5733 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5734 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5735 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5736 | |
| 5737 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5738 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5739 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5740 | |
| 5741 | static PyObject * |
| 5742 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5743 | { |
| 5744 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5745 | int ncpus; |
| 5746 | size_t setsize; |
| 5747 | cpu_set_t *mask = NULL; |
| 5748 | PyObject *iterable, *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5749 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5750 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity", |
| 5751 | &pid, &iterable)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5752 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5753 | |
| 5754 | iterator = PyObject_GetIter(iterable); |
| 5755 | if (iterator == NULL) |
| 5756 | return NULL; |
| 5757 | |
| 5758 | ncpus = NCPUS_START; |
| 5759 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5760 | mask = CPU_ALLOC(ncpus); |
| 5761 | if (mask == NULL) { |
| 5762 | PyErr_NoMemory(); |
| 5763 | goto error; |
| 5764 | } |
| 5765 | CPU_ZERO_S(setsize, mask); |
| 5766 | |
| 5767 | while ((item = PyIter_Next(iterator))) { |
| 5768 | long cpu; |
| 5769 | if (!PyLong_Check(item)) { |
| 5770 | PyErr_Format(PyExc_TypeError, |
| 5771 | "expected an iterator of ints, " |
| 5772 | "but iterator yielded %R", |
| 5773 | Py_TYPE(item)); |
| 5774 | Py_DECREF(item); |
| 5775 | goto error; |
| 5776 | } |
| 5777 | cpu = PyLong_AsLong(item); |
| 5778 | Py_DECREF(item); |
| 5779 | if (cpu < 0) { |
| 5780 | if (!PyErr_Occurred()) |
| 5781 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5782 | goto error; |
| 5783 | } |
| 5784 | if (cpu > INT_MAX - 1) { |
| 5785 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5786 | goto error; |
| 5787 | } |
| 5788 | if (cpu >= ncpus) { |
| 5789 | /* Grow CPU mask to fit the CPU number */ |
| 5790 | int newncpus = ncpus; |
| 5791 | cpu_set_t *newmask; |
| 5792 | size_t newsetsize; |
| 5793 | while (newncpus <= cpu) { |
| 5794 | if (newncpus > INT_MAX / 2) |
| 5795 | newncpus = cpu + 1; |
| 5796 | else |
| 5797 | newncpus = newncpus * 2; |
| 5798 | } |
| 5799 | newmask = CPU_ALLOC(newncpus); |
| 5800 | if (newmask == NULL) { |
| 5801 | PyErr_NoMemory(); |
| 5802 | goto error; |
| 5803 | } |
| 5804 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5805 | CPU_ZERO_S(newsetsize, newmask); |
| 5806 | memcpy(newmask, mask, setsize); |
| 5807 | CPU_FREE(mask); |
| 5808 | setsize = newsetsize; |
| 5809 | mask = newmask; |
| 5810 | ncpus = newncpus; |
| 5811 | } |
| 5812 | CPU_SET_S(cpu, setsize, mask); |
| 5813 | } |
| 5814 | Py_CLEAR(iterator); |
| 5815 | |
| 5816 | if (sched_setaffinity(pid, setsize, mask)) { |
| 5817 | posix_error(); |
| 5818 | goto error; |
| 5819 | } |
| 5820 | CPU_FREE(mask); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5821 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5822 | |
| 5823 | error: |
| 5824 | if (mask) |
| 5825 | CPU_FREE(mask); |
| 5826 | Py_XDECREF(iterator); |
| 5827 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5828 | } |
| 5829 | |
| 5830 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5831 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5832 | Return the affinity of the process with PID *pid*.\n\ |
| 5833 | The returned cpu_set will be of size *ncpus*."); |
| 5834 | |
| 5835 | static PyObject * |
| 5836 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5837 | { |
| 5838 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5839 | int cpu, ncpus, count; |
| 5840 | size_t setsize; |
| 5841 | cpu_set_t *mask = NULL; |
| 5842 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5843 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5844 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity", |
| 5845 | &pid)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5846 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5847 | |
| 5848 | ncpus = NCPUS_START; |
| 5849 | while (1) { |
| 5850 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5851 | mask = CPU_ALLOC(ncpus); |
| 5852 | if (mask == NULL) |
| 5853 | return PyErr_NoMemory(); |
| 5854 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5855 | break; |
| 5856 | CPU_FREE(mask); |
| 5857 | if (errno != EINVAL) |
| 5858 | return posix_error(); |
| 5859 | if (ncpus > INT_MAX / 2) { |
| 5860 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5861 | "a large enough CPU set"); |
| 5862 | return NULL; |
| 5863 | } |
| 5864 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5865 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5866 | |
| 5867 | res = PySet_New(NULL); |
| 5868 | if (res == NULL) |
| 5869 | goto error; |
| 5870 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5871 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5872 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5873 | --count; |
| 5874 | if (cpu_num == NULL) |
| 5875 | goto error; |
| 5876 | if (PySet_Add(res, cpu_num)) { |
| 5877 | Py_DECREF(cpu_num); |
| 5878 | goto error; |
| 5879 | } |
| 5880 | Py_DECREF(cpu_num); |
| 5881 | } |
| 5882 | } |
| 5883 | CPU_FREE(mask); |
| 5884 | return res; |
| 5885 | |
| 5886 | error: |
| 5887 | if (mask) |
| 5888 | CPU_FREE(mask); |
| 5889 | Py_XDECREF(res); |
| 5890 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5891 | } |
| 5892 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5893 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5894 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5895 | #endif /* HAVE_SCHED_H */ |
| 5896 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5897 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5898 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5899 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5900 | #define DEV_PTY_FILE "/dev/ptc" |
| 5901 | #define HAVE_DEV_PTMX |
| 5902 | #else |
| 5903 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5904 | #endif |
| 5905 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5906 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5907 | #ifdef HAVE_PTY_H |
| 5908 | #include <pty.h> |
| 5909 | #else |
| 5910 | #ifdef HAVE_LIBUTIL_H |
| 5911 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5912 | #else |
| 5913 | #ifdef HAVE_UTIL_H |
| 5914 | #include <util.h> |
| 5915 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5916 | #endif /* HAVE_LIBUTIL_H */ |
| 5917 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5918 | #ifdef HAVE_STROPTS_H |
| 5919 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5920 | #endif |
| 5921 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5922 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5923 | #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] | 5924 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5925 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5926 | 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] | 5927 | |
| 5928 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5929 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5930 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5931 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5932 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5933 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5934 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5935 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5936 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5937 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5938 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5939 | #endif |
| 5940 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5941 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5942 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5943 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5944 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5945 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5946 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5947 | if (slave_name == NULL) |
| 5948 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5949 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5950 | slave_fd = open(slave_name, O_RDWR); |
| 5951 | if (slave_fd < 0) |
| 5952 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5953 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5954 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5955 | if (master_fd < 0) |
| 5956 | return posix_error(); |
| 5957 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5958 | /* change permission of slave */ |
| 5959 | if (grantpt(master_fd) < 0) { |
| 5960 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5961 | return posix_error(); |
| 5962 | } |
| 5963 | /* unlock slave */ |
| 5964 | if (unlockpt(master_fd) < 0) { |
| 5965 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5966 | return posix_error(); |
| 5967 | } |
| 5968 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5969 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5970 | if (slave_name == NULL) |
| 5971 | return posix_error(); |
| 5972 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5973 | if (slave_fd < 0) |
| 5974 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5975 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5976 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5977 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5978 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5979 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5980 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5981 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5982 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5983 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5984 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5985 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5986 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5987 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5988 | |
| 5989 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5990 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5991 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5992 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5993 | 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] | 5994 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5995 | |
| 5996 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5997 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5998 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5999 | int master_fd = -1, result = 0; |
| 6000 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6001 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6002 | _PyImport_AcquireLock(); |
| 6003 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6004 | if (pid == 0) { |
| 6005 | /* child: this clobbers and resets the import lock. */ |
| 6006 | PyOS_AfterFork(); |
| 6007 | } else { |
| 6008 | /* parent: release the import lock. */ |
| 6009 | result = _PyImport_ReleaseLock(); |
| 6010 | } |
| 6011 | if (pid == -1) |
| 6012 | return posix_error(); |
| 6013 | if (result < 0) { |
| 6014 | /* Don't clobber the OSError if the fork failed. */ |
| 6015 | PyErr_SetString(PyExc_RuntimeError, |
| 6016 | "not holding the import lock"); |
| 6017 | return NULL; |
| 6018 | } |
| 6019 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6020 | } |
| 6021 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6022 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6023 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6024 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6025 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6026 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6027 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6028 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6029 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6030 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6031 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6032 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6033 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6034 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6035 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6036 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6037 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6038 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6039 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6040 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6041 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6042 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6043 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6044 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6045 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6046 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6047 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6048 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6049 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6050 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6051 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6052 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6053 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6054 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6055 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6056 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6057 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6058 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6059 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6060 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6061 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6062 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6063 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6064 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6065 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6066 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6067 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6068 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6069 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6070 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6071 | } |
| 6072 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6073 | #ifdef HAVE_GETGROUPLIST |
| 6074 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6075 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6076 | Returns a list of groups to which a user belongs.\n\n\ |
| 6077 | user: username to lookup\n\ |
| 6078 | group: base group id of the user"); |
| 6079 | |
| 6080 | static PyObject * |
| 6081 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6082 | { |
| 6083 | #ifdef NGROUPS_MAX |
| 6084 | #define MAX_GROUPS NGROUPS_MAX |
| 6085 | #else |
| 6086 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6087 | #define MAX_GROUPS 64 |
| 6088 | #endif |
| 6089 | |
| 6090 | const char *user; |
| 6091 | int i, ngroups; |
| 6092 | PyObject *list; |
| 6093 | #ifdef __APPLE__ |
| 6094 | int *groups, basegid; |
| 6095 | #else |
| 6096 | gid_t *groups, basegid; |
| 6097 | #endif |
| 6098 | ngroups = MAX_GROUPS; |
| 6099 | |
| 6100 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 6101 | return NULL; |
| 6102 | |
| 6103 | #ifdef __APPLE__ |
| 6104 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 6105 | #else |
| 6106 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 6107 | #endif |
| 6108 | if (groups == NULL) |
| 6109 | return PyErr_NoMemory(); |
| 6110 | |
| 6111 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6112 | PyMem_Del(groups); |
| 6113 | return posix_error(); |
| 6114 | } |
| 6115 | |
| 6116 | list = PyList_New(ngroups); |
| 6117 | if (list == NULL) { |
| 6118 | PyMem_Del(groups); |
| 6119 | return NULL; |
| 6120 | } |
| 6121 | |
| 6122 | for (i = 0; i < ngroups; i++) { |
| 6123 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 6124 | if (o == NULL) { |
| 6125 | Py_DECREF(list); |
| 6126 | PyMem_Del(groups); |
| 6127 | return NULL; |
| 6128 | } |
| 6129 | PyList_SET_ITEM(list, i, o); |
| 6130 | } |
| 6131 | |
| 6132 | PyMem_Del(groups); |
| 6133 | |
| 6134 | return list; |
| 6135 | } |
| 6136 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6137 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6138 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6139 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6140 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6141 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6142 | |
| 6143 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6144 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6145 | { |
| 6146 | PyObject *result = NULL; |
| 6147 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6148 | #ifdef NGROUPS_MAX |
| 6149 | #define MAX_GROUPS NGROUPS_MAX |
| 6150 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6151 | /* 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] | 6152 | #define MAX_GROUPS 64 |
| 6153 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6154 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6155 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6156 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6157 | * This is a helper variable to store the intermediate result when |
| 6158 | * that happens. |
| 6159 | * |
| 6160 | * To keep the code readable the OSX behaviour is unconditional, |
| 6161 | * according to the POSIX spec this should be safe on all unix-y |
| 6162 | * systems. |
| 6163 | */ |
| 6164 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6165 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6166 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6167 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6168 | if (n < 0) { |
| 6169 | if (errno == EINVAL) { |
| 6170 | n = getgroups(0, NULL); |
| 6171 | if (n == -1) { |
| 6172 | return posix_error(); |
| 6173 | } |
| 6174 | if (n == 0) { |
| 6175 | /* Avoid malloc(0) */ |
| 6176 | alt_grouplist = grouplist; |
| 6177 | } else { |
| 6178 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6179 | if (alt_grouplist == NULL) { |
| 6180 | errno = EINVAL; |
| 6181 | return posix_error(); |
| 6182 | } |
| 6183 | n = getgroups(n, alt_grouplist); |
| 6184 | if (n == -1) { |
| 6185 | PyMem_Free(alt_grouplist); |
| 6186 | return posix_error(); |
| 6187 | } |
| 6188 | } |
| 6189 | } else { |
| 6190 | return posix_error(); |
| 6191 | } |
| 6192 | } |
| 6193 | result = PyList_New(n); |
| 6194 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6195 | int i; |
| 6196 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6197 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6198 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6199 | Py_DECREF(result); |
| 6200 | result = NULL; |
| 6201 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6202 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6203 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6204 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | if (alt_grouplist != grouplist) { |
| 6208 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6209 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6210 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6211 | return result; |
| 6212 | } |
| 6213 | #endif |
| 6214 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6215 | #ifdef HAVE_INITGROUPS |
| 6216 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6217 | "initgroups(username, gid) -> None\n\n\ |
| 6218 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6219 | the groups of which the specified username is a member, plus the specified\n\ |
| 6220 | group id."); |
| 6221 | |
| 6222 | static PyObject * |
| 6223 | posix_initgroups(PyObject *self, PyObject *args) |
| 6224 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6225 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6226 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6227 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6228 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6229 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6230 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 6231 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6232 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6233 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6234 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6235 | res = initgroups(username, (gid_t) gid); |
| 6236 | Py_DECREF(oname); |
| 6237 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6238 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6239 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6240 | Py_INCREF(Py_None); |
| 6241 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6242 | } |
| 6243 | #endif |
| 6244 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6245 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6246 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6247 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6248 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6249 | |
| 6250 | static PyObject * |
| 6251 | posix_getpgid(PyObject *self, PyObject *args) |
| 6252 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6253 | pid_t pid, pgid; |
| 6254 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 6255 | return NULL; |
| 6256 | pgid = getpgid(pid); |
| 6257 | if (pgid < 0) |
| 6258 | return posix_error(); |
| 6259 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6260 | } |
| 6261 | #endif /* HAVE_GETPGID */ |
| 6262 | |
| 6263 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6264 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6265 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6266 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6267 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6268 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6269 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6270 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6271 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6272 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6273 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6274 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6275 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6276 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6277 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6278 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6279 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6280 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6281 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6282 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6283 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 6284 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6285 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6286 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6287 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6288 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6289 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6290 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6291 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6292 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6293 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6294 | return posix_error(); |
| 6295 | Py_INCREF(Py_None); |
| 6296 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6297 | } |
| 6298 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6299 | #endif /* HAVE_SETPGRP */ |
| 6300 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6301 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6302 | |
| 6303 | #ifdef MS_WINDOWS |
| 6304 | #include <tlhelp32.h> |
| 6305 | |
| 6306 | static PyObject* |
| 6307 | win32_getppid() |
| 6308 | { |
| 6309 | HANDLE snapshot; |
| 6310 | pid_t mypid; |
| 6311 | PyObject* result = NULL; |
| 6312 | BOOL have_record; |
| 6313 | PROCESSENTRY32 pe; |
| 6314 | |
| 6315 | mypid = getpid(); /* This function never fails */ |
| 6316 | |
| 6317 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6318 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6319 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6320 | |
| 6321 | pe.dwSize = sizeof(pe); |
| 6322 | have_record = Process32First(snapshot, &pe); |
| 6323 | while (have_record) { |
| 6324 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6325 | /* We could cache the ulong value in a static variable. */ |
| 6326 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6327 | break; |
| 6328 | } |
| 6329 | |
| 6330 | have_record = Process32Next(snapshot, &pe); |
| 6331 | } |
| 6332 | |
| 6333 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6334 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6335 | * error anyway, so let's raise it. */ |
| 6336 | if (!result) |
| 6337 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6338 | |
| 6339 | CloseHandle(snapshot); |
| 6340 | |
| 6341 | return result; |
| 6342 | } |
| 6343 | #endif /*MS_WINDOWS*/ |
| 6344 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6345 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6346 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6347 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6348 | Windows machines will still return its id; others systems will return the id\n\ |
| 6349 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6350 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6351 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6352 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6353 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6354 | #ifdef MS_WINDOWS |
| 6355 | return win32_getppid(); |
| 6356 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6357 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6358 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6359 | } |
| 6360 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6361 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6362 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6363 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6364 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6365 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6366 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6367 | |
| 6368 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6369 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6370 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6371 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6372 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6373 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6374 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6375 | |
| 6376 | if (GetUserNameW(user_name, &num_chars)) { |
| 6377 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6378 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6379 | } |
| 6380 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6381 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6382 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6383 | char *name; |
| 6384 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6385 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6386 | errno = 0; |
| 6387 | name = getlogin(); |
| 6388 | if (name == NULL) { |
| 6389 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6390 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6391 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6392 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6393 | } |
| 6394 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6395 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6396 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6397 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6398 | return result; |
| 6399 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6400 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6401 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6402 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6403 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6404 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6405 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6406 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6407 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6408 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6409 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6410 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6411 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6412 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6413 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6414 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6415 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6416 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6417 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6418 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6419 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6420 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6421 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6422 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6423 | pid_t pid; |
| 6424 | int sig; |
| 6425 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6426 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6427 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6428 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 6429 | APIRET rc; |
| 6430 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6431 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6432 | |
| 6433 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 6434 | APIRET rc; |
| 6435 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6436 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6437 | |
| 6438 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 6439 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6440 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6441 | if (kill(pid, sig) == -1) |
| 6442 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6443 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6444 | Py_INCREF(Py_None); |
| 6445 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6446 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6447 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6448 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6449 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6450 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6451 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6452 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6453 | |
| 6454 | static PyObject * |
| 6455 | posix_killpg(PyObject *self, PyObject *args) |
| 6456 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6457 | int sig; |
| 6458 | pid_t pgid; |
| 6459 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6460 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6461 | take the same type. Moreover, pid_t is always at least as wide as |
| 6462 | int (else compilation of this module fails), which is safe. */ |
| 6463 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6464 | return NULL; |
| 6465 | if (killpg(pgid, sig) == -1) |
| 6466 | return posix_error(); |
| 6467 | Py_INCREF(Py_None); |
| 6468 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6469 | } |
| 6470 | #endif |
| 6471 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6472 | #ifdef MS_WINDOWS |
| 6473 | PyDoc_STRVAR(win32_kill__doc__, |
| 6474 | "kill(pid, sig)\n\n\ |
| 6475 | Kill a process with a signal."); |
| 6476 | |
| 6477 | static PyObject * |
| 6478 | win32_kill(PyObject *self, PyObject *args) |
| 6479 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6480 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6481 | DWORD pid, sig, err; |
| 6482 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6483 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6484 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 6485 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6486 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6487 | /* Console processes which share a common console can be sent CTRL+C or |
| 6488 | CTRL+BREAK events, provided they handle said events. */ |
| 6489 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 6490 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 6491 | err = GetLastError(); |
| 6492 | PyErr_SetFromWindowsErr(err); |
| 6493 | } |
| 6494 | else |
| 6495 | Py_RETURN_NONE; |
| 6496 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6497 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6498 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6499 | attempt to open and terminate the process. */ |
| 6500 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 6501 | if (handle == NULL) { |
| 6502 | err = GetLastError(); |
| 6503 | return PyErr_SetFromWindowsErr(err); |
| 6504 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6505 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6506 | if (TerminateProcess(handle, sig) == 0) { |
| 6507 | err = GetLastError(); |
| 6508 | result = PyErr_SetFromWindowsErr(err); |
| 6509 | } else { |
| 6510 | Py_INCREF(Py_None); |
| 6511 | result = Py_None; |
| 6512 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6513 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6514 | CloseHandle(handle); |
| 6515 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6516 | } |
| 6517 | #endif /* MS_WINDOWS */ |
| 6518 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6519 | #ifdef HAVE_PLOCK |
| 6520 | |
| 6521 | #ifdef HAVE_SYS_LOCK_H |
| 6522 | #include <sys/lock.h> |
| 6523 | #endif |
| 6524 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6525 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6526 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6527 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6528 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6529 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6530 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6531 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6532 | int op; |
| 6533 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6534 | return NULL; |
| 6535 | if (plock(op) == -1) |
| 6536 | return posix_error(); |
| 6537 | Py_INCREF(Py_None); |
| 6538 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6539 | } |
| 6540 | #endif |
| 6541 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6542 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6543 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6544 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6545 | Set the current process's user id."); |
| 6546 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6547 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6548 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6549 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6550 | long uid_arg; |
| 6551 | uid_t uid; |
| 6552 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 6553 | return NULL; |
| 6554 | uid = uid_arg; |
| 6555 | if (uid != uid_arg) { |
| 6556 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6557 | return NULL; |
| 6558 | } |
| 6559 | if (setuid(uid) < 0) |
| 6560 | return posix_error(); |
| 6561 | Py_INCREF(Py_None); |
| 6562 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6563 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6564 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6565 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6566 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6567 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6568 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6569 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6570 | Set the current process's effective user id."); |
| 6571 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6572 | static PyObject * |
| 6573 | posix_seteuid (PyObject *self, PyObject *args) |
| 6574 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6575 | long euid_arg; |
| 6576 | uid_t euid; |
| 6577 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 6578 | return NULL; |
| 6579 | euid = euid_arg; |
| 6580 | if (euid != euid_arg) { |
| 6581 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6582 | return NULL; |
| 6583 | } |
| 6584 | if (seteuid(euid) < 0) { |
| 6585 | return posix_error(); |
| 6586 | } else { |
| 6587 | Py_INCREF(Py_None); |
| 6588 | return Py_None; |
| 6589 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6590 | } |
| 6591 | #endif /* HAVE_SETEUID */ |
| 6592 | |
| 6593 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6594 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6595 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6596 | Set the current process's effective group id."); |
| 6597 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6598 | static PyObject * |
| 6599 | posix_setegid (PyObject *self, PyObject *args) |
| 6600 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6601 | long egid_arg; |
| 6602 | gid_t egid; |
| 6603 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 6604 | return NULL; |
| 6605 | egid = egid_arg; |
| 6606 | if (egid != egid_arg) { |
| 6607 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6608 | return NULL; |
| 6609 | } |
| 6610 | if (setegid(egid) < 0) { |
| 6611 | return posix_error(); |
| 6612 | } else { |
| 6613 | Py_INCREF(Py_None); |
| 6614 | return Py_None; |
| 6615 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6616 | } |
| 6617 | #endif /* HAVE_SETEGID */ |
| 6618 | |
| 6619 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6620 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6621 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6622 | Set the current process's real and effective user ids."); |
| 6623 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6624 | static PyObject * |
| 6625 | posix_setreuid (PyObject *self, PyObject *args) |
| 6626 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6627 | long ruid_arg, euid_arg; |
| 6628 | uid_t ruid, euid; |
| 6629 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 6630 | return NULL; |
| 6631 | if (ruid_arg == -1) |
| 6632 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 6633 | else |
| 6634 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 6635 | if (euid_arg == -1) |
| 6636 | euid = (uid_t)-1; |
| 6637 | else |
| 6638 | euid = euid_arg; |
| 6639 | if ((euid_arg != -1 && euid != euid_arg) || |
| 6640 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 6641 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6642 | return NULL; |
| 6643 | } |
| 6644 | if (setreuid(ruid, euid) < 0) { |
| 6645 | return posix_error(); |
| 6646 | } else { |
| 6647 | Py_INCREF(Py_None); |
| 6648 | return Py_None; |
| 6649 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6650 | } |
| 6651 | #endif /* HAVE_SETREUID */ |
| 6652 | |
| 6653 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6654 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6655 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6656 | Set the current process's real and effective group ids."); |
| 6657 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6658 | static PyObject * |
| 6659 | posix_setregid (PyObject *self, PyObject *args) |
| 6660 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6661 | long rgid_arg, egid_arg; |
| 6662 | gid_t rgid, egid; |
| 6663 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 6664 | return NULL; |
| 6665 | if (rgid_arg == -1) |
| 6666 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 6667 | else |
| 6668 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6669 | if (egid_arg == -1) |
| 6670 | egid = (gid_t)-1; |
| 6671 | else |
| 6672 | egid = egid_arg; |
| 6673 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6674 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6675 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6676 | return NULL; |
| 6677 | } |
| 6678 | if (setregid(rgid, egid) < 0) { |
| 6679 | return posix_error(); |
| 6680 | } else { |
| 6681 | Py_INCREF(Py_None); |
| 6682 | return Py_None; |
| 6683 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6684 | } |
| 6685 | #endif /* HAVE_SETREGID */ |
| 6686 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6687 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6688 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6689 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6690 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6691 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6692 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6693 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6694 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6695 | long gid_arg; |
| 6696 | gid_t gid; |
| 6697 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6698 | return NULL; |
| 6699 | gid = gid_arg; |
| 6700 | if (gid != gid_arg) { |
| 6701 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6702 | return NULL; |
| 6703 | } |
| 6704 | if (setgid(gid) < 0) |
| 6705 | return posix_error(); |
| 6706 | Py_INCREF(Py_None); |
| 6707 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6708 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6709 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6710 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6711 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6712 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6713 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6714 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6715 | |
| 6716 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6717 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6718 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6719 | int i, len; |
| 6720 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6721 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6722 | if (!PySequence_Check(groups)) { |
| 6723 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6724 | return NULL; |
| 6725 | } |
| 6726 | len = PySequence_Size(groups); |
| 6727 | if (len > MAX_GROUPS) { |
| 6728 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6729 | return NULL; |
| 6730 | } |
| 6731 | for(i = 0; i < len; i++) { |
| 6732 | PyObject *elem; |
| 6733 | elem = PySequence_GetItem(groups, i); |
| 6734 | if (!elem) |
| 6735 | return NULL; |
| 6736 | if (!PyLong_Check(elem)) { |
| 6737 | PyErr_SetString(PyExc_TypeError, |
| 6738 | "groups must be integers"); |
| 6739 | Py_DECREF(elem); |
| 6740 | return NULL; |
| 6741 | } else { |
| 6742 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6743 | if (PyErr_Occurred()) { |
| 6744 | PyErr_SetString(PyExc_TypeError, |
| 6745 | "group id too big"); |
| 6746 | Py_DECREF(elem); |
| 6747 | return NULL; |
| 6748 | } |
| 6749 | grouplist[i] = x; |
| 6750 | /* read back the value to see if it fitted in gid_t */ |
| 6751 | if (grouplist[i] != x) { |
| 6752 | PyErr_SetString(PyExc_TypeError, |
| 6753 | "group id too big"); |
| 6754 | Py_DECREF(elem); |
| 6755 | return NULL; |
| 6756 | } |
| 6757 | } |
| 6758 | Py_DECREF(elem); |
| 6759 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6760 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6761 | if (setgroups(len, grouplist) < 0) |
| 6762 | return posix_error(); |
| 6763 | Py_INCREF(Py_None); |
| 6764 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6765 | } |
| 6766 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6767 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6768 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6769 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6770 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6771 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6772 | PyObject *result; |
| 6773 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6774 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6775 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6776 | if (pid == -1) |
| 6777 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6778 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6779 | if (struct_rusage == NULL) { |
| 6780 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6781 | if (m == NULL) |
| 6782 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6783 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6784 | Py_DECREF(m); |
| 6785 | if (struct_rusage == NULL) |
| 6786 | return NULL; |
| 6787 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6788 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6789 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6790 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6791 | if (!result) |
| 6792 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6793 | |
| 6794 | #ifndef doubletime |
| 6795 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6796 | #endif |
| 6797 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6798 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6799 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6800 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6801 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6802 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6803 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6804 | SET_INT(result, 2, ru->ru_maxrss); |
| 6805 | SET_INT(result, 3, ru->ru_ixrss); |
| 6806 | SET_INT(result, 4, ru->ru_idrss); |
| 6807 | SET_INT(result, 5, ru->ru_isrss); |
| 6808 | SET_INT(result, 6, ru->ru_minflt); |
| 6809 | SET_INT(result, 7, ru->ru_majflt); |
| 6810 | SET_INT(result, 8, ru->ru_nswap); |
| 6811 | SET_INT(result, 9, ru->ru_inblock); |
| 6812 | SET_INT(result, 10, ru->ru_oublock); |
| 6813 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6814 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6815 | SET_INT(result, 13, ru->ru_nsignals); |
| 6816 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6817 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6818 | #undef SET_INT |
| 6819 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6820 | if (PyErr_Occurred()) { |
| 6821 | Py_DECREF(result); |
| 6822 | return NULL; |
| 6823 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6824 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6825 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6826 | } |
| 6827 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6828 | |
| 6829 | #ifdef HAVE_WAIT3 |
| 6830 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6831 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6832 | Wait for completion of a child process."); |
| 6833 | |
| 6834 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6835 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6836 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6837 | pid_t pid; |
| 6838 | int options; |
| 6839 | struct rusage ru; |
| 6840 | WAIT_TYPE status; |
| 6841 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6842 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6843 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6844 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6845 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6846 | Py_BEGIN_ALLOW_THREADS |
| 6847 | pid = wait3(&status, options, &ru); |
| 6848 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6849 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6850 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6851 | } |
| 6852 | #endif /* HAVE_WAIT3 */ |
| 6853 | |
| 6854 | #ifdef HAVE_WAIT4 |
| 6855 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6856 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6857 | Wait for completion of a given child process."); |
| 6858 | |
| 6859 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6860 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6861 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6862 | pid_t pid; |
| 6863 | int options; |
| 6864 | struct rusage ru; |
| 6865 | WAIT_TYPE status; |
| 6866 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6867 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6868 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6869 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6870 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6871 | Py_BEGIN_ALLOW_THREADS |
| 6872 | pid = wait4(pid, &status, options, &ru); |
| 6873 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6874 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6875 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6876 | } |
| 6877 | #endif /* HAVE_WAIT4 */ |
| 6878 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6879 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6880 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6881 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6882 | Wait for the completion of one or more child processes.\n\n\ |
| 6883 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6884 | id specifies the pid to wait on.\n\ |
| 6885 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6886 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6887 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6888 | no children in a waitable state."); |
| 6889 | |
| 6890 | static PyObject * |
| 6891 | posix_waitid(PyObject *self, PyObject *args) |
| 6892 | { |
| 6893 | PyObject *result; |
| 6894 | idtype_t idtype; |
| 6895 | id_t id; |
| 6896 | int options, res; |
| 6897 | siginfo_t si; |
| 6898 | si.si_pid = 0; |
| 6899 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6900 | return NULL; |
| 6901 | Py_BEGIN_ALLOW_THREADS |
| 6902 | res = waitid(idtype, id, &si, options); |
| 6903 | Py_END_ALLOW_THREADS |
| 6904 | if (res == -1) |
| 6905 | return posix_error(); |
| 6906 | |
| 6907 | if (si.si_pid == 0) |
| 6908 | Py_RETURN_NONE; |
| 6909 | |
| 6910 | result = PyStructSequence_New(&WaitidResultType); |
| 6911 | if (!result) |
| 6912 | return NULL; |
| 6913 | |
| 6914 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6915 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6916 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6917 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6918 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6919 | if (PyErr_Occurred()) { |
| 6920 | Py_DECREF(result); |
| 6921 | return NULL; |
| 6922 | } |
| 6923 | |
| 6924 | return result; |
| 6925 | } |
| 6926 | #endif |
| 6927 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6928 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6929 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6930 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6931 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6934 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6935 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6936 | pid_t pid; |
| 6937 | int options; |
| 6938 | WAIT_TYPE status; |
| 6939 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6940 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6941 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6942 | return NULL; |
| 6943 | Py_BEGIN_ALLOW_THREADS |
| 6944 | pid = waitpid(pid, &status, options); |
| 6945 | Py_END_ALLOW_THREADS |
| 6946 | if (pid == -1) |
| 6947 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6948 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6949 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6950 | } |
| 6951 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6952 | #elif defined(HAVE_CWAIT) |
| 6953 | |
| 6954 | /* 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] | 6955 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6956 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6957 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6958 | |
| 6959 | static PyObject * |
| 6960 | posix_waitpid(PyObject *self, PyObject *args) |
| 6961 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6962 | Py_intptr_t pid; |
| 6963 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6964 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6965 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6966 | return NULL; |
| 6967 | Py_BEGIN_ALLOW_THREADS |
| 6968 | pid = _cwait(&status, pid, options); |
| 6969 | Py_END_ALLOW_THREADS |
| 6970 | if (pid == -1) |
| 6971 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6972 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6973 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6974 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6975 | } |
| 6976 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6977 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6978 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6979 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6980 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6981 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6982 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6983 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6984 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6985 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6986 | pid_t pid; |
| 6987 | WAIT_TYPE status; |
| 6988 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6989 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6990 | Py_BEGIN_ALLOW_THREADS |
| 6991 | pid = wait(&status); |
| 6992 | Py_END_ALLOW_THREADS |
| 6993 | if (pid == -1) |
| 6994 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6995 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6996 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6997 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6998 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6999 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7000 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7001 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7002 | PyDoc_STRVAR(readlink__doc__, |
| 7003 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7004 | Return a string representing the path to which the symbolic link points.\n\ |
| 7005 | \n\ |
| 7006 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7007 | and path should be relative; path will then be relative to that directory.\n\ |
| 7008 | dir_fd may not be implemented on your platform.\n\ |
| 7009 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7010 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7011 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7012 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7013 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7014 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7015 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7016 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7017 | path_t path; |
| 7018 | int dir_fd = DEFAULT_DIR_FD; |
| 7019 | char buffer[MAXPATHLEN]; |
| 7020 | ssize_t length; |
| 7021 | PyObject *return_value = NULL; |
| 7022 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7023 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7024 | memset(&path, 0, sizeof(path)); |
| 7025 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7026 | path_converter, &path, |
| 7027 | #ifdef HAVE_READLINKAT |
| 7028 | dir_fd_converter, &dir_fd |
| 7029 | #else |
| 7030 | dir_fd_unavailable, &dir_fd |
| 7031 | #endif |
| 7032 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7033 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7034 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7035 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7036 | #ifdef HAVE_READLINKAT |
| 7037 | if (dir_fd != DEFAULT_DIR_FD) |
| 7038 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7039 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7040 | #endif |
| 7041 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7042 | Py_END_ALLOW_THREADS |
| 7043 | |
| 7044 | if (length < 0) { |
| 7045 | return_value = path_posix_error("readlink", &path); |
| 7046 | goto exit; |
| 7047 | } |
| 7048 | |
| 7049 | if (PyUnicode_Check(path.object)) |
| 7050 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7051 | else |
| 7052 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7053 | exit: |
| 7054 | path_cleanup(&path); |
| 7055 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7056 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7057 | |
| 7058 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7059 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7060 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7061 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7062 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7063 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7064 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7065 | Create a symbolic link pointing to src named dst.\n\n\ |
| 7066 | target_is_directory is required on Windows if the target is to be\n\ |
| 7067 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 7068 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 7069 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 7070 | \n\ |
| 7071 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7072 | and path should be relative; path will then be relative to that directory.\n\ |
| 7073 | dir_fd may not be implemented on your platform.\n\ |
| 7074 | If it is unavailable, using it will raise a NotImplementedError."); |
| 7075 | |
| 7076 | #if defined(MS_WINDOWS) |
| 7077 | |
| 7078 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7079 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7080 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
| 7081 | static int |
| 7082 | check_CreateSymbolicLink() |
| 7083 | { |
| 7084 | HINSTANCE hKernel32; |
| 7085 | /* only recheck */ |
| 7086 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7087 | return 1; |
| 7088 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7089 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7090 | "CreateSymbolicLinkW"); |
| 7091 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7092 | "CreateSymbolicLinkA"); |
| 7093 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7094 | } |
| 7095 | |
| 7096 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7097 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7098 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7099 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7100 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7101 | path_t src; |
| 7102 | path_t dst; |
| 7103 | int dir_fd = DEFAULT_DIR_FD; |
| 7104 | int target_is_directory = 0; |
| 7105 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 7106 | "dir_fd", NULL}; |
| 7107 | PyObject *return_value; |
| 7108 | #ifdef MS_WINDOWS |
| 7109 | DWORD result; |
| 7110 | #else |
| 7111 | int result; |
| 7112 | #endif |
| 7113 | |
| 7114 | memset(&src, 0, sizeof(src)); |
| 7115 | src.argument_name = "src"; |
| 7116 | memset(&dst, 0, sizeof(dst)); |
| 7117 | dst.argument_name = "dst"; |
| 7118 | |
| 7119 | #ifdef MS_WINDOWS |
| 7120 | if (!check_CreateSymbolicLink()) { |
| 7121 | PyErr_SetString(PyExc_NotImplementedError, |
| 7122 | "CreateSymbolicLink functions not found"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7123 | return NULL; |
| 7124 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7125 | if (!win32_can_symlink) { |
| 7126 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7127 | return NULL; |
| 7128 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7129 | #endif |
| 7130 | |
| 7131 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 7132 | keywords, |
| 7133 | path_converter, &src, |
| 7134 | path_converter, &dst, |
| 7135 | &target_is_directory, |
| 7136 | #ifdef HAVE_SYMLINKAT |
| 7137 | dir_fd_converter, &dir_fd |
| 7138 | #else |
| 7139 | dir_fd_unavailable, &dir_fd |
| 7140 | #endif |
| 7141 | )) |
| 7142 | return NULL; |
| 7143 | |
| 7144 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 7145 | PyErr_SetString(PyExc_ValueError, |
| 7146 | "symlink: src and dst must be the same type"); |
| 7147 | return_value = NULL; |
| 7148 | goto exit; |
| 7149 | } |
| 7150 | |
| 7151 | #ifdef MS_WINDOWS |
| 7152 | Py_BEGIN_ALLOW_THREADS |
| 7153 | if (dst.wide) |
| 7154 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 7155 | target_is_directory); |
| 7156 | else |
| 7157 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 7158 | target_is_directory); |
| 7159 | Py_END_ALLOW_THREADS |
| 7160 | |
| 7161 | if (!result) { |
| 7162 | return_value = win32_error_object("symlink", src.object); |
| 7163 | goto exit; |
| 7164 | } |
| 7165 | |
| 7166 | #else |
| 7167 | |
| 7168 | Py_BEGIN_ALLOW_THREADS |
| 7169 | #if HAVE_SYMLINKAT |
| 7170 | if (dir_fd != DEFAULT_DIR_FD) |
| 7171 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 7172 | else |
| 7173 | #endif |
| 7174 | result = symlink(src.narrow, dst.narrow); |
| 7175 | Py_END_ALLOW_THREADS |
| 7176 | |
| 7177 | if (result) { |
| 7178 | return_value = path_error("symlink", &dst); |
| 7179 | goto exit; |
| 7180 | } |
| 7181 | #endif |
| 7182 | |
| 7183 | return_value = Py_None; |
| 7184 | Py_INCREF(Py_None); |
| 7185 | goto exit; /* silence "unused label" warning */ |
| 7186 | exit: |
| 7187 | path_cleanup(&src); |
| 7188 | path_cleanup(&dst); |
| 7189 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7190 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7191 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7192 | #endif /* HAVE_SYMLINK */ |
| 7193 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7194 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7195 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7196 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7197 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7198 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7199 | { |
| 7200 | wchar_t *path; |
| 7201 | DWORD n_bytes_returned; |
| 7202 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7203 | PyObject *po, *result; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7204 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7205 | HANDLE reparse_point_handle; |
| 7206 | |
| 7207 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7208 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7209 | wchar_t *print_name; |
| 7210 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7211 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7212 | |
| 7213 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7214 | &po, |
| 7215 | dir_fd_unavailable, &dir_fd |
| 7216 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7217 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7218 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7219 | path = PyUnicode_AsUnicode(po); |
| 7220 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7221 | return NULL; |
| 7222 | |
| 7223 | /* First get a handle to the reparse point */ |
| 7224 | Py_BEGIN_ALLOW_THREADS |
| 7225 | reparse_point_handle = CreateFileW( |
| 7226 | path, |
| 7227 | 0, |
| 7228 | 0, |
| 7229 | 0, |
| 7230 | OPEN_EXISTING, |
| 7231 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7232 | 0); |
| 7233 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7234 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7235 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7236 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7237 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7238 | Py_BEGIN_ALLOW_THREADS |
| 7239 | /* New call DeviceIoControl to read the reparse point */ |
| 7240 | io_result = DeviceIoControl( |
| 7241 | reparse_point_handle, |
| 7242 | FSCTL_GET_REPARSE_POINT, |
| 7243 | 0, 0, /* in buffer */ |
| 7244 | target_buffer, sizeof(target_buffer), |
| 7245 | &n_bytes_returned, |
| 7246 | 0 /* we're not using OVERLAPPED_IO */ |
| 7247 | ); |
| 7248 | CloseHandle(reparse_point_handle); |
| 7249 | Py_END_ALLOW_THREADS |
| 7250 | |
| 7251 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7252 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7253 | |
| 7254 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7255 | { |
| 7256 | PyErr_SetString(PyExc_ValueError, |
| 7257 | "not a symbolic link"); |
| 7258 | return NULL; |
| 7259 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 7260 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7261 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7262 | |
| 7263 | result = PyUnicode_FromWideChar(print_name, |
| 7264 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7265 | return result; |
| 7266 | } |
| 7267 | |
| 7268 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7269 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7270 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7271 | static PyStructSequence_Field times_result_fields[] = { |
| 7272 | {"user", "user time"}, |
| 7273 | {"system", "system time"}, |
| 7274 | {"children_user", "user time of children"}, |
| 7275 | {"children_system", "system time of children"}, |
| 7276 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7277 | {NULL} |
| 7278 | }; |
| 7279 | |
| 7280 | PyDoc_STRVAR(times_result__doc__, |
| 7281 | "times_result: Result from os.times().\n\n\ |
| 7282 | This object may be accessed either as a tuple of\n\ |
| 7283 | (user, system, children_user, children_system, elapsed),\n\ |
| 7284 | or via the attributes user, system, children_user, children_system,\n\ |
| 7285 | and elapsed.\n\ |
| 7286 | \n\ |
| 7287 | See os.times for more information."); |
| 7288 | |
| 7289 | static PyStructSequence_Desc times_result_desc = { |
| 7290 | "times_result", /* name */ |
| 7291 | times_result__doc__, /* doc */ |
| 7292 | times_result_fields, |
| 7293 | 5 |
| 7294 | }; |
| 7295 | |
| 7296 | static PyTypeObject TimesResultType; |
| 7297 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7298 | #ifdef MS_WINDOWS |
| 7299 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7300 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7301 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7302 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7303 | |
| 7304 | static PyObject * |
| 7305 | build_times_result(double user, double system, |
| 7306 | double children_user, double children_system, |
| 7307 | double elapsed) |
| 7308 | { |
| 7309 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7310 | if (value == NULL) |
| 7311 | return NULL; |
| 7312 | |
| 7313 | #define SET(i, field) \ |
| 7314 | { \ |
| 7315 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7316 | if (!o) { \ |
| 7317 | Py_DECREF(value); \ |
| 7318 | return NULL; \ |
| 7319 | } \ |
| 7320 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7321 | } \ |
| 7322 | |
| 7323 | SET(0, user); |
| 7324 | SET(1, system); |
| 7325 | SET(2, children_user); |
| 7326 | SET(3, children_system); |
| 7327 | SET(4, elapsed); |
| 7328 | |
| 7329 | #undef SET |
| 7330 | |
| 7331 | return value; |
| 7332 | } |
| 7333 | |
| 7334 | PyDoc_STRVAR(posix_times__doc__, |
| 7335 | "times() -> times_result\n\n\ |
| 7336 | Return an object containing floating point numbers indicating process\n\ |
| 7337 | times. The object behaves like a named tuple with these fields:\n\ |
| 7338 | (utime, stime, cutime, cstime, elapsed_time)"); |
| 7339 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7340 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 7341 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7342 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7343 | { |
| 7344 | ULONG value = 0; |
| 7345 | |
| 7346 | Py_BEGIN_ALLOW_THREADS |
| 7347 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 7348 | Py_END_ALLOW_THREADS |
| 7349 | |
| 7350 | return value; |
| 7351 | } |
| 7352 | |
| 7353 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7354 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7355 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7356 | /* Currently Only Uptime is Provided -- Others Later */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7357 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7358 | (double)0 /* t.tms_utime / HZ */, |
| 7359 | (double)0 /* t.tms_stime / HZ */, |
| 7360 | (double)0 /* t.tms_cutime / HZ */, |
| 7361 | (double)0 /* t.tms_cstime / HZ */, |
| 7362 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7363 | } |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7364 | #elif defined(MS_WINDOWS) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7365 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7366 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7367 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7368 | FILETIME create, exit, kernel, user; |
| 7369 | HANDLE hProc; |
| 7370 | hProc = GetCurrentProcess(); |
| 7371 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7372 | /* The fields of a FILETIME structure are the hi and lo part |
| 7373 | of a 64-bit value expressed in 100 nanosecond units. |
| 7374 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7375 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7376 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7377 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7378 | (double)(user.dwHighDateTime*429.4967296 + |
| 7379 | user.dwLowDateTime*1e-7), |
| 7380 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7381 | kernel.dwLowDateTime*1e-7), |
| 7382 | (double)0, |
| 7383 | (double)0, |
| 7384 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7385 | } |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7386 | #else /* Neither Windows nor OS/2 */ |
| 7387 | #define NEED_TICKS_PER_SECOND |
| 7388 | static long ticks_per_second = -1; |
| 7389 | static PyObject * |
| 7390 | posix_times(PyObject *self, PyObject *noargs) |
| 7391 | { |
| 7392 | struct tms t; |
| 7393 | clock_t c; |
| 7394 | errno = 0; |
| 7395 | c = times(&t); |
| 7396 | if (c == (clock_t) -1) |
| 7397 | return posix_error(); |
| 7398 | return build_times_result( |
| 7399 | (double)t.tms_utime / ticks_per_second, |
| 7400 | (double)t.tms_stime / ticks_per_second, |
| 7401 | (double)t.tms_cutime / ticks_per_second, |
| 7402 | (double)t.tms_cstime / ticks_per_second, |
| 7403 | (double)c / ticks_per_second); |
| 7404 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7405 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7406 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7407 | #endif /* HAVE_TIMES */ |
| 7408 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7409 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7410 | #ifdef HAVE_GETSID |
| 7411 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7412 | "getsid(pid) -> sid\n\n\ |
| 7413 | Call the system call getsid()."); |
| 7414 | |
| 7415 | static PyObject * |
| 7416 | posix_getsid(PyObject *self, PyObject *args) |
| 7417 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7418 | pid_t pid; |
| 7419 | int sid; |
| 7420 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7421 | return NULL; |
| 7422 | sid = getsid(pid); |
| 7423 | if (sid < 0) |
| 7424 | return posix_error(); |
| 7425 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7426 | } |
| 7427 | #endif /* HAVE_GETSID */ |
| 7428 | |
| 7429 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7430 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7431 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7432 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7433 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7434 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7435 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7436 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7437 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7438 | if (setsid() < 0) |
| 7439 | return posix_error(); |
| 7440 | Py_INCREF(Py_None); |
| 7441 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7442 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7443 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7444 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7445 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7446 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7447 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7448 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7449 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7450 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7451 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7452 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7453 | pid_t pid; |
| 7454 | int pgrp; |
| 7455 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7456 | return NULL; |
| 7457 | if (setpgid(pid, pgrp) < 0) |
| 7458 | return posix_error(); |
| 7459 | Py_INCREF(Py_None); |
| 7460 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7461 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7462 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7463 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7464 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7465 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7466 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7467 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7468 | 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] | 7469 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7470 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7471 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7473 | int fd; |
| 7474 | pid_t pgid; |
| 7475 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7476 | return NULL; |
| 7477 | pgid = tcgetpgrp(fd); |
| 7478 | if (pgid < 0) |
| 7479 | return posix_error(); |
| 7480 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7481 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7482 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7483 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7484 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7485 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7486 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7487 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7488 | 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] | 7489 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7490 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7491 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7492 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7493 | int fd; |
| 7494 | pid_t pgid; |
| 7495 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7496 | return NULL; |
| 7497 | if (tcsetpgrp(fd, pgid) < 0) |
| 7498 | return posix_error(); |
| 7499 | Py_INCREF(Py_None); |
| 7500 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7501 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7502 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7503 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7504 | /* Functions acting on file descriptors */ |
| 7505 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7506 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7507 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7508 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7509 | \n\ |
| 7510 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7511 | and path should be relative; path will then be relative to that directory.\n\ |
| 7512 | dir_fd may not be implemented on your platform.\n\ |
| 7513 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7514 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7515 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7516 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7517 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7518 | path_t path; |
| 7519 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7520 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7521 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7522 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7523 | PyObject *return_value = NULL; |
| 7524 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7525 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7526 | memset(&path, 0, sizeof(path)); |
| 7527 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7528 | path_converter, &path, |
| 7529 | &flags, &mode, |
| 7530 | #ifdef HAVE_OPENAT |
| 7531 | dir_fd_converter, &dir_fd |
| 7532 | #else |
| 7533 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7534 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7535 | )) |
| 7536 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7537 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7538 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7539 | #ifdef MS_WINDOWS |
| 7540 | if (path.wide) |
| 7541 | fd = _wopen(path.wide, flags, mode); |
| 7542 | else |
| 7543 | #endif |
| 7544 | #ifdef HAVE_OPENAT |
| 7545 | if (dir_fd != DEFAULT_DIR_FD) |
| 7546 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7547 | else |
| 7548 | #endif |
| 7549 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7550 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7551 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7552 | if (fd == -1) { |
| 7553 | #ifdef MS_WINDOWS |
| 7554 | /* force use of posix_error here for exact backwards compatibility */ |
| 7555 | if (path.wide) |
| 7556 | return_value = posix_error(); |
| 7557 | else |
| 7558 | #endif |
| 7559 | return_value = path_error("open", &path); |
| 7560 | goto exit; |
| 7561 | } |
| 7562 | |
| 7563 | return_value = PyLong_FromLong((long)fd); |
| 7564 | |
| 7565 | exit: |
| 7566 | path_cleanup(&path); |
| 7567 | return return_value; |
| 7568 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7569 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7570 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7571 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7572 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7573 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7574 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7575 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7576 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7577 | int fd, res; |
| 7578 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7579 | return NULL; |
| 7580 | if (!_PyVerify_fd(fd)) |
| 7581 | return posix_error(); |
| 7582 | Py_BEGIN_ALLOW_THREADS |
| 7583 | res = close(fd); |
| 7584 | Py_END_ALLOW_THREADS |
| 7585 | if (res < 0) |
| 7586 | return posix_error(); |
| 7587 | Py_INCREF(Py_None); |
| 7588 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7589 | } |
| 7590 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7591 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7592 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7593 | "closerange(fd_low, fd_high)\n\n\ |
| 7594 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7595 | |
| 7596 | static PyObject * |
| 7597 | posix_closerange(PyObject *self, PyObject *args) |
| 7598 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7599 | int fd_from, fd_to, i; |
| 7600 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7601 | return NULL; |
| 7602 | Py_BEGIN_ALLOW_THREADS |
| 7603 | for (i = fd_from; i < fd_to; i++) |
| 7604 | if (_PyVerify_fd(i)) |
| 7605 | close(i); |
| 7606 | Py_END_ALLOW_THREADS |
| 7607 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7608 | } |
| 7609 | |
| 7610 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7611 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7612 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7613 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7614 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7615 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7616 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7617 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7618 | int fd; |
| 7619 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7620 | return NULL; |
| 7621 | if (!_PyVerify_fd(fd)) |
| 7622 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7623 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7624 | if (fd < 0) |
| 7625 | return posix_error(); |
| 7626 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7627 | } |
| 7628 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7629 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7630 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7631 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7632 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7633 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7634 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7635 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7636 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7637 | int fd, fd2, res; |
| 7638 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 7639 | return NULL; |
| 7640 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7641 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7642 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7643 | if (res < 0) |
| 7644 | return posix_error(); |
| 7645 | Py_INCREF(Py_None); |
| 7646 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7647 | } |
| 7648 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7649 | #ifdef HAVE_LOCKF |
| 7650 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7651 | "lockf(fd, cmd, len)\n\n\ |
| 7652 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7653 | fd is an open file descriptor.\n\ |
| 7654 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7655 | F_TEST.\n\ |
| 7656 | len specifies the section of the file to lock."); |
| 7657 | |
| 7658 | static PyObject * |
| 7659 | posix_lockf(PyObject *self, PyObject *args) |
| 7660 | { |
| 7661 | int fd, cmd, res; |
| 7662 | off_t len; |
| 7663 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7664 | &fd, &cmd, _parse_off_t, &len)) |
| 7665 | return NULL; |
| 7666 | |
| 7667 | Py_BEGIN_ALLOW_THREADS |
| 7668 | res = lockf(fd, cmd, len); |
| 7669 | Py_END_ALLOW_THREADS |
| 7670 | |
| 7671 | if (res < 0) |
| 7672 | return posix_error(); |
| 7673 | |
| 7674 | Py_RETURN_NONE; |
| 7675 | } |
| 7676 | #endif |
| 7677 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7679 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7680 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7681 | Set the current position of a file descriptor.\n\ |
| 7682 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7683 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7684 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7685 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7686 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7687 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7688 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7689 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7690 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7691 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7692 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7693 | PyObject *posobj; |
| 7694 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7695 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7696 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7697 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7698 | switch (how) { |
| 7699 | case 0: how = SEEK_SET; break; |
| 7700 | case 1: how = SEEK_CUR; break; |
| 7701 | case 2: how = SEEK_END; break; |
| 7702 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7703 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7704 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7705 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7706 | pos = PyLong_AsLong(posobj); |
| 7707 | #else |
| 7708 | pos = PyLong_AsLongLong(posobj); |
| 7709 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7710 | if (PyErr_Occurred()) |
| 7711 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7712 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7713 | if (!_PyVerify_fd(fd)) |
| 7714 | return posix_error(); |
| 7715 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7716 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7717 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7718 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7719 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7720 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7721 | Py_END_ALLOW_THREADS |
| 7722 | if (res < 0) |
| 7723 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7724 | |
| 7725 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7726 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7727 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7728 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7729 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7730 | } |
| 7731 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7732 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7733 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7734 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7735 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7736 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7737 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7738 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7739 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7740 | int fd, size; |
| 7741 | Py_ssize_t n; |
| 7742 | PyObject *buffer; |
| 7743 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7744 | return NULL; |
| 7745 | if (size < 0) { |
| 7746 | errno = EINVAL; |
| 7747 | return posix_error(); |
| 7748 | } |
| 7749 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7750 | if (buffer == NULL) |
| 7751 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7752 | if (!_PyVerify_fd(fd)) { |
| 7753 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7754 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7755 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7756 | Py_BEGIN_ALLOW_THREADS |
| 7757 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7758 | Py_END_ALLOW_THREADS |
| 7759 | if (n < 0) { |
| 7760 | Py_DECREF(buffer); |
| 7761 | return posix_error(); |
| 7762 | } |
| 7763 | if (n != size) |
| 7764 | _PyBytes_Resize(&buffer, n); |
| 7765 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7766 | } |
| 7767 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7768 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7769 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7770 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7771 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7772 | { |
| 7773 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7774 | Py_ssize_t blen, total = 0; |
| 7775 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7776 | *iov = PyMem_New(struct iovec, cnt); |
| 7777 | if (*iov == NULL) { |
| 7778 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7779 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7780 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7781 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7782 | *buf = PyMem_New(Py_buffer, cnt); |
| 7783 | if (*buf == NULL) { |
| 7784 | PyMem_Del(*iov); |
| 7785 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7786 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7787 | } |
| 7788 | |
| 7789 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7790 | PyObject *item = PySequence_GetItem(seq, i); |
| 7791 | if (item == NULL) |
| 7792 | goto fail; |
| 7793 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7794 | Py_DECREF(item); |
| 7795 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7796 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7797 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7798 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7799 | blen = (*buf)[i].len; |
| 7800 | (*iov)[i].iov_len = blen; |
| 7801 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7802 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7803 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7804 | |
| 7805 | fail: |
| 7806 | PyMem_Del(*iov); |
| 7807 | for (j = 0; j < i; j++) { |
| 7808 | PyBuffer_Release(&(*buf)[j]); |
| 7809 | } |
| 7810 | PyMem_Del(*buf); |
| 7811 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7812 | } |
| 7813 | |
| 7814 | static void |
| 7815 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7816 | { |
| 7817 | int i; |
| 7818 | PyMem_Del(iov); |
| 7819 | for (i = 0; i < cnt; i++) { |
| 7820 | PyBuffer_Release(&buf[i]); |
| 7821 | } |
| 7822 | PyMem_Del(buf); |
| 7823 | } |
| 7824 | #endif |
| 7825 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7826 | #ifdef HAVE_READV |
| 7827 | PyDoc_STRVAR(posix_readv__doc__, |
| 7828 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7829 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7830 | is an arbitrary sequence of writable buffers.\n\ |
| 7831 | Returns the total number of bytes read."); |
| 7832 | |
| 7833 | static PyObject * |
| 7834 | posix_readv(PyObject *self, PyObject *args) |
| 7835 | { |
| 7836 | int fd, cnt; |
| 7837 | Py_ssize_t n; |
| 7838 | PyObject *seq; |
| 7839 | struct iovec *iov; |
| 7840 | Py_buffer *buf; |
| 7841 | |
| 7842 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7843 | return NULL; |
| 7844 | if (!PySequence_Check(seq)) { |
| 7845 | PyErr_SetString(PyExc_TypeError, |
| 7846 | "readv() arg 2 must be a sequence"); |
| 7847 | return NULL; |
| 7848 | } |
| 7849 | cnt = PySequence_Size(seq); |
| 7850 | |
| 7851 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7852 | return NULL; |
| 7853 | |
| 7854 | Py_BEGIN_ALLOW_THREADS |
| 7855 | n = readv(fd, iov, cnt); |
| 7856 | Py_END_ALLOW_THREADS |
| 7857 | |
| 7858 | iov_cleanup(iov, buf, cnt); |
| 7859 | return PyLong_FromSsize_t(n); |
| 7860 | } |
| 7861 | #endif |
| 7862 | |
| 7863 | #ifdef HAVE_PREAD |
| 7864 | PyDoc_STRVAR(posix_pread__doc__, |
| 7865 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7866 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7867 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7868 | |
| 7869 | static PyObject * |
| 7870 | posix_pread(PyObject *self, PyObject *args) |
| 7871 | { |
| 7872 | int fd, size; |
| 7873 | off_t offset; |
| 7874 | Py_ssize_t n; |
| 7875 | PyObject *buffer; |
| 7876 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7877 | return NULL; |
| 7878 | |
| 7879 | if (size < 0) { |
| 7880 | errno = EINVAL; |
| 7881 | return posix_error(); |
| 7882 | } |
| 7883 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7884 | if (buffer == NULL) |
| 7885 | return NULL; |
| 7886 | if (!_PyVerify_fd(fd)) { |
| 7887 | Py_DECREF(buffer); |
| 7888 | return posix_error(); |
| 7889 | } |
| 7890 | Py_BEGIN_ALLOW_THREADS |
| 7891 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7892 | Py_END_ALLOW_THREADS |
| 7893 | if (n < 0) { |
| 7894 | Py_DECREF(buffer); |
| 7895 | return posix_error(); |
| 7896 | } |
| 7897 | if (n != size) |
| 7898 | _PyBytes_Resize(&buffer, n); |
| 7899 | return buffer; |
| 7900 | } |
| 7901 | #endif |
| 7902 | |
| 7903 | PyDoc_STRVAR(posix_write__doc__, |
| 7904 | "write(fd, string) -> byteswritten\n\n\ |
| 7905 | Write a string to a file descriptor."); |
| 7906 | |
| 7907 | static PyObject * |
| 7908 | posix_write(PyObject *self, PyObject *args) |
| 7909 | { |
| 7910 | Py_buffer pbuf; |
| 7911 | int fd; |
| 7912 | Py_ssize_t size, len; |
| 7913 | |
| 7914 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7915 | return NULL; |
| 7916 | if (!_PyVerify_fd(fd)) { |
| 7917 | PyBuffer_Release(&pbuf); |
| 7918 | return posix_error(); |
| 7919 | } |
| 7920 | len = pbuf.len; |
| 7921 | Py_BEGIN_ALLOW_THREADS |
| 7922 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7923 | if (len > INT_MAX) |
| 7924 | len = INT_MAX; |
| 7925 | size = write(fd, pbuf.buf, (int)len); |
| 7926 | #else |
| 7927 | size = write(fd, pbuf.buf, len); |
| 7928 | #endif |
| 7929 | Py_END_ALLOW_THREADS |
| 7930 | PyBuffer_Release(&pbuf); |
| 7931 | if (size < 0) |
| 7932 | return posix_error(); |
| 7933 | return PyLong_FromSsize_t(size); |
| 7934 | } |
| 7935 | |
| 7936 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7937 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7938 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7939 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7940 | -> byteswritten\n\ |
| 7941 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7942 | |
| 7943 | static PyObject * |
| 7944 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7945 | { |
| 7946 | int in, out; |
| 7947 | Py_ssize_t ret; |
| 7948 | off_t offset; |
| 7949 | |
| 7950 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7951 | #ifndef __APPLE__ |
| 7952 | Py_ssize_t len; |
| 7953 | #endif |
| 7954 | PyObject *headers = NULL, *trailers = NULL; |
| 7955 | Py_buffer *hbuf, *tbuf; |
| 7956 | off_t sbytes; |
| 7957 | struct sf_hdtr sf; |
| 7958 | int flags = 0; |
| 7959 | sf.headers = NULL; |
| 7960 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7961 | static char *keywords[] = {"out", "in", |
| 7962 | "offset", "count", |
| 7963 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7964 | |
| 7965 | #ifdef __APPLE__ |
| 7966 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7967 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7968 | #else |
| 7969 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7970 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7971 | #endif |
| 7972 | &headers, &trailers, &flags)) |
| 7973 | return NULL; |
| 7974 | if (headers != NULL) { |
| 7975 | if (!PySequence_Check(headers)) { |
| 7976 | PyErr_SetString(PyExc_TypeError, |
| 7977 | "sendfile() headers must be a sequence or None"); |
| 7978 | return NULL; |
| 7979 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7980 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7981 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7982 | if (sf.hdr_cnt > 0 && |
| 7983 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7984 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7985 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7986 | #ifdef __APPLE__ |
| 7987 | sbytes += i; |
| 7988 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7989 | } |
| 7990 | } |
| 7991 | if (trailers != NULL) { |
| 7992 | if (!PySequence_Check(trailers)) { |
| 7993 | PyErr_SetString(PyExc_TypeError, |
| 7994 | "sendfile() trailers must be a sequence or None"); |
| 7995 | return NULL; |
| 7996 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7997 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7998 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7999 | if (sf.trl_cnt > 0 && |
| 8000 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 8001 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8002 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8003 | #ifdef __APPLE__ |
| 8004 | sbytes += i; |
| 8005 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8006 | } |
| 8007 | } |
| 8008 | |
| 8009 | Py_BEGIN_ALLOW_THREADS |
| 8010 | #ifdef __APPLE__ |
| 8011 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 8012 | #else |
| 8013 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 8014 | #endif |
| 8015 | Py_END_ALLOW_THREADS |
| 8016 | |
| 8017 | if (sf.headers != NULL) |
| 8018 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8019 | if (sf.trailers != NULL) |
| 8020 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8021 | |
| 8022 | if (ret < 0) { |
| 8023 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8024 | if (sbytes != 0) { |
| 8025 | // some data has been sent |
| 8026 | goto done; |
| 8027 | } |
| 8028 | else { |
| 8029 | // no data has been sent; upper application is supposed |
| 8030 | // to retry on EAGAIN or EBUSY |
| 8031 | return posix_error(); |
| 8032 | } |
| 8033 | } |
| 8034 | return posix_error(); |
| 8035 | } |
| 8036 | goto done; |
| 8037 | |
| 8038 | done: |
| 8039 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8040 | return Py_BuildValue("l", sbytes); |
| 8041 | #else |
| 8042 | return Py_BuildValue("L", sbytes); |
| 8043 | #endif |
| 8044 | |
| 8045 | #else |
| 8046 | Py_ssize_t count; |
| 8047 | PyObject *offobj; |
| 8048 | static char *keywords[] = {"out", "in", |
| 8049 | "offset", "count", NULL}; |
| 8050 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8051 | keywords, &out, &in, &offobj, &count)) |
| 8052 | return NULL; |
| 8053 | #ifdef linux |
| 8054 | if (offobj == Py_None) { |
| 8055 | Py_BEGIN_ALLOW_THREADS |
| 8056 | ret = sendfile(out, in, NULL, count); |
| 8057 | Py_END_ALLOW_THREADS |
| 8058 | if (ret < 0) |
| 8059 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8060 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8061 | } |
| 8062 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8063 | if (!_parse_off_t(offobj, &offset)) |
| 8064 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8065 | Py_BEGIN_ALLOW_THREADS |
| 8066 | ret = sendfile(out, in, &offset, count); |
| 8067 | Py_END_ALLOW_THREADS |
| 8068 | if (ret < 0) |
| 8069 | return posix_error(); |
| 8070 | return Py_BuildValue("n", ret); |
| 8071 | #endif |
| 8072 | } |
| 8073 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8075 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8076 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8077 | Like stat(), but for an open file descriptor.\n\ |
| 8078 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8079 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8080 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8081 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8083 | int fd; |
| 8084 | STRUCT_STAT st; |
| 8085 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8086 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8087 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 8088 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8089 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 8090 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 8091 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8092 | Py_BEGIN_ALLOW_THREADS |
| 8093 | res = FSTAT(fd, &st); |
| 8094 | Py_END_ALLOW_THREADS |
| 8095 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8096 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8097 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8098 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8099 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8100 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8101 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8102 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8103 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8104 | } |
| 8105 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8106 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8107 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8108 | 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] | 8109 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8110 | |
| 8111 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 8112 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8113 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8114 | int fd; |
| 8115 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 8116 | return NULL; |
| 8117 | if (!_PyVerify_fd(fd)) |
| 8118 | return PyBool_FromLong(0); |
| 8119 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8120 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8121 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8122 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8123 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8124 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8125 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8127 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8128 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8129 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8130 | #if defined(PYOS_OS2) |
| 8131 | HFILE read, write; |
| 8132 | APIRET rc; |
| 8133 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8134 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8135 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8136 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8137 | |
| 8138 | return Py_BuildValue("(ii)", read, write); |
| 8139 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8140 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8141 | int fds[2]; |
| 8142 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8143 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8144 | if (res != 0) |
| 8145 | return posix_error(); |
| 8146 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8147 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8148 | HANDLE read, write; |
| 8149 | int read_fd, write_fd; |
| 8150 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8151 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8152 | if (!ok) |
| 8153 | return win32_error("CreatePipe", NULL); |
| 8154 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 8155 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 8156 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8157 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8158 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8159 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8160 | #endif /* HAVE_PIPE */ |
| 8161 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8162 | #ifdef HAVE_PIPE2 |
| 8163 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8164 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 8165 | Create a pipe with flags set atomically.\n\ |
| 8166 | flags can be constructed by ORing together one or more of these values:\n\ |
| 8167 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8168 | "); |
| 8169 | |
| 8170 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8171 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8172 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8173 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8174 | int fds[2]; |
| 8175 | int res; |
| 8176 | |
Serhiy Storchaka | 9101e23 | 2013-01-19 12:41:45 +0200 | [diff] [blame] | 8177 | flags = _PyLong_AsInt(arg); |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8178 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8179 | return NULL; |
| 8180 | |
| 8181 | res = pipe2(fds, flags); |
| 8182 | if (res != 0) |
| 8183 | return posix_error(); |
| 8184 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8185 | } |
| 8186 | #endif /* HAVE_PIPE2 */ |
| 8187 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8188 | #ifdef HAVE_WRITEV |
| 8189 | PyDoc_STRVAR(posix_writev__doc__, |
| 8190 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 8191 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 8192 | arbitrary sequence of buffers.\n\ |
| 8193 | Returns the total bytes written."); |
| 8194 | |
| 8195 | static PyObject * |
| 8196 | posix_writev(PyObject *self, PyObject *args) |
| 8197 | { |
| 8198 | int fd, cnt; |
| 8199 | Py_ssize_t res; |
| 8200 | PyObject *seq; |
| 8201 | struct iovec *iov; |
| 8202 | Py_buffer *buf; |
| 8203 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 8204 | return NULL; |
| 8205 | if (!PySequence_Check(seq)) { |
| 8206 | PyErr_SetString(PyExc_TypeError, |
| 8207 | "writev() arg 2 must be a sequence"); |
| 8208 | return NULL; |
| 8209 | } |
| 8210 | cnt = PySequence_Size(seq); |
| 8211 | |
| 8212 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 8213 | return NULL; |
| 8214 | } |
| 8215 | |
| 8216 | Py_BEGIN_ALLOW_THREADS |
| 8217 | res = writev(fd, iov, cnt); |
| 8218 | Py_END_ALLOW_THREADS |
| 8219 | |
| 8220 | iov_cleanup(iov, buf, cnt); |
| 8221 | return PyLong_FromSsize_t(res); |
| 8222 | } |
| 8223 | #endif |
| 8224 | |
| 8225 | #ifdef HAVE_PWRITE |
| 8226 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 8227 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 8228 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 8229 | offset unchanged."); |
| 8230 | |
| 8231 | static PyObject * |
| 8232 | posix_pwrite(PyObject *self, PyObject *args) |
| 8233 | { |
| 8234 | Py_buffer pbuf; |
| 8235 | int fd; |
| 8236 | off_t offset; |
| 8237 | Py_ssize_t size; |
| 8238 | |
| 8239 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 8240 | return NULL; |
| 8241 | |
| 8242 | if (!_PyVerify_fd(fd)) { |
| 8243 | PyBuffer_Release(&pbuf); |
| 8244 | return posix_error(); |
| 8245 | } |
| 8246 | Py_BEGIN_ALLOW_THREADS |
| 8247 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 8248 | Py_END_ALLOW_THREADS |
| 8249 | PyBuffer_Release(&pbuf); |
| 8250 | if (size < 0) |
| 8251 | return posix_error(); |
| 8252 | return PyLong_FromSsize_t(size); |
| 8253 | } |
| 8254 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8255 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8256 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8257 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8258 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 8259 | Create a FIFO (a POSIX named pipe).\n\ |
| 8260 | \n\ |
| 8261 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8262 | and path should be relative; path will then be relative to that directory.\n\ |
| 8263 | dir_fd may not be implemented on your platform.\n\ |
| 8264 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8265 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8266 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8267 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8268 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8269 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8270 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8271 | int dir_fd = DEFAULT_DIR_FD; |
| 8272 | int result; |
| 8273 | PyObject *return_value = NULL; |
| 8274 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 8275 | |
| 8276 | memset(&path, 0, sizeof(path)); |
| 8277 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 8278 | path_converter, &path, |
| 8279 | &mode, |
| 8280 | #ifdef HAVE_MKFIFOAT |
| 8281 | dir_fd_converter, &dir_fd |
| 8282 | #else |
| 8283 | dir_fd_unavailable, &dir_fd |
| 8284 | #endif |
| 8285 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8286 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8287 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8288 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8289 | #ifdef HAVE_MKFIFOAT |
| 8290 | if (dir_fd != DEFAULT_DIR_FD) |
| 8291 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 8292 | else |
| 8293 | #endif |
| 8294 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8295 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8296 | |
| 8297 | if (result < 0) { |
| 8298 | return_value = posix_error(); |
| 8299 | goto exit; |
| 8300 | } |
| 8301 | |
| 8302 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8303 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8304 | |
| 8305 | exit: |
| 8306 | path_cleanup(&path); |
| 8307 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8308 | } |
| 8309 | #endif |
| 8310 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8311 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8312 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8313 | "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] | 8314 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 8315 | named filename. mode specifies both the permissions to use and the\n\ |
| 8316 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 8317 | 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] | 8318 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8319 | os.makedev()), otherwise it is ignored.\n\ |
| 8320 | \n\ |
| 8321 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8322 | and path should be relative; path will then be relative to that directory.\n\ |
| 8323 | dir_fd may not be implemented on your platform.\n\ |
| 8324 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8325 | |
| 8326 | |
| 8327 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8328 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8329 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8330 | path_t path; |
| 8331 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8332 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8333 | int dir_fd = DEFAULT_DIR_FD; |
| 8334 | int result; |
| 8335 | PyObject *return_value = NULL; |
| 8336 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 8337 | |
| 8338 | memset(&path, 0, sizeof(path)); |
| 8339 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 8340 | path_converter, &path, |
| 8341 | &mode, &device, |
| 8342 | #ifdef HAVE_MKNODAT |
| 8343 | dir_fd_converter, &dir_fd |
| 8344 | #else |
| 8345 | dir_fd_unavailable, &dir_fd |
| 8346 | #endif |
| 8347 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8348 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8349 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8350 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8351 | #ifdef HAVE_MKNODAT |
| 8352 | if (dir_fd != DEFAULT_DIR_FD) |
| 8353 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 8354 | else |
| 8355 | #endif |
| 8356 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8357 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8358 | |
| 8359 | if (result < 0) { |
| 8360 | return_value = posix_error(); |
| 8361 | goto exit; |
| 8362 | } |
| 8363 | |
| 8364 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8365 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 8366 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8367 | exit: |
| 8368 | path_cleanup(&path); |
| 8369 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8370 | } |
| 8371 | #endif |
| 8372 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8373 | #ifdef HAVE_DEVICE_MACROS |
| 8374 | PyDoc_STRVAR(posix_major__doc__, |
| 8375 | "major(device) -> major number\n\ |
| 8376 | Extracts a device major number from a raw device number."); |
| 8377 | |
| 8378 | static PyObject * |
| 8379 | posix_major(PyObject *self, PyObject *args) |
| 8380 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8381 | int device; |
| 8382 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 8383 | return NULL; |
| 8384 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8385 | } |
| 8386 | |
| 8387 | PyDoc_STRVAR(posix_minor__doc__, |
| 8388 | "minor(device) -> minor number\n\ |
| 8389 | Extracts a device minor number from a raw device number."); |
| 8390 | |
| 8391 | static PyObject * |
| 8392 | posix_minor(PyObject *self, PyObject *args) |
| 8393 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8394 | int device; |
| 8395 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 8396 | return NULL; |
| 8397 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8398 | } |
| 8399 | |
| 8400 | PyDoc_STRVAR(posix_makedev__doc__, |
| 8401 | "makedev(major, minor) -> device number\n\ |
| 8402 | Composes a raw device number from the major and minor device numbers."); |
| 8403 | |
| 8404 | static PyObject * |
| 8405 | posix_makedev(PyObject *self, PyObject *args) |
| 8406 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8407 | int major, minor; |
| 8408 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 8409 | return NULL; |
| 8410 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8411 | } |
| 8412 | #endif /* device macros */ |
| 8413 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8414 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8415 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8416 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8417 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8418 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8419 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8420 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8421 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8422 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8423 | int fd; |
| 8424 | off_t length; |
| 8425 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8426 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8427 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8428 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8429 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8430 | Py_BEGIN_ALLOW_THREADS |
| 8431 | res = ftruncate(fd, length); |
| 8432 | Py_END_ALLOW_THREADS |
| 8433 | if (res < 0) |
| 8434 | return posix_error(); |
| 8435 | Py_INCREF(Py_None); |
| 8436 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8437 | } |
| 8438 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8439 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8440 | #ifdef HAVE_TRUNCATE |
| 8441 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8442 | "truncate(path, length)\n\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8443 | Truncate the file given by path to length bytes.\n\ |
| 8444 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8445 | If this functionality is unavailable, using it raises an exception."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8446 | |
| 8447 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8448 | posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8449 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8450 | path_t path; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8451 | off_t length; |
| 8452 | int res; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8453 | PyObject *result = NULL; |
| 8454 | static char *keywords[] = {"path", "length", NULL}; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8455 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8456 | memset(&path, 0, sizeof(path)); |
| 8457 | #ifdef HAVE_FTRUNCATE |
| 8458 | path.allow_fd = 1; |
| 8459 | #endif |
| 8460 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords, |
| 8461 | path_converter, &path, |
| 8462 | _parse_off_t, &length)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8463 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8464 | |
| 8465 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8466 | #ifdef HAVE_FTRUNCATE |
| 8467 | if (path.fd != -1) |
| 8468 | res = ftruncate(path.fd, length); |
| 8469 | else |
| 8470 | #endif |
| 8471 | res = truncate(path.narrow, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8472 | Py_END_ALLOW_THREADS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8473 | if (res < 0) |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8474 | result = path_posix_error("truncate", &path); |
| 8475 | else { |
| 8476 | Py_INCREF(Py_None); |
| 8477 | result = Py_None; |
| 8478 | } |
| 8479 | path_cleanup(&path); |
| 8480 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8481 | } |
| 8482 | #endif |
| 8483 | |
| 8484 | #ifdef HAVE_POSIX_FALLOCATE |
| 8485 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8486 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8487 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8488 | starting from offset and continuing for len bytes."); |
| 8489 | |
| 8490 | static PyObject * |
| 8491 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8492 | { |
| 8493 | off_t len, offset; |
| 8494 | int res, fd; |
| 8495 | |
| 8496 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8497 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8498 | return NULL; |
| 8499 | |
| 8500 | Py_BEGIN_ALLOW_THREADS |
| 8501 | res = posix_fallocate(fd, offset, len); |
| 8502 | Py_END_ALLOW_THREADS |
| 8503 | if (res != 0) { |
| 8504 | errno = res; |
| 8505 | return posix_error(); |
| 8506 | } |
| 8507 | Py_RETURN_NONE; |
| 8508 | } |
| 8509 | #endif |
| 8510 | |
| 8511 | #ifdef HAVE_POSIX_FADVISE |
| 8512 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8513 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8514 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8515 | the kernel to make optimizations.\n\ |
| 8516 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8517 | offset and continuing for len bytes.\n\ |
| 8518 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8519 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8520 | POSIX_FADV_DONTNEED."); |
| 8521 | |
| 8522 | static PyObject * |
| 8523 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8524 | { |
| 8525 | off_t len, offset; |
| 8526 | int res, fd, advice; |
| 8527 | |
| 8528 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8529 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8530 | return NULL; |
| 8531 | |
| 8532 | Py_BEGIN_ALLOW_THREADS |
| 8533 | res = posix_fadvise(fd, offset, len, advice); |
| 8534 | Py_END_ALLOW_THREADS |
| 8535 | if (res != 0) { |
| 8536 | errno = res; |
| 8537 | return posix_error(); |
| 8538 | } |
| 8539 | Py_RETURN_NONE; |
| 8540 | } |
| 8541 | #endif |
| 8542 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8543 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8544 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8545 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8546 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8547 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8548 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8549 | * get re-set with another call for the same key. */ |
| 8550 | static PyObject *posix_putenv_garbage; |
| 8551 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8552 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8553 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8554 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8555 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8556 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8557 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8558 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8559 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8560 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8561 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8562 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8563 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8564 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8565 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8566 | if (newstr == NULL) { |
| 8567 | PyErr_NoMemory(); |
| 8568 | goto error; |
| 8569 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8570 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8571 | PyErr_Format(PyExc_ValueError, |
| 8572 | "the environment variable is longer than %u characters", |
| 8573 | _MAX_ENV); |
| 8574 | goto error; |
| 8575 | } |
| 8576 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8577 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8578 | if (newenv == NULL) |
| 8579 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8580 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8581 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8582 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8583 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8584 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8585 | PyObject *os1, *os2; |
| 8586 | char *s1, *s2; |
| 8587 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8588 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8589 | if (!PyArg_ParseTuple(args, |
| 8590 | "O&O&:putenv", |
| 8591 | PyUnicode_FSConverter, &os1, |
| 8592 | PyUnicode_FSConverter, &os2)) |
| 8593 | return NULL; |
| 8594 | s1 = PyBytes_AsString(os1); |
| 8595 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8596 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8597 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8598 | if (newstr == NULL) { |
| 8599 | PyErr_NoMemory(); |
| 8600 | goto error; |
| 8601 | } |
| 8602 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8603 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8605 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8606 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8608 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8609 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8610 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8611 | * this will cause previous value to be collected. This has to |
| 8612 | * happen after the real putenv() call because the old value |
| 8613 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8614 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8615 | /* really not much we can do; just leak */ |
| 8616 | PyErr_Clear(); |
| 8617 | } |
| 8618 | else { |
| 8619 | Py_DECREF(newstr); |
| 8620 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8621 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8622 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8623 | Py_DECREF(os1); |
| 8624 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8625 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8626 | Py_RETURN_NONE; |
| 8627 | |
| 8628 | error: |
| 8629 | #ifndef MS_WINDOWS |
| 8630 | Py_DECREF(os1); |
| 8631 | Py_DECREF(os2); |
| 8632 | #endif |
| 8633 | Py_XDECREF(newstr); |
| 8634 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8635 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8636 | #endif /* putenv */ |
| 8637 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8638 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8639 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8640 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8641 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8642 | |
| 8643 | static PyObject * |
| 8644 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8645 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8646 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8647 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8648 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8649 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8650 | |
| 8651 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8652 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8653 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8654 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8655 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8656 | #ifdef HAVE_BROKEN_UNSETENV |
| 8657 | unsetenv(PyBytes_AS_STRING(name)); |
| 8658 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8659 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8660 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8661 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8662 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8663 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8664 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8665 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8666 | /* Remove the key from posix_putenv_garbage; |
| 8667 | * this will cause it to be collected. This has to |
| 8668 | * happen after the real unsetenv() call because the |
| 8669 | * old value was still accessible until then. |
| 8670 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8671 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8672 | /* really not much we can do; just leak */ |
| 8673 | PyErr_Clear(); |
| 8674 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8675 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8676 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8677 | } |
| 8678 | #endif /* unsetenv */ |
| 8679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8680 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8681 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8682 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8683 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8684 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8685 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8686 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8687 | int code; |
| 8688 | char *message; |
| 8689 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8690 | return NULL; |
| 8691 | message = strerror(code); |
| 8692 | if (message == NULL) { |
| 8693 | PyErr_SetString(PyExc_ValueError, |
| 8694 | "strerror() argument out of range"); |
| 8695 | return NULL; |
| 8696 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8697 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8698 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8699 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8700 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8701 | #ifdef HAVE_SYS_WAIT_H |
| 8702 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8703 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8704 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8705 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8706 | 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] | 8707 | |
| 8708 | static PyObject * |
| 8709 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 8710 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8711 | WAIT_TYPE status; |
| 8712 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8713 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8714 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 8715 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8716 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8717 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8718 | } |
| 8719 | #endif /* WCOREDUMP */ |
| 8720 | |
| 8721 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8722 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8723 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8724 | 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] | 8725 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8726 | |
| 8727 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8728 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8729 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8730 | WAIT_TYPE status; |
| 8731 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8732 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8733 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 8734 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8735 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8736 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8737 | } |
| 8738 | #endif /* WIFCONTINUED */ |
| 8739 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8740 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8741 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8742 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8743 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8744 | |
| 8745 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8746 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8747 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8748 | WAIT_TYPE status; |
| 8749 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8750 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8751 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 8752 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8753 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8754 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8755 | } |
| 8756 | #endif /* WIFSTOPPED */ |
| 8757 | |
| 8758 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8759 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8760 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8761 | 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] | 8762 | |
| 8763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8764 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8765 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8766 | WAIT_TYPE status; |
| 8767 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8768 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8769 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 8770 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8771 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8772 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8773 | } |
| 8774 | #endif /* WIFSIGNALED */ |
| 8775 | |
| 8776 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8777 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8778 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8779 | 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] | 8780 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8781 | |
| 8782 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8783 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8784 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8785 | WAIT_TYPE status; |
| 8786 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8787 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8788 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 8789 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8790 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8791 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8792 | } |
| 8793 | #endif /* WIFEXITED */ |
| 8794 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8795 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8796 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8797 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8798 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8799 | |
| 8800 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8801 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8802 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8803 | WAIT_TYPE status; |
| 8804 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8806 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8807 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8808 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8809 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8810 | } |
| 8811 | #endif /* WEXITSTATUS */ |
| 8812 | |
| 8813 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8814 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8815 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8816 | 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] | 8817 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8818 | |
| 8819 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8820 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8821 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8822 | WAIT_TYPE status; |
| 8823 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8824 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8825 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8826 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8827 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8828 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8829 | } |
| 8830 | #endif /* WTERMSIG */ |
| 8831 | |
| 8832 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8833 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8834 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8835 | Return the signal that stopped the process that provided\n\ |
| 8836 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8837 | |
| 8838 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8839 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8840 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8841 | WAIT_TYPE status; |
| 8842 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8843 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8844 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8845 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8846 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8847 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8848 | } |
| 8849 | #endif /* WSTOPSIG */ |
| 8850 | |
| 8851 | #endif /* HAVE_SYS_WAIT_H */ |
| 8852 | |
| 8853 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8854 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8855 | #ifdef _SCO_DS |
| 8856 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8857 | needed definitions in sys/statvfs.h */ |
| 8858 | #define _SVID3 |
| 8859 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8860 | #include <sys/statvfs.h> |
| 8861 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8862 | static PyObject* |
| 8863 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8864 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8865 | if (v == NULL) |
| 8866 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8867 | |
| 8868 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8869 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8870 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8871 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8872 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8873 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8874 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8875 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8876 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8877 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8878 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8879 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8880 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8881 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8882 | PyStructSequence_SET_ITEM(v, 2, |
| 8883 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8884 | PyStructSequence_SET_ITEM(v, 3, |
| 8885 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8886 | PyStructSequence_SET_ITEM(v, 4, |
| 8887 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8888 | PyStructSequence_SET_ITEM(v, 5, |
| 8889 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8890 | PyStructSequence_SET_ITEM(v, 6, |
| 8891 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8892 | PyStructSequence_SET_ITEM(v, 7, |
| 8893 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8894 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8895 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8896 | #endif |
| 8897 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8898 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8899 | } |
| 8900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8901 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8902 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8903 | Perform an fstatvfs system call on the given fd.\n\ |
| 8904 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8905 | |
| 8906 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8907 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8908 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8909 | int fd, res; |
| 8910 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8912 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8913 | return NULL; |
| 8914 | Py_BEGIN_ALLOW_THREADS |
| 8915 | res = fstatvfs(fd, &st); |
| 8916 | Py_END_ALLOW_THREADS |
| 8917 | if (res != 0) |
| 8918 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8919 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8920 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8921 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8922 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8923 | |
| 8924 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8925 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8926 | #include <sys/statvfs.h> |
| 8927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8928 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8929 | "statvfs(path)\n\n\ |
| 8930 | Perform a statvfs system call on the given path.\n\ |
| 8931 | \n\ |
| 8932 | path may always be specified as a string.\n\ |
| 8933 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8934 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8935 | |
| 8936 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8937 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8938 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8939 | static char *keywords[] = {"path", NULL}; |
| 8940 | path_t path; |
| 8941 | int result; |
| 8942 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8943 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8944 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8945 | memset(&path, 0, sizeof(path)); |
| 8946 | #ifdef HAVE_FSTATVFS |
| 8947 | path.allow_fd = 1; |
| 8948 | #endif |
| 8949 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 8950 | path_converter, &path |
| 8951 | )) |
| 8952 | return NULL; |
| 8953 | |
| 8954 | Py_BEGIN_ALLOW_THREADS |
| 8955 | #ifdef HAVE_FSTATVFS |
| 8956 | if (path.fd != -1) { |
| 8957 | #ifdef __APPLE__ |
| 8958 | /* handle weak-linking on Mac OS X 10.3 */ |
| 8959 | if (fstatvfs == NULL) { |
| 8960 | fd_specified("statvfs", path.fd); |
| 8961 | goto exit; |
| 8962 | } |
| 8963 | #endif |
| 8964 | result = fstatvfs(path.fd, &st); |
| 8965 | } |
| 8966 | else |
| 8967 | #endif |
| 8968 | result = statvfs(path.narrow, &st); |
| 8969 | Py_END_ALLOW_THREADS |
| 8970 | |
| 8971 | if (result) { |
| 8972 | return_value = path_posix_error("statvfs", &path); |
| 8973 | goto exit; |
| 8974 | } |
| 8975 | |
| 8976 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 8977 | |
| 8978 | exit: |
| 8979 | path_cleanup(&path); |
| 8980 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8981 | } |
| 8982 | #endif /* HAVE_STATVFS */ |
| 8983 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8984 | #ifdef MS_WINDOWS |
| 8985 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8986 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8987 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8988 | |
| 8989 | static PyObject * |
| 8990 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8991 | { |
| 8992 | BOOL retval; |
| 8993 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8994 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8995 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8996 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8997 | return NULL; |
| 8998 | |
| 8999 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9000 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9001 | Py_END_ALLOW_THREADS |
| 9002 | if (retval == 0) |
| 9003 | return PyErr_SetFromWindowsErr(0); |
| 9004 | |
| 9005 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9006 | } |
| 9007 | #endif |
| 9008 | |
| 9009 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9010 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9011 | * It maps strings representing configuration variable names to |
| 9012 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9013 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9014 | * rarely-used constants. There are three separate tables that use |
| 9015 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9016 | * |
| 9017 | * This code is always included, even if none of the interfaces that |
| 9018 | * need it are included. The #if hackery needed to avoid it would be |
| 9019 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9020 | */ |
| 9021 | struct constdef { |
| 9022 | char *name; |
| 9023 | long value; |
| 9024 | }; |
| 9025 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9026 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9027 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9028 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9029 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9030 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9031 | *valuep = PyLong_AS_LONG(arg); |
| 9032 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9033 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9034 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9035 | /* look up the value in the table using a binary search */ |
| 9036 | size_t lo = 0; |
| 9037 | size_t mid; |
| 9038 | size_t hi = tablesize; |
| 9039 | int cmp; |
| 9040 | const char *confname; |
| 9041 | if (!PyUnicode_Check(arg)) { |
| 9042 | PyErr_SetString(PyExc_TypeError, |
| 9043 | "configuration names must be strings or integers"); |
| 9044 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9045 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9046 | confname = _PyUnicode_AsString(arg); |
| 9047 | if (confname == NULL) |
| 9048 | return 0; |
| 9049 | while (lo < hi) { |
| 9050 | mid = (lo + hi) / 2; |
| 9051 | cmp = strcmp(confname, table[mid].name); |
| 9052 | if (cmp < 0) |
| 9053 | hi = mid; |
| 9054 | else if (cmp > 0) |
| 9055 | lo = mid + 1; |
| 9056 | else { |
| 9057 | *valuep = table[mid].value; |
| 9058 | return 1; |
| 9059 | } |
| 9060 | } |
| 9061 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9062 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9063 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9064 | } |
| 9065 | |
| 9066 | |
| 9067 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9068 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9069 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9070 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9071 | #endif |
| 9072 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9073 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9074 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9075 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9076 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9077 | #endif |
| 9078 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9079 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9080 | #endif |
| 9081 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9082 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9083 | #endif |
| 9084 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9085 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9086 | #endif |
| 9087 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9088 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9089 | #endif |
| 9090 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9091 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9092 | #endif |
| 9093 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9094 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9095 | #endif |
| 9096 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9097 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9098 | #endif |
| 9099 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9100 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9101 | #endif |
| 9102 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9103 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9104 | #endif |
| 9105 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9106 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9107 | #endif |
| 9108 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9109 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9110 | #endif |
| 9111 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9112 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9113 | #endif |
| 9114 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9115 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9116 | #endif |
| 9117 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9118 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9119 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9120 | #ifdef _PC_ACL_ENABLED |
| 9121 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9122 | #endif |
| 9123 | #ifdef _PC_MIN_HOLE_SIZE |
| 9124 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9125 | #endif |
| 9126 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9127 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9128 | #endif |
| 9129 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9130 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9131 | #endif |
| 9132 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9133 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9134 | #endif |
| 9135 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9136 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9137 | #endif |
| 9138 | #ifdef _PC_REC_XFER_ALIGN |
| 9139 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9140 | #endif |
| 9141 | #ifdef _PC_SYMLINK_MAX |
| 9142 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9143 | #endif |
| 9144 | #ifdef _PC_XATTR_ENABLED |
| 9145 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9146 | #endif |
| 9147 | #ifdef _PC_XATTR_EXISTS |
| 9148 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9149 | #endif |
| 9150 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9151 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9152 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9153 | }; |
| 9154 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9155 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9156 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9157 | { |
| 9158 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9159 | sizeof(posix_constants_pathconf) |
| 9160 | / sizeof(struct constdef)); |
| 9161 | } |
| 9162 | #endif |
| 9163 | |
| 9164 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9165 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9166 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9167 | 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] | 9168 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9169 | |
| 9170 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9171 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9172 | { |
| 9173 | PyObject *result = NULL; |
| 9174 | int name, fd; |
| 9175 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9176 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 9177 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9178 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9179 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9180 | errno = 0; |
| 9181 | limit = fpathconf(fd, name); |
| 9182 | if (limit == -1 && errno != 0) |
| 9183 | posix_error(); |
| 9184 | else |
| 9185 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9186 | } |
| 9187 | return result; |
| 9188 | } |
| 9189 | #endif |
| 9190 | |
| 9191 | |
| 9192 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9193 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9194 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9195 | Return the configuration limit name for the file or directory path.\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9196 | If there is no limit, return -1.\n\ |
| 9197 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9198 | If this functionality is unavailable, using it raises an exception."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9199 | |
| 9200 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9201 | posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9202 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9203 | path_t path; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9204 | PyObject *result = NULL; |
| 9205 | int name; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9206 | static char *keywords[] = {"path", "name", NULL}; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9207 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9208 | memset(&path, 0, sizeof(path)); |
| 9209 | #ifdef HAVE_FPATHCONF |
| 9210 | path.allow_fd = 1; |
| 9211 | #endif |
| 9212 | if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords, |
| 9213 | path_converter, &path, |
| 9214 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9215 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9216 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9217 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9218 | #ifdef HAVE_FPATHCONF |
| 9219 | if (path.fd != -1) |
| 9220 | limit = fpathconf(path.fd, name); |
| 9221 | else |
| 9222 | #endif |
| 9223 | limit = pathconf(path.narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9224 | if (limit == -1 && errno != 0) { |
| 9225 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9226 | /* could be a path or name problem */ |
| 9227 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9228 | else |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9229 | result = path_posix_error("pathconf", &path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9230 | } |
| 9231 | else |
| 9232 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9233 | } |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9234 | path_cleanup(&path); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9235 | return result; |
| 9236 | } |
| 9237 | #endif |
| 9238 | |
| 9239 | #ifdef HAVE_CONFSTR |
| 9240 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9241 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9242 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9243 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9244 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9245 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9246 | #endif |
| 9247 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9248 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9249 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9250 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9251 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9252 | #endif |
| 9253 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9254 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9255 | #endif |
| 9256 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9258 | #endif |
| 9259 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9260 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9261 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9262 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9263 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9264 | #endif |
| 9265 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9266 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9267 | #endif |
| 9268 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9269 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9270 | #endif |
| 9271 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9272 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9273 | #endif |
| 9274 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9275 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9276 | #endif |
| 9277 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9279 | #endif |
| 9280 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9281 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9282 | #endif |
| 9283 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9284 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9285 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9286 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9287 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9288 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9289 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9290 | {"CS_PATH", _CS_PATH}, |
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 _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9293 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9294 | #endif |
| 9295 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9296 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9297 | #endif |
| 9298 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9299 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9300 | #endif |
| 9301 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9302 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9303 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9304 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9305 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9306 | #endif |
| 9307 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9308 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9309 | #endif |
| 9310 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9311 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9312 | #endif |
| 9313 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9314 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9315 | #endif |
| 9316 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9317 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9318 | #endif |
| 9319 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9320 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9321 | #endif |
| 9322 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9323 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9324 | #endif |
| 9325 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9326 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9327 | #endif |
| 9328 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9329 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9330 | #endif |
| 9331 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9332 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9333 | #endif |
| 9334 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9335 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9336 | #endif |
| 9337 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9338 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9339 | #endif |
| 9340 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9341 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9342 | #endif |
| 9343 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9344 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9345 | #endif |
| 9346 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9347 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9348 | #endif |
| 9349 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9350 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9351 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9352 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9353 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9354 | #endif |
| 9355 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9356 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9357 | #endif |
| 9358 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9359 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9360 | #endif |
| 9361 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9362 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9363 | #endif |
| 9364 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9365 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9366 | #endif |
| 9367 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9368 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9369 | #endif |
| 9370 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9371 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9372 | #endif |
| 9373 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9375 | #endif |
| 9376 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9377 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9378 | #endif |
| 9379 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9380 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9381 | #endif |
| 9382 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9383 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9384 | #endif |
| 9385 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9386 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9387 | #endif |
| 9388 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9389 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9390 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9391 | }; |
| 9392 | |
| 9393 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9394 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9395 | { |
| 9396 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9397 | sizeof(posix_constants_confstr) |
| 9398 | / sizeof(struct constdef)); |
| 9399 | } |
| 9400 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9401 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9402 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9403 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9404 | |
| 9405 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9406 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9407 | { |
| 9408 | PyObject *result = NULL; |
| 9409 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9410 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9411 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9412 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9413 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 9414 | return NULL; |
| 9415 | |
| 9416 | errno = 0; |
| 9417 | len = confstr(name, buffer, sizeof(buffer)); |
| 9418 | if (len == 0) { |
| 9419 | if (errno) { |
| 9420 | posix_error(); |
| 9421 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9422 | } |
| 9423 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9424 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9425 | } |
| 9426 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9427 | |
| 9428 | if ((unsigned int)len >= sizeof(buffer)) { |
| 9429 | char *buf = PyMem_Malloc(len); |
| 9430 | if (buf == NULL) |
| 9431 | return PyErr_NoMemory(); |
| 9432 | confstr(name, buf, len); |
| 9433 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9434 | PyMem_Free(buf); |
| 9435 | } |
| 9436 | else |
| 9437 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9438 | return result; |
| 9439 | } |
| 9440 | #endif |
| 9441 | |
| 9442 | |
| 9443 | #ifdef HAVE_SYSCONF |
| 9444 | static struct constdef posix_constants_sysconf[] = { |
| 9445 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9446 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9447 | #endif |
| 9448 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9449 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9450 | #endif |
| 9451 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9452 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9453 | #endif |
| 9454 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9455 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9456 | #endif |
| 9457 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9458 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9459 | #endif |
| 9460 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9461 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9462 | #endif |
| 9463 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9464 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9465 | #endif |
| 9466 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9467 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9468 | #endif |
| 9469 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9470 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9471 | #endif |
| 9472 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9473 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9474 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9475 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9476 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9477 | #endif |
| 9478 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9479 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9480 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9481 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9482 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9483 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9484 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9485 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9486 | #endif |
| 9487 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9488 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9489 | #endif |
| 9490 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9491 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9492 | #endif |
| 9493 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9494 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9495 | #endif |
| 9496 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9497 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9498 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9499 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9500 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9501 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9502 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9503 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9504 | #endif |
| 9505 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9506 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9507 | #endif |
| 9508 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9509 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9510 | #endif |
| 9511 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9512 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9513 | #endif |
| 9514 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9515 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9516 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9517 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9518 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9519 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9520 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9521 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9522 | #endif |
| 9523 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9524 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9525 | #endif |
| 9526 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9527 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9528 | #endif |
| 9529 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9530 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9531 | #endif |
| 9532 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9533 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9534 | #endif |
| 9535 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9536 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9537 | #endif |
| 9538 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9539 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9540 | #endif |
| 9541 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9542 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9543 | #endif |
| 9544 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9545 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9546 | #endif |
| 9547 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9548 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9549 | #endif |
| 9550 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9551 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9552 | #endif |
| 9553 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9554 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9555 | #endif |
| 9556 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9557 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9558 | #endif |
| 9559 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9560 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9561 | #endif |
| 9562 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9563 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9564 | #endif |
| 9565 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9566 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9567 | #endif |
| 9568 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9569 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9570 | #endif |
| 9571 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9572 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9573 | #endif |
| 9574 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9575 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9576 | #endif |
| 9577 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9578 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9579 | #endif |
| 9580 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9581 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9582 | #endif |
| 9583 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9584 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9585 | #endif |
| 9586 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9587 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9588 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9589 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9590 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9591 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9592 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9593 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9594 | #endif |
| 9595 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9596 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9597 | #endif |
| 9598 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9599 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9600 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9601 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9602 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9603 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9604 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9605 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9606 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9607 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9608 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9609 | #endif |
| 9610 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9611 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9612 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9613 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9614 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9615 | #endif |
| 9616 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9617 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9618 | #endif |
| 9619 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9620 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9621 | #endif |
| 9622 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9623 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9624 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9625 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9626 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9627 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9628 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9629 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9630 | #endif |
| 9631 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9632 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9633 | #endif |
| 9634 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9635 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9636 | #endif |
| 9637 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9638 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9639 | #endif |
| 9640 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9641 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9642 | #endif |
| 9643 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9644 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9645 | #endif |
| 9646 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9647 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9648 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9649 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9650 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9651 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9652 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9653 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9654 | #endif |
| 9655 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9656 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9657 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9658 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9659 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9660 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9661 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9662 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9663 | #endif |
| 9664 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9665 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9666 | #endif |
| 9667 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9668 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9669 | #endif |
| 9670 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9671 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9672 | #endif |
| 9673 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9674 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9675 | #endif |
| 9676 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9677 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9678 | #endif |
| 9679 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9680 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9681 | #endif |
| 9682 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9683 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9684 | #endif |
| 9685 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9686 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9687 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9688 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9689 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9690 | #endif |
| 9691 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9692 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9693 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9694 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9695 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9696 | #endif |
| 9697 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9698 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9699 | #endif |
| 9700 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9701 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9702 | #endif |
| 9703 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9704 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9705 | #endif |
| 9706 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9707 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9708 | #endif |
| 9709 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9710 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9711 | #endif |
| 9712 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9713 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9714 | #endif |
| 9715 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9716 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9717 | #endif |
| 9718 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9719 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9720 | #endif |
| 9721 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9722 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9723 | #endif |
| 9724 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9725 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9726 | #endif |
| 9727 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9728 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9729 | #endif |
| 9730 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9731 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9732 | #endif |
| 9733 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9734 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9735 | #endif |
| 9736 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9737 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9738 | #endif |
| 9739 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9740 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9741 | #endif |
| 9742 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9743 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9744 | #endif |
| 9745 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9746 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9747 | #endif |
| 9748 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9749 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9750 | #endif |
| 9751 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9752 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9753 | #endif |
| 9754 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9755 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9756 | #endif |
| 9757 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9758 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9759 | #endif |
| 9760 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9761 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9762 | #endif |
| 9763 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9764 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9765 | #endif |
| 9766 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9767 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9768 | #endif |
| 9769 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9770 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9771 | #endif |
| 9772 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9773 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9774 | #endif |
| 9775 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9776 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9777 | #endif |
| 9778 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9779 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9780 | #endif |
| 9781 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9782 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9783 | #endif |
| 9784 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9785 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9786 | #endif |
| 9787 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9788 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9789 | #endif |
| 9790 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9791 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9792 | #endif |
| 9793 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9794 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9795 | #endif |
| 9796 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9797 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9798 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9799 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9800 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9801 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9802 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9803 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9804 | #endif |
| 9805 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9806 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9807 | #endif |
| 9808 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9809 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9810 | #endif |
| 9811 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9812 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9813 | #endif |
| 9814 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9815 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9816 | #endif |
| 9817 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9818 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9819 | #endif |
| 9820 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9821 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9822 | #endif |
| 9823 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9824 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9825 | #endif |
| 9826 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9827 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9828 | #endif |
| 9829 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9830 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9831 | #endif |
| 9832 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9833 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9834 | #endif |
| 9835 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9836 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9837 | #endif |
| 9838 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9839 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9840 | #endif |
| 9841 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9842 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9843 | #endif |
| 9844 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9845 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9846 | #endif |
| 9847 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9848 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9849 | #endif |
| 9850 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9851 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9852 | #endif |
| 9853 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9854 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9855 | #endif |
| 9856 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9857 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9858 | #endif |
| 9859 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9860 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9861 | #endif |
| 9862 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9863 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9864 | #endif |
| 9865 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9866 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9867 | #endif |
| 9868 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9869 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9870 | #endif |
| 9871 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9872 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9873 | #endif |
| 9874 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9875 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9876 | #endif |
| 9877 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9878 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9879 | #endif |
| 9880 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9881 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9882 | #endif |
| 9883 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9884 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9885 | #endif |
| 9886 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9887 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9888 | #endif |
| 9889 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9890 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9891 | #endif |
| 9892 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9893 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9894 | #endif |
| 9895 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9896 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9897 | #endif |
| 9898 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9899 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9900 | #endif |
| 9901 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9902 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9903 | #endif |
| 9904 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9905 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9906 | #endif |
| 9907 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9908 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9909 | #endif |
| 9910 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9911 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9912 | #endif |
| 9913 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9914 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9915 | #endif |
| 9916 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9917 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9918 | #endif |
| 9919 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9920 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9921 | #endif |
| 9922 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9923 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9924 | #endif |
| 9925 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9926 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9927 | #endif |
| 9928 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9929 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9930 | #endif |
| 9931 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9932 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9933 | #endif |
| 9934 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9935 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9936 | #endif |
| 9937 | }; |
| 9938 | |
| 9939 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9940 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9941 | { |
| 9942 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9943 | sizeof(posix_constants_sysconf) |
| 9944 | / sizeof(struct constdef)); |
| 9945 | } |
| 9946 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9947 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9948 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9949 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9950 | |
| 9951 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9952 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9953 | { |
| 9954 | PyObject *result = NULL; |
| 9955 | int name; |
| 9956 | |
| 9957 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9958 | int value; |
| 9959 | |
| 9960 | errno = 0; |
| 9961 | value = sysconf(name); |
| 9962 | if (value == -1 && errno != 0) |
| 9963 | posix_error(); |
| 9964 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9965 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9966 | } |
| 9967 | return result; |
| 9968 | } |
| 9969 | #endif |
| 9970 | |
| 9971 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9972 | /* This code is used to ensure that the tables of configuration value names |
| 9973 | * are in sorted order as required by conv_confname(), and also to build the |
| 9974 | * the exported dictionaries that are used to publish information about the |
| 9975 | * names available on the host platform. |
| 9976 | * |
| 9977 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9978 | * when used, even for platforms we're not able to test on. It also makes |
| 9979 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9980 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9981 | |
| 9982 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9983 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9984 | { |
| 9985 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9986 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9987 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9988 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9989 | |
| 9990 | return strcmp(c1->name, c2->name); |
| 9991 | } |
| 9992 | |
| 9993 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9994 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9995 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9996 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9997 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9998 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9999 | |
| 10000 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10001 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10002 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10003 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10004 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10005 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10006 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10007 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10008 | Py_XDECREF(o); |
| 10009 | Py_DECREF(d); |
| 10010 | return -1; |
| 10011 | } |
| 10012 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10013 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10014 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10015 | } |
| 10016 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10017 | /* Return -1 on failure, 0 on success. */ |
| 10018 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10019 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10020 | { |
| 10021 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10022 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10023 | sizeof(posix_constants_pathconf) |
| 10024 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10025 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10026 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10027 | #endif |
| 10028 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10029 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10030 | sizeof(posix_constants_confstr) |
| 10031 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10032 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10033 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10034 | #endif |
| 10035 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10036 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10037 | sizeof(posix_constants_sysconf) |
| 10038 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10039 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10040 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10041 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10042 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10043 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10044 | |
| 10045 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10046 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10047 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10048 | 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] | 10049 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10050 | |
| 10051 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10052 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10053 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10054 | abort(); |
| 10055 | /*NOTREACHED*/ |
| 10056 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10057 | return NULL; |
| 10058 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10059 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10060 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10061 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10062 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 10063 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10064 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10065 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10066 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10067 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10068 | application (if any) its extension is associated.\n\ |
| 10069 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10070 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10071 | \n\ |
| 10072 | startfile returns as soon as the associated application is launched.\n\ |
| 10073 | There is no option to wait for the application to close, and no way\n\ |
| 10074 | to retrieve the application's exit status.\n\ |
| 10075 | \n\ |
| 10076 | The filepath is relative to the current directory. If you want to use\n\ |
| 10077 | 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] | 10078 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10079 | |
| 10080 | static PyObject * |
| 10081 | win32_startfile(PyObject *self, PyObject *args) |
| 10082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10083 | PyObject *ofilepath; |
| 10084 | char *filepath; |
| 10085 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10086 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10087 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10088 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10089 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10090 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10091 | &unipath, &operation)) { |
| 10092 | PyErr_Clear(); |
| 10093 | goto normal; |
| 10094 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10096 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10097 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10098 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10099 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10100 | PyErr_Clear(); |
| 10101 | operation = NULL; |
| 10102 | goto normal; |
| 10103 | } |
| 10104 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10105 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10106 | wpath = PyUnicode_AsUnicode(unipath); |
| 10107 | if (wpath == NULL) |
| 10108 | goto normal; |
| 10109 | if (uoperation) { |
| 10110 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10111 | if (woperation == NULL) |
| 10112 | goto normal; |
| 10113 | } |
| 10114 | else |
| 10115 | woperation = NULL; |
| 10116 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10117 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10118 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 10119 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10120 | Py_END_ALLOW_THREADS |
| 10121 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10122 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10123 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10124 | win32_error_object("startfile", unipath); |
| 10125 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10126 | } |
| 10127 | Py_INCREF(Py_None); |
| 10128 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10129 | |
| 10130 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10131 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10132 | PyUnicode_FSConverter, &ofilepath, |
| 10133 | &operation)) |
| 10134 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10135 | if (win32_warn_bytes_api()) { |
| 10136 | Py_DECREF(ofilepath); |
| 10137 | return NULL; |
| 10138 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10139 | filepath = PyBytes_AsString(ofilepath); |
| 10140 | Py_BEGIN_ALLOW_THREADS |
| 10141 | rc = ShellExecute((HWND)0, operation, filepath, |
| 10142 | NULL, NULL, SW_SHOWNORMAL); |
| 10143 | Py_END_ALLOW_THREADS |
| 10144 | if (rc <= (HINSTANCE)32) { |
| 10145 | PyObject *errval = win32_error("startfile", filepath); |
| 10146 | Py_DECREF(ofilepath); |
| 10147 | return errval; |
| 10148 | } |
| 10149 | Py_DECREF(ofilepath); |
| 10150 | Py_INCREF(Py_None); |
| 10151 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10152 | } |
| 10153 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10154 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10155 | #ifdef HAVE_GETLOADAVG |
| 10156 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 10157 | "getloadavg() -> (float, float, float)\n\n\ |
| 10158 | Return the number of processes in the system run queue averaged over\n\ |
| 10159 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 10160 | was unobtainable"); |
| 10161 | |
| 10162 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10163 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10164 | { |
| 10165 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10166 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10167 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10168 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10169 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10170 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10171 | } |
| 10172 | #endif |
| 10173 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10174 | PyDoc_STRVAR(device_encoding__doc__, |
| 10175 | "device_encoding(fd) -> str\n\n\ |
| 10176 | Return a string describing the encoding of the device\n\ |
| 10177 | if the output is a terminal; else return None."); |
| 10178 | |
| 10179 | static PyObject * |
| 10180 | device_encoding(PyObject *self, PyObject *args) |
| 10181 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10182 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10184 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 10185 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10186 | |
| 10187 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10188 | } |
| 10189 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10190 | #ifdef HAVE_SETRESUID |
| 10191 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 10192 | "setresuid(ruid, euid, suid)\n\n\ |
| 10193 | Set the current process's real, effective, and saved user ids."); |
| 10194 | |
| 10195 | static PyObject* |
| 10196 | posix_setresuid (PyObject *self, PyObject *args) |
| 10197 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10198 | /* We assume uid_t is no larger than a long. */ |
| 10199 | long ruid, euid, suid; |
| 10200 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 10201 | return NULL; |
| 10202 | if (setresuid(ruid, euid, suid) < 0) |
| 10203 | return posix_error(); |
| 10204 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10205 | } |
| 10206 | #endif |
| 10207 | |
| 10208 | #ifdef HAVE_SETRESGID |
| 10209 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 10210 | "setresgid(rgid, egid, sgid)\n\n\ |
| 10211 | Set the current process's real, effective, and saved group ids."); |
| 10212 | |
| 10213 | static PyObject* |
| 10214 | posix_setresgid (PyObject *self, PyObject *args) |
| 10215 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10216 | /* We assume uid_t is no larger than a long. */ |
| 10217 | long rgid, egid, sgid; |
| 10218 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 10219 | return NULL; |
| 10220 | if (setresgid(rgid, egid, sgid) < 0) |
| 10221 | return posix_error(); |
| 10222 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10223 | } |
| 10224 | #endif |
| 10225 | |
| 10226 | #ifdef HAVE_GETRESUID |
| 10227 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 10228 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 10229 | Get tuple of the current process's real, effective, and saved user ids."); |
| 10230 | |
| 10231 | static PyObject* |
| 10232 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 10233 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10234 | uid_t ruid, euid, suid; |
| 10235 | long l_ruid, l_euid, l_suid; |
| 10236 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10237 | return posix_error(); |
| 10238 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 10239 | l_ruid = ruid; |
| 10240 | l_euid = euid; |
| 10241 | l_suid = suid; |
| 10242 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10243 | } |
| 10244 | #endif |
| 10245 | |
| 10246 | #ifdef HAVE_GETRESGID |
| 10247 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 10248 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 10249 | 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] | 10250 | |
| 10251 | static PyObject* |
| 10252 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 10253 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10254 | uid_t rgid, egid, sgid; |
| 10255 | long l_rgid, l_egid, l_sgid; |
| 10256 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10257 | return posix_error(); |
| 10258 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 10259 | l_rgid = rgid; |
| 10260 | l_egid = egid; |
| 10261 | l_sgid = sgid; |
| 10262 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10263 | } |
| 10264 | #endif |
| 10265 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10266 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10267 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10268 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10269 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 10270 | Return the value of extended attribute attribute on path.\n\ |
| 10271 | \n\ |
| 10272 | path may be either a string or an open file descriptor.\n\ |
| 10273 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10274 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 10275 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10276 | |
| 10277 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10278 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10279 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10280 | path_t path; |
| 10281 | path_t attribute; |
| 10282 | int follow_symlinks = 1; |
| 10283 | PyObject *buffer = NULL; |
| 10284 | int i; |
| 10285 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10286 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10287 | memset(&path, 0, sizeof(path)); |
| 10288 | memset(&attribute, 0, sizeof(attribute)); |
| 10289 | path.allow_fd = 1; |
| 10290 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 10291 | path_converter, &path, |
| 10292 | path_converter, &attribute, |
| 10293 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10294 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10295 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10296 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 10297 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10298 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10299 | for (i = 0; ; i++) { |
| 10300 | void *ptr; |
| 10301 | ssize_t result; |
| 10302 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 10303 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10304 | if (!buffer_size) { |
| 10305 | path_error("getxattr", &path); |
| 10306 | goto exit; |
| 10307 | } |
| 10308 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10309 | if (!buffer) |
| 10310 | goto exit; |
| 10311 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10312 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10313 | Py_BEGIN_ALLOW_THREADS; |
| 10314 | if (path.fd >= 0) |
| 10315 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 10316 | else if (follow_symlinks) |
| 10317 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10318 | else |
| 10319 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10320 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10321 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10322 | if (result < 0) { |
| 10323 | Py_DECREF(buffer); |
| 10324 | buffer = NULL; |
| 10325 | if (errno == ERANGE) |
| 10326 | continue; |
| 10327 | path_error("getxattr", &path); |
| 10328 | goto exit; |
| 10329 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10330 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10331 | if (result != buffer_size) { |
| 10332 | /* Can only shrink. */ |
| 10333 | _PyBytes_Resize(&buffer, result); |
| 10334 | } |
| 10335 | break; |
| 10336 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10337 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10338 | exit: |
| 10339 | path_cleanup(&path); |
| 10340 | path_cleanup(&attribute); |
| 10341 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10342 | } |
| 10343 | |
| 10344 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10345 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 10346 | Set extended attribute attribute on path to value.\n\ |
| 10347 | path may be either a string or an open file descriptor.\n\ |
| 10348 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10349 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 10350 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10351 | |
| 10352 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10353 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10354 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10355 | path_t path; |
| 10356 | path_t attribute; |
| 10357 | Py_buffer value; |
| 10358 | int flags = 0; |
| 10359 | int follow_symlinks = 1; |
| 10360 | int result; |
| 10361 | PyObject *return_value = NULL; |
| 10362 | static char *keywords[] = {"path", "attribute", "value", |
| 10363 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10364 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10365 | memset(&path, 0, sizeof(path)); |
| 10366 | path.allow_fd = 1; |
| 10367 | memset(&attribute, 0, sizeof(attribute)); |
| 10368 | memset(&value, 0, sizeof(value)); |
| 10369 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 10370 | keywords, |
| 10371 | path_converter, &path, |
| 10372 | path_converter, &attribute, |
| 10373 | &value, &flags, |
| 10374 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10375 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10376 | |
| 10377 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 10378 | goto exit; |
| 10379 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10380 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10381 | if (path.fd > -1) |
| 10382 | result = fsetxattr(path.fd, attribute.narrow, |
| 10383 | value.buf, value.len, flags); |
| 10384 | else if (follow_symlinks) |
| 10385 | result = setxattr(path.narrow, attribute.narrow, |
| 10386 | value.buf, value.len, flags); |
| 10387 | else |
| 10388 | result = lsetxattr(path.narrow, attribute.narrow, |
| 10389 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10390 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10391 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10392 | if (result) { |
| 10393 | return_value = path_error("setxattr", &path); |
| 10394 | goto exit; |
| 10395 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10396 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10397 | return_value = Py_None; |
| 10398 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10399 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10400 | exit: |
| 10401 | path_cleanup(&path); |
| 10402 | path_cleanup(&attribute); |
| 10403 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10404 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10405 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10406 | } |
| 10407 | |
| 10408 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10409 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 10410 | Remove extended attribute attribute on path.\n\ |
| 10411 | path may be either a string or an open file descriptor.\n\ |
| 10412 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10413 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 10414 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10415 | |
| 10416 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10417 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10418 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10419 | path_t path; |
| 10420 | path_t attribute; |
| 10421 | int follow_symlinks = 1; |
| 10422 | int result; |
| 10423 | PyObject *return_value = NULL; |
| 10424 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10425 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10426 | memset(&path, 0, sizeof(path)); |
| 10427 | memset(&attribute, 0, sizeof(attribute)); |
| 10428 | path.allow_fd = 1; |
| 10429 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 10430 | keywords, |
| 10431 | path_converter, &path, |
| 10432 | path_converter, &attribute, |
| 10433 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10434 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10435 | |
| 10436 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 10437 | goto exit; |
| 10438 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10439 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10440 | if (path.fd > -1) |
| 10441 | result = fremovexattr(path.fd, attribute.narrow); |
| 10442 | else if (follow_symlinks) |
| 10443 | result = removexattr(path.narrow, attribute.narrow); |
| 10444 | else |
| 10445 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10446 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10447 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10448 | if (result) { |
| 10449 | return_value = path_error("removexattr", &path); |
| 10450 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10451 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10452 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10453 | return_value = Py_None; |
| 10454 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10455 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10456 | exit: |
| 10457 | path_cleanup(&path); |
| 10458 | path_cleanup(&attribute); |
| 10459 | |
| 10460 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10461 | } |
| 10462 | |
| 10463 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10464 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 10465 | Return a list of extended attributes on path.\n\ |
| 10466 | \n\ |
| 10467 | path may be either None, a string, or an open file descriptor.\n\ |
| 10468 | if path is None, listxattr will examine the current directory.\n\ |
| 10469 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10470 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 10471 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10472 | |
| 10473 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10474 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10475 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10476 | path_t path; |
| 10477 | int follow_symlinks = 1; |
| 10478 | Py_ssize_t i; |
| 10479 | PyObject *result = NULL; |
| 10480 | char *buffer = NULL; |
| 10481 | char *name; |
| 10482 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10483 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10484 | memset(&path, 0, sizeof(path)); |
| 10485 | path.allow_fd = 1; |
| 10486 | path.fd = -1; |
| 10487 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 10488 | path_converter, &path, |
| 10489 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10490 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10491 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10492 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 10493 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10494 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10495 | name = path.narrow ? path.narrow : "."; |
| 10496 | for (i = 0; ; i++) { |
| 10497 | char *start, *trace, *end; |
| 10498 | ssize_t length; |
| 10499 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10500 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10501 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10502 | /* ERANGE */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10503 | path_error("listxattr", &path); |
| 10504 | break; |
| 10505 | } |
| 10506 | buffer = PyMem_MALLOC(buffer_size); |
| 10507 | if (!buffer) { |
| 10508 | PyErr_NoMemory(); |
| 10509 | break; |
| 10510 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10511 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10512 | Py_BEGIN_ALLOW_THREADS; |
| 10513 | if (path.fd > -1) |
| 10514 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10515 | else if (follow_symlinks) |
| 10516 | length = listxattr(name, buffer, buffer_size); |
| 10517 | else |
| 10518 | length = llistxattr(name, buffer, buffer_size); |
| 10519 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10520 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10521 | if (length < 0) { |
| 10522 | if (errno == ERANGE) |
| 10523 | continue; |
| 10524 | path_error("listxattr", &path); |
| 10525 | break; |
| 10526 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10527 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10528 | result = PyList_New(0); |
| 10529 | if (!result) { |
| 10530 | goto exit; |
| 10531 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10532 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10533 | end = buffer + length; |
| 10534 | for (trace = start = buffer; trace != end; trace++) { |
| 10535 | if (!*trace) { |
| 10536 | int error; |
| 10537 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10538 | trace - start); |
| 10539 | if (!attribute) { |
| 10540 | Py_DECREF(result); |
| 10541 | result = NULL; |
| 10542 | goto exit; |
| 10543 | } |
| 10544 | error = PyList_Append(result, attribute); |
| 10545 | Py_DECREF(attribute); |
| 10546 | if (error) { |
| 10547 | Py_DECREF(result); |
| 10548 | result = NULL; |
| 10549 | goto exit; |
| 10550 | } |
| 10551 | start = trace + 1; |
| 10552 | } |
| 10553 | } |
| 10554 | break; |
| 10555 | } |
| 10556 | exit: |
| 10557 | path_cleanup(&path); |
| 10558 | if (buffer) |
| 10559 | PyMem_FREE(buffer); |
| 10560 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10561 | } |
| 10562 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10563 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10564 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10565 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10566 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10567 | "urandom(n) -> str\n\n\ |
| 10568 | Return n random bytes suitable for cryptographic use."); |
| 10569 | |
| 10570 | static PyObject * |
| 10571 | posix_urandom(PyObject *self, PyObject *args) |
| 10572 | { |
| 10573 | Py_ssize_t size; |
| 10574 | PyObject *result; |
| 10575 | int ret; |
| 10576 | |
| 10577 | /* Read arguments */ |
| 10578 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10579 | return NULL; |
| 10580 | if (size < 0) |
| 10581 | return PyErr_Format(PyExc_ValueError, |
| 10582 | "negative argument not allowed"); |
| 10583 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10584 | if (result == NULL) |
| 10585 | return NULL; |
| 10586 | |
| 10587 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10588 | PyBytes_GET_SIZE(result)); |
| 10589 | if (ret == -1) { |
| 10590 | Py_DECREF(result); |
| 10591 | return NULL; |
| 10592 | } |
| 10593 | return result; |
| 10594 | } |
| 10595 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10596 | /* Terminal size querying */ |
| 10597 | |
| 10598 | static PyTypeObject TerminalSizeType; |
| 10599 | |
| 10600 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10601 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10602 | |
| 10603 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10604 | {"columns", "width of the terminal window in characters"}, |
| 10605 | {"lines", "height of the terminal window in characters"}, |
| 10606 | {NULL, NULL} |
| 10607 | }; |
| 10608 | |
| 10609 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10610 | "os.terminal_size", |
| 10611 | TerminalSize_docstring, |
| 10612 | TerminalSize_fields, |
| 10613 | 2, |
| 10614 | }; |
| 10615 | |
| 10616 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10617 | PyDoc_STRVAR(termsize__doc__, |
| 10618 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10619 | "\n" \ |
| 10620 | "The optional argument fd (default standard output) specifies\n" \ |
| 10621 | "which file descriptor should be queried.\n" \ |
| 10622 | "\n" \ |
| 10623 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10624 | "is thrown.\n" \ |
| 10625 | "\n" \ |
| 10626 | "This function will only be defined if an implementation is\n" \ |
| 10627 | "available for this system.\n" \ |
| 10628 | "\n" \ |
| 10629 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10630 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10631 | |
| 10632 | static PyObject* |
| 10633 | get_terminal_size(PyObject *self, PyObject *args) |
| 10634 | { |
| 10635 | int columns, lines; |
| 10636 | PyObject *termsize; |
| 10637 | |
| 10638 | int fd = fileno(stdout); |
| 10639 | /* Under some conditions stdout may not be connected and |
| 10640 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10641 | * GUI apps don't have valid standard streams by default. |
| 10642 | * |
| 10643 | * If this happens, and the optional fd argument is not present, |
| 10644 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10645 | */ |
| 10646 | |
| 10647 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10648 | return NULL; |
| 10649 | |
| 10650 | #ifdef TERMSIZE_USE_IOCTL |
| 10651 | { |
| 10652 | struct winsize w; |
| 10653 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10654 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10655 | columns = w.ws_col; |
| 10656 | lines = w.ws_row; |
| 10657 | } |
| 10658 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10659 | |
| 10660 | #ifdef TERMSIZE_USE_CONIO |
| 10661 | { |
| 10662 | DWORD nhandle; |
| 10663 | HANDLE handle; |
| 10664 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10665 | switch (fd) { |
| 10666 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10667 | break; |
| 10668 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10669 | break; |
| 10670 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10671 | break; |
| 10672 | default: |
| 10673 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10674 | } |
| 10675 | handle = GetStdHandle(nhandle); |
| 10676 | if (handle == NULL) |
| 10677 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10678 | if (handle == INVALID_HANDLE_VALUE) |
| 10679 | return PyErr_SetFromWindowsErr(0); |
| 10680 | |
| 10681 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10682 | return PyErr_SetFromWindowsErr(0); |
| 10683 | |
| 10684 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10685 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10686 | } |
| 10687 | #endif /* TERMSIZE_USE_CONIO */ |
| 10688 | |
| 10689 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10690 | if (termsize == NULL) |
| 10691 | return NULL; |
| 10692 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10693 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10694 | if (PyErr_Occurred()) { |
| 10695 | Py_DECREF(termsize); |
| 10696 | return NULL; |
| 10697 | } |
| 10698 | return termsize; |
| 10699 | } |
| 10700 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10701 | |
| 10702 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10703 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10704 | {"access", (PyCFunction)posix_access, |
| 10705 | METH_VARARGS | METH_KEYWORDS, |
| 10706 | posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10707 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10708 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10709 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10710 | {"chdir", (PyCFunction)posix_chdir, |
| 10711 | METH_VARARGS | METH_KEYWORDS, |
| 10712 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10713 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10714 | {"chflags", (PyCFunction)posix_chflags, |
| 10715 | METH_VARARGS | METH_KEYWORDS, |
| 10716 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10717 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10718 | {"chmod", (PyCFunction)posix_chmod, |
| 10719 | METH_VARARGS | METH_KEYWORDS, |
| 10720 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10721 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10722 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10723 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10724 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10725 | {"chown", (PyCFunction)posix_chown, |
| 10726 | METH_VARARGS | METH_KEYWORDS, |
| 10727 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10728 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10729 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10730 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10731 | #endif /* HAVE_LCHMOD */ |
| 10732 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10733 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10734 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10735 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10736 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10737 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10738 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10739 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10740 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10741 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10742 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10743 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10744 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10745 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10746 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10747 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10748 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10749 | METH_NOARGS, posix_getcwd__doc__}, |
| 10750 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10751 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10752 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10753 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 10754 | {"link", (PyCFunction)posix_link, |
| 10755 | METH_VARARGS | METH_KEYWORDS, |
| 10756 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10757 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10758 | {"listdir", (PyCFunction)posix_listdir, |
| 10759 | METH_VARARGS | METH_KEYWORDS, |
| 10760 | posix_listdir__doc__}, |
| 10761 | {"lstat", (PyCFunction)posix_lstat, |
| 10762 | METH_VARARGS | METH_KEYWORDS, |
| 10763 | posix_lstat__doc__}, |
| 10764 | {"mkdir", (PyCFunction)posix_mkdir, |
| 10765 | METH_VARARGS | METH_KEYWORDS, |
| 10766 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10767 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10768 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10769 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10770 | #ifdef HAVE_GETPRIORITY |
| 10771 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10772 | #endif /* HAVE_GETPRIORITY */ |
| 10773 | #ifdef HAVE_SETPRIORITY |
| 10774 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10775 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10776 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10777 | {"readlink", (PyCFunction)posix_readlink, |
| 10778 | METH_VARARGS | METH_KEYWORDS, |
| 10779 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10780 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10781 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10782 | {"readlink", (PyCFunction)win_readlink, |
| 10783 | METH_VARARGS | METH_KEYWORDS, |
| 10784 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10785 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10786 | {"rename", (PyCFunction)posix_rename, |
| 10787 | METH_VARARGS | METH_KEYWORDS, |
| 10788 | posix_rename__doc__}, |
| 10789 | {"replace", (PyCFunction)posix_replace, |
| 10790 | METH_VARARGS | METH_KEYWORDS, |
| 10791 | posix_replace__doc__}, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 10792 | {"rmdir", (PyCFunction)posix_rmdir, |
| 10793 | METH_VARARGS | METH_KEYWORDS, |
| 10794 | posix_rmdir__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10795 | {"stat", (PyCFunction)posix_stat, |
| 10796 | METH_VARARGS | METH_KEYWORDS, |
| 10797 | posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10798 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10799 | #if defined(HAVE_SYMLINK) |
| 10800 | {"symlink", (PyCFunction)posix_symlink, |
| 10801 | METH_VARARGS | METH_KEYWORDS, |
| 10802 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10803 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10804 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10805 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10806 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10807 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10808 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10809 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10810 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10811 | {"unlink", (PyCFunction)posix_unlink, |
| 10812 | METH_VARARGS | METH_KEYWORDS, |
| 10813 | posix_unlink__doc__}, |
| 10814 | {"remove", (PyCFunction)posix_unlink, |
| 10815 | METH_VARARGS | METH_KEYWORDS, |
| 10816 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10817 | {"utime", (PyCFunction)posix_utime, |
| 10818 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10819 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10820 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10821 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10822 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10823 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10824 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10825 | {"execve", (PyCFunction)posix_execve, |
| 10826 | METH_VARARGS | METH_KEYWORDS, |
| 10827 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10828 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10829 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10830 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10831 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10832 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10833 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10834 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10835 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10836 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10837 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10838 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10839 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10840 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10841 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10842 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10843 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10844 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10845 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10846 | {"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] | 10847 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10848 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10849 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10850 | #endif |
| 10851 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10852 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10853 | #endif |
| 10854 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10855 | {"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] | 10856 | #endif |
| 10857 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10858 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10859 | #endif |
| 10860 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10861 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10862 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10863 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10864 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10865 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10866 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10867 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10868 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10869 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10870 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10871 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10872 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10873 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10874 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10875 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10876 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 10877 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10878 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10879 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 10880 | #endif /* HAVE_GETEUID */ |
| 10881 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10882 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10883 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10884 | #ifdef HAVE_GETGROUPLIST |
| 10885 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10886 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10887 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10888 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10889 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10890 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10891 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10892 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10893 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10894 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10895 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10896 | #endif /* HAVE_GETPPID */ |
| 10897 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10898 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10899 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10900 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10901 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10902 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10903 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10904 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10905 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10906 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10907 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10908 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10909 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10910 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10911 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10912 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10913 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10914 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10915 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10916 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10917 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10918 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10919 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10920 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10921 | #endif /* HAVE_SETEUID */ |
| 10922 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10923 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10924 | #endif /* HAVE_SETEGID */ |
| 10925 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10926 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10927 | #endif /* HAVE_SETREUID */ |
| 10928 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10929 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10930 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10931 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10932 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10933 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10934 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10935 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10936 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10937 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10938 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10939 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10940 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10941 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10942 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10943 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10944 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10945 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10946 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10947 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10948 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10949 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10950 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10951 | #endif /* HAVE_WAIT3 */ |
| 10952 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10953 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10954 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10955 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10956 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10957 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10958 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10959 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10960 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10961 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10962 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10963 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10964 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10965 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10966 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10967 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10968 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10969 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10970 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10971 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10972 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10973 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10974 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10975 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10976 | {"open", (PyCFunction)posix_open,\ |
| 10977 | METH_VARARGS | METH_KEYWORDS, |
| 10978 | posix_open__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10979 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10980 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10981 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10982 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10983 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10984 | #ifdef HAVE_LOCKF |
| 10985 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10986 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10987 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10988 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10989 | #ifdef HAVE_READV |
| 10990 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10991 | #endif |
| 10992 | #ifdef HAVE_PREAD |
| 10993 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10994 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10995 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10996 | #ifdef HAVE_WRITEV |
| 10997 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10998 | #endif |
| 10999 | #ifdef HAVE_PWRITE |
| 11000 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 11001 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11002 | #ifdef HAVE_SENDFILE |
| 11003 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 11004 | posix_sendfile__doc__}, |
| 11005 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11006 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11007 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11008 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11009 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11010 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11011 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 11012 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11013 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11014 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11015 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 11016 | METH_VARARGS | METH_KEYWORDS, |
| 11017 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11018 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 11019 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11020 | {"mknod", (PyCFunction)posix_mknod, |
| 11021 | METH_VARARGS | METH_KEYWORDS, |
| 11022 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 11023 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11024 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11025 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 11026 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 11027 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11028 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11029 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11030 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11031 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11032 | #ifdef HAVE_TRUNCATE |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11033 | {"truncate", (PyCFunction)posix_truncate, |
| 11034 | METH_VARARGS | METH_KEYWORDS, |
| 11035 | posix_truncate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11036 | #endif |
| 11037 | #ifdef HAVE_POSIX_FALLOCATE |
| 11038 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 11039 | #endif |
| 11040 | #ifdef HAVE_POSIX_FADVISE |
| 11041 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 11042 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11043 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11044 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11045 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11046 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11047 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11048 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11049 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11050 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11051 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11052 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11053 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11054 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11055 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11056 | #ifdef HAVE_SYNC |
| 11057 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 11058 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11059 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11060 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11061 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11062 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11063 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11064 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11065 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11066 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11068 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11069 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11071 | #endif /* WIFSTOPPED */ |
| 11072 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11074 | #endif /* WIFSIGNALED */ |
| 11075 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11076 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11077 | #endif /* WIFEXITED */ |
| 11078 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11079 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11080 | #endif /* WEXITSTATUS */ |
| 11081 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11082 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11083 | #endif /* WTERMSIG */ |
| 11084 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11086 | #endif /* WSTOPSIG */ |
| 11087 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11088 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11090 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11091 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11092 | {"statvfs", (PyCFunction)posix_statvfs, |
| 11093 | METH_VARARGS | METH_KEYWORDS, |
| 11094 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11095 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11096 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11098 | #endif |
| 11099 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11100 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11101 | #endif |
| 11102 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11104 | #endif |
| 11105 | #ifdef HAVE_PATHCONF |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11106 | {"pathconf", (PyCFunction)posix_pathconf, |
| 11107 | METH_VARARGS | METH_KEYWORDS, |
| 11108 | posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11109 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11111 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11112 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11113 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 11114 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 11115 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11116 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 11117 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11118 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11119 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11120 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 11121 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11122 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11123 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11124 | #endif |
| 11125 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11126 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11127 | #endif |
| 11128 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11129 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11130 | #endif |
| 11131 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11132 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11133 | #endif |
| 11134 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11135 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11136 | {"setxattr", (PyCFunction)posix_setxattr, |
| 11137 | METH_VARARGS | METH_KEYWORDS, |
| 11138 | posix_setxattr__doc__}, |
| 11139 | {"getxattr", (PyCFunction)posix_getxattr, |
| 11140 | METH_VARARGS | METH_KEYWORDS, |
| 11141 | posix_getxattr__doc__}, |
| 11142 | {"removexattr", (PyCFunction)posix_removexattr, |
| 11143 | METH_VARARGS | METH_KEYWORDS, |
| 11144 | posix_removexattr__doc__}, |
| 11145 | {"listxattr", (PyCFunction)posix_listxattr, |
| 11146 | METH_VARARGS | METH_KEYWORDS, |
| 11147 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11148 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11149 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 11150 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 11151 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11152 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11153 | }; |
| 11154 | |
| 11155 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11156 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11157 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11158 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11159 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11160 | } |
| 11161 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11162 | #if defined(PYOS_OS2) |
| 11163 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11164 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11165 | { |
| 11166 | APIRET rc; |
| 11167 | ULONG values[QSV_MAX+1]; |
| 11168 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 11169 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11170 | |
| 11171 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 11172 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11173 | Py_END_ALLOW_THREADS |
| 11174 | |
| 11175 | if (rc != NO_ERROR) { |
| 11176 | os2_error(rc); |
| 11177 | return -1; |
| 11178 | } |
| 11179 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11180 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 11181 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 11182 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 11183 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 11184 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 11185 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 11186 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11187 | |
| 11188 | switch (values[QSV_VERSION_MINOR]) { |
| 11189 | case 0: ver = "2.00"; break; |
| 11190 | case 10: ver = "2.10"; break; |
| 11191 | case 11: ver = "2.11"; break; |
| 11192 | case 30: ver = "3.00"; break; |
| 11193 | case 40: ver = "4.00"; break; |
| 11194 | case 50: ver = "5.00"; break; |
| 11195 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11196 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11197 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11198 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11199 | ver = &tmp[0]; |
| 11200 | } |
| 11201 | |
| 11202 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11203 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11204 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11205 | |
| 11206 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 11207 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 11208 | tmp[1] = ':'; |
| 11209 | tmp[2] = '\0'; |
| 11210 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11211 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11212 | } |
| 11213 | #endif |
| 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 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11217 | enable_symlink() |
| 11218 | { |
| 11219 | HANDLE tok; |
| 11220 | TOKEN_PRIVILEGES tok_priv; |
| 11221 | LUID luid; |
| 11222 | int meth_idx = 0; |
| 11223 | |
| 11224 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11225 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11226 | |
| 11227 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11228 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11229 | |
| 11230 | tok_priv.PrivilegeCount = 1; |
| 11231 | tok_priv.Privileges[0].Luid = luid; |
| 11232 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11233 | |
| 11234 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11235 | sizeof(TOKEN_PRIVILEGES), |
| 11236 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11237 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11238 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11239 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11240 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11241 | } |
| 11242 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11243 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11244 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11245 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11246 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11247 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11248 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11249 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11250 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11251 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11252 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11253 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11254 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11255 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11256 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11257 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11258 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11259 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11260 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11261 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11262 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11263 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11264 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11265 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11266 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11267 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11268 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11269 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11270 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11271 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11272 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11273 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11274 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11275 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11276 | #endif |
| 11277 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11278 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11279 | #endif |
| 11280 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11281 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11282 | #endif |
| 11283 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11284 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11285 | #endif |
| 11286 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11287 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11288 | #endif |
| 11289 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11290 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11291 | #endif |
| 11292 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11293 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11294 | #endif |
| 11295 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11296 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11297 | #endif |
| 11298 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11299 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11300 | #endif |
| 11301 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11302 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11303 | #endif |
| 11304 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11305 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11306 | #endif |
| 11307 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11308 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11309 | #endif |
| 11310 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11311 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11312 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11313 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11315 | #endif |
| 11316 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11317 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11318 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11319 | #ifdef O_XATTR |
| 11320 | if (ins(d, "O_XATTR", (long)O_XATTR)) return -1; |
| 11321 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11322 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11323 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11324 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11325 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11326 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11327 | #endif |
| 11328 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11329 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11330 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11331 | #ifdef O_EXEC |
| 11332 | if (ins(d, "O_EXEC", (long)O_EXEC)) return -1; |
| 11333 | #endif |
| 11334 | #ifdef O_SEARCH |
| 11335 | if (ins(d, "O_SEARCH", (long)O_SEARCH)) return -1; |
| 11336 | #endif |
| 11337 | #ifdef O_TTY_INIT |
| 11338 | if (ins(d, "O_TTY_INIT", (long)O_TTY_INIT)) return -1; |
| 11339 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11340 | #ifdef PRIO_PROCESS |
| 11341 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11342 | #endif |
| 11343 | #ifdef PRIO_PGRP |
| 11344 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11345 | #endif |
| 11346 | #ifdef PRIO_USER |
| 11347 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11348 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11349 | #ifdef O_CLOEXEC |
| 11350 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11351 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11352 | #ifdef O_ACCMODE |
| 11353 | if (ins(d, "O_ACCMODE", (long)O_ACCMODE)) return -1; |
| 11354 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11355 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11356 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11357 | #ifdef SEEK_HOLE |
| 11358 | if (ins(d, "SEEK_HOLE", (long)SEEK_HOLE)) return -1; |
| 11359 | #endif |
| 11360 | #ifdef SEEK_DATA |
| 11361 | if (ins(d, "SEEK_DATA", (long)SEEK_DATA)) return -1; |
| 11362 | #endif |
| 11363 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11364 | /* MS Windows */ |
| 11365 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11366 | /* Don't inherit in child processes. */ |
| 11367 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11368 | #endif |
| 11369 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11370 | /* Optimize for short life (keep in memory). */ |
| 11371 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11372 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11373 | #endif |
| 11374 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11375 | /* Automatically delete when last handle is closed. */ |
| 11376 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11377 | #endif |
| 11378 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11379 | /* Optimize for random access. */ |
| 11380 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11381 | #endif |
| 11382 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11383 | /* Optimize for sequential access. */ |
| 11384 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11385 | #endif |
| 11386 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11387 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11388 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11389 | /* Send a SIGIO signal whenever input or output |
| 11390 | becomes available on file descriptor */ |
| 11391 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11392 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11393 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11394 | /* Direct disk access. */ |
| 11395 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11396 | #endif |
| 11397 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11398 | /* Must be a directory. */ |
| 11399 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11400 | #endif |
| 11401 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11402 | /* Do not follow links. */ |
| 11403 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11404 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11405 | #ifdef O_NOLINKS |
| 11406 | /* Fails if link count of the named file is greater than 1 */ |
| 11407 | if (ins(d, "O_NOLINKS", (long)O_NOLINKS)) return -1; |
| 11408 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11409 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11410 | /* Do not update the access time. */ |
| 11411 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11412 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11413 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11414 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11415 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11416 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11417 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11418 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11419 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11420 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11421 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11422 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11423 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11424 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11425 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11426 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11427 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11428 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11429 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11430 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11431 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11432 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11433 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11434 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11435 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11436 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11437 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11438 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11439 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11440 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11441 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11442 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11443 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11444 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11445 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11446 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11447 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11448 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11449 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11450 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11451 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11452 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11453 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11454 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11455 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11456 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11457 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11458 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11459 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11460 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11461 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11462 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11463 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11464 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11465 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11466 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11467 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11468 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11469 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11470 | #endif /* ST_RDONLY */ |
| 11471 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11472 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11473 | #endif /* ST_NOSUID */ |
| 11474 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11475 | /* FreeBSD sendfile() constants */ |
| 11476 | #ifdef SF_NODISKIO |
| 11477 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11478 | #endif |
| 11479 | #ifdef SF_MNOWAIT |
| 11480 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11481 | #endif |
| 11482 | #ifdef SF_SYNC |
| 11483 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11484 | #endif |
| 11485 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11486 | /* constants for posix_fadvise */ |
| 11487 | #ifdef POSIX_FADV_NORMAL |
| 11488 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11489 | #endif |
| 11490 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11491 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11492 | #endif |
| 11493 | #ifdef POSIX_FADV_RANDOM |
| 11494 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11495 | #endif |
| 11496 | #ifdef POSIX_FADV_NOREUSE |
| 11497 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11498 | #endif |
| 11499 | #ifdef POSIX_FADV_WILLNEED |
| 11500 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11501 | #endif |
| 11502 | #ifdef POSIX_FADV_DONTNEED |
| 11503 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11504 | #endif |
| 11505 | |
| 11506 | /* constants for waitid */ |
| 11507 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11508 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11509 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11510 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11511 | #endif |
| 11512 | #ifdef WEXITED |
| 11513 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11514 | #endif |
| 11515 | #ifdef WNOWAIT |
| 11516 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11517 | #endif |
| 11518 | #ifdef WSTOPPED |
| 11519 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11520 | #endif |
| 11521 | #ifdef CLD_EXITED |
| 11522 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11523 | #endif |
| 11524 | #ifdef CLD_DUMPED |
| 11525 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11526 | #endif |
| 11527 | #ifdef CLD_TRAPPED |
| 11528 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11529 | #endif |
| 11530 | #ifdef CLD_CONTINUED |
| 11531 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11532 | #endif |
| 11533 | |
| 11534 | /* constants for lockf */ |
| 11535 | #ifdef F_LOCK |
| 11536 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11537 | #endif |
| 11538 | #ifdef F_TLOCK |
| 11539 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11540 | #endif |
| 11541 | #ifdef F_ULOCK |
| 11542 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11543 | #endif |
| 11544 | #ifdef F_TEST |
| 11545 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11546 | #endif |
| 11547 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11548 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11549 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11550 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11551 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11552 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11553 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11554 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11555 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11556 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11557 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11558 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11559 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11560 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11561 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11562 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11563 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11564 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11565 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11566 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11567 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11568 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11569 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11570 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11571 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11572 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11573 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11574 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11575 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11576 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11577 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11578 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11579 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11580 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11581 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11582 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11583 | #ifdef SCHED_SPORADIC |
| 11584 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11585 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11586 | #ifdef SCHED_BATCH |
| 11587 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11588 | #endif |
| 11589 | #ifdef SCHED_IDLE |
| 11590 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11591 | #endif |
| 11592 | #ifdef SCHED_RESET_ON_FORK |
| 11593 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11594 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11595 | #ifdef SCHED_SYS |
| 11596 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11597 | #endif |
| 11598 | #ifdef SCHED_IA |
| 11599 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11600 | #endif |
| 11601 | #ifdef SCHED_FSS |
| 11602 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11603 | #endif |
| 11604 | #ifdef SCHED_FX |
| 11605 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11606 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11607 | #endif |
| 11608 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11609 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11610 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11611 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11612 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11613 | #endif |
| 11614 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11615 | #ifdef RTLD_LAZY |
| 11616 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11617 | #endif |
| 11618 | #ifdef RTLD_NOW |
| 11619 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11620 | #endif |
| 11621 | #ifdef RTLD_GLOBAL |
| 11622 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11623 | #endif |
| 11624 | #ifdef RTLD_LOCAL |
| 11625 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11626 | #endif |
| 11627 | #ifdef RTLD_NODELETE |
| 11628 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11629 | #endif |
| 11630 | #ifdef RTLD_NOLOAD |
| 11631 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11632 | #endif |
| 11633 | #ifdef RTLD_DEEPBIND |
| 11634 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11635 | #endif |
| 11636 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11637 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11638 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11639 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11640 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11641 | } |
| 11642 | |
| 11643 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11644 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11645 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11646 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11647 | |
| 11648 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11649 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11650 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11651 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11652 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11653 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11654 | #define MODNAME "posix" |
| 11655 | #endif |
| 11656 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11657 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11658 | PyModuleDef_HEAD_INIT, |
| 11659 | MODNAME, |
| 11660 | posix__doc__, |
| 11661 | -1, |
| 11662 | posix_methods, |
| 11663 | NULL, |
| 11664 | NULL, |
| 11665 | NULL, |
| 11666 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11667 | }; |
| 11668 | |
| 11669 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11670 | static char *have_functions[] = { |
| 11671 | |
| 11672 | #ifdef HAVE_FACCESSAT |
| 11673 | "HAVE_FACCESSAT", |
| 11674 | #endif |
| 11675 | |
| 11676 | #ifdef HAVE_FCHDIR |
| 11677 | "HAVE_FCHDIR", |
| 11678 | #endif |
| 11679 | |
| 11680 | #ifdef HAVE_FCHMOD |
| 11681 | "HAVE_FCHMOD", |
| 11682 | #endif |
| 11683 | |
| 11684 | #ifdef HAVE_FCHMODAT |
| 11685 | "HAVE_FCHMODAT", |
| 11686 | #endif |
| 11687 | |
| 11688 | #ifdef HAVE_FCHOWN |
| 11689 | "HAVE_FCHOWN", |
| 11690 | #endif |
| 11691 | |
| 11692 | #ifdef HAVE_FEXECVE |
| 11693 | "HAVE_FEXECVE", |
| 11694 | #endif |
| 11695 | |
| 11696 | #ifdef HAVE_FDOPENDIR |
| 11697 | "HAVE_FDOPENDIR", |
| 11698 | #endif |
| 11699 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11700 | #ifdef HAVE_FPATHCONF |
| 11701 | "HAVE_FPATHCONF", |
| 11702 | #endif |
| 11703 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11704 | #ifdef HAVE_FSTATAT |
| 11705 | "HAVE_FSTATAT", |
| 11706 | #endif |
| 11707 | |
| 11708 | #ifdef HAVE_FSTATVFS |
| 11709 | "HAVE_FSTATVFS", |
| 11710 | #endif |
| 11711 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11712 | #ifdef HAVE_FTRUNCATE |
| 11713 | "HAVE_FTRUNCATE", |
| 11714 | #endif |
| 11715 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11716 | #ifdef HAVE_FUTIMENS |
| 11717 | "HAVE_FUTIMENS", |
| 11718 | #endif |
| 11719 | |
| 11720 | #ifdef HAVE_FUTIMES |
| 11721 | "HAVE_FUTIMES", |
| 11722 | #endif |
| 11723 | |
| 11724 | #ifdef HAVE_FUTIMESAT |
| 11725 | "HAVE_FUTIMESAT", |
| 11726 | #endif |
| 11727 | |
| 11728 | #ifdef HAVE_LINKAT |
| 11729 | "HAVE_LINKAT", |
| 11730 | #endif |
| 11731 | |
| 11732 | #ifdef HAVE_LCHFLAGS |
| 11733 | "HAVE_LCHFLAGS", |
| 11734 | #endif |
| 11735 | |
| 11736 | #ifdef HAVE_LCHMOD |
| 11737 | "HAVE_LCHMOD", |
| 11738 | #endif |
| 11739 | |
| 11740 | #ifdef HAVE_LCHOWN |
| 11741 | "HAVE_LCHOWN", |
| 11742 | #endif |
| 11743 | |
| 11744 | #ifdef HAVE_LSTAT |
| 11745 | "HAVE_LSTAT", |
| 11746 | #endif |
| 11747 | |
| 11748 | #ifdef HAVE_LUTIMES |
| 11749 | "HAVE_LUTIMES", |
| 11750 | #endif |
| 11751 | |
| 11752 | #ifdef HAVE_MKDIRAT |
| 11753 | "HAVE_MKDIRAT", |
| 11754 | #endif |
| 11755 | |
| 11756 | #ifdef HAVE_MKFIFOAT |
| 11757 | "HAVE_MKFIFOAT", |
| 11758 | #endif |
| 11759 | |
| 11760 | #ifdef HAVE_MKNODAT |
| 11761 | "HAVE_MKNODAT", |
| 11762 | #endif |
| 11763 | |
| 11764 | #ifdef HAVE_OPENAT |
| 11765 | "HAVE_OPENAT", |
| 11766 | #endif |
| 11767 | |
| 11768 | #ifdef HAVE_READLINKAT |
| 11769 | "HAVE_READLINKAT", |
| 11770 | #endif |
| 11771 | |
| 11772 | #ifdef HAVE_RENAMEAT |
| 11773 | "HAVE_RENAMEAT", |
| 11774 | #endif |
| 11775 | |
| 11776 | #ifdef HAVE_SYMLINKAT |
| 11777 | "HAVE_SYMLINKAT", |
| 11778 | #endif |
| 11779 | |
| 11780 | #ifdef HAVE_UNLINKAT |
| 11781 | "HAVE_UNLINKAT", |
| 11782 | #endif |
| 11783 | |
| 11784 | #ifdef HAVE_UTIMENSAT |
| 11785 | "HAVE_UTIMENSAT", |
| 11786 | #endif |
| 11787 | |
| 11788 | #ifdef MS_WINDOWS |
| 11789 | "MS_WINDOWS", |
| 11790 | #endif |
| 11791 | |
| 11792 | NULL |
| 11793 | }; |
| 11794 | |
| 11795 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11796 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11797 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11798 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11799 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11800 | PyObject *list; |
| 11801 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11802 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11803 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11804 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11805 | #endif |
| 11806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11807 | m = PyModule_Create(&posixmodule); |
| 11808 | if (m == NULL) |
| 11809 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11810 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11811 | /* Initialize environ dictionary */ |
| 11812 | v = convertenviron(); |
| 11813 | Py_XINCREF(v); |
| 11814 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11815 | return NULL; |
| 11816 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11817 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11818 | if (all_ins(m)) |
| 11819 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11821 | if (setup_confname_tables(m)) |
| 11822 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11823 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11824 | Py_INCREF(PyExc_OSError); |
| 11825 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11826 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11827 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11828 | if (posix_putenv_garbage == NULL) |
| 11829 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11830 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11831 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11832 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11833 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11834 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11835 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11836 | #endif |
| 11837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11838 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11839 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11840 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11841 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11842 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11843 | structseq_new = StatResultType.tp_new; |
| 11844 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11845 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11846 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11847 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11848 | #ifdef NEED_TICKS_PER_SECOND |
| 11849 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11850 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11851 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11852 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11853 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11854 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11855 | # endif |
| 11856 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11857 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11858 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11859 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11860 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11861 | SchedParamType.tp_new = sched_param_new; |
| 11862 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11863 | |
| 11864 | /* initialize TerminalSize_info */ |
| 11865 | PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11866 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11867 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11868 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11869 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11870 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11871 | Py_INCREF((PyObject*) &StatResultType); |
| 11872 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11873 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11874 | PyModule_AddObject(m, "statvfs_result", |
| 11875 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11876 | |
| 11877 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11878 | Py_INCREF(&SchedParamType); |
| 11879 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11880 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11881 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 11882 | times_result_desc.name = MODNAME ".times_result"; |
| 11883 | PyStructSequence_InitType(&TimesResultType, ×_result_desc); |
| 11884 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 11885 | |
| 11886 | uname_result_desc.name = MODNAME ".uname_result"; |
| 11887 | PyStructSequence_InitType(&UnameResultType, &uname_result_desc); |
| 11888 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 11889 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11890 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11891 | /* |
| 11892 | * Step 2 of weak-linking support on Mac OS X. |
| 11893 | * |
| 11894 | * The code below removes functions that are not available on the |
| 11895 | * currently active platform. |
| 11896 | * |
| 11897 | * 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] | 11898 | * 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] | 11899 | * OSX 10.4. |
| 11900 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11901 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11902 | if (fstatvfs == NULL) { |
| 11903 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11904 | return NULL; |
| 11905 | } |
| 11906 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11907 | #endif /* HAVE_FSTATVFS */ |
| 11908 | |
| 11909 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11910 | if (statvfs == NULL) { |
| 11911 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11912 | return NULL; |
| 11913 | } |
| 11914 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11915 | #endif /* HAVE_STATVFS */ |
| 11916 | |
| 11917 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11918 | if (lchown == NULL) { |
| 11919 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11920 | return NULL; |
| 11921 | } |
| 11922 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11923 | #endif /* HAVE_LCHOWN */ |
| 11924 | |
| 11925 | |
| 11926 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11927 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 11928 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11929 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 11930 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 11931 | billion = PyLong_FromLong(1000000000); |
| 11932 | if (!billion) |
| 11933 | return NULL; |
| 11934 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11935 | /* suppress "function not used" warnings */ |
| 11936 | { |
| 11937 | int ignored; |
| 11938 | fd_specified("", -1); |
| 11939 | follow_symlinks_specified("", 1); |
| 11940 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 11941 | dir_fd_converter(Py_None, &ignored); |
| 11942 | dir_fd_unavailable(Py_None, &ignored); |
| 11943 | } |
| 11944 | |
| 11945 | /* |
| 11946 | * provide list of locally available functions |
| 11947 | * so os.py can populate support_* lists |
| 11948 | */ |
| 11949 | list = PyList_New(0); |
| 11950 | if (!list) |
| 11951 | return NULL; |
| 11952 | for (trace = have_functions; *trace; trace++) { |
| 11953 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 11954 | if (!unicode) |
| 11955 | return NULL; |
| 11956 | if (PyList_Append(list, unicode)) |
| 11957 | return NULL; |
| 11958 | Py_DECREF(unicode); |
| 11959 | } |
| 11960 | PyModule_AddObject(m, "_have_functions", list); |
| 11961 | |
| 11962 | initialized = 1; |
| 11963 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11964 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11965 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11966 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11967 | |
| 11968 | #ifdef __cplusplus |
| 11969 | } |
| 11970 | #endif |