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 */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 958 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 959 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 960 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 961 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 962 | */ |
| 963 | #include <crt_externs.h> |
| 964 | static char **environ; |
| 965 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 966 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 967 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 968 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 969 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 970 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 971 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 972 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 973 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 974 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 975 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 976 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 977 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 978 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 979 | APIRET rc; |
| 980 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 981 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 982 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 983 | d = PyDict_New(); |
| 984 | if (d == NULL) |
| 985 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 986 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 987 | if (environ == NULL) |
| 988 | environ = *_NSGetEnviron(); |
| 989 | #endif |
| 990 | #ifdef MS_WINDOWS |
| 991 | /* _wenviron must be initialized in this way if the program is started |
| 992 | through main() instead of wmain(). */ |
| 993 | _wgetenv(L""); |
| 994 | if (_wenviron == NULL) |
| 995 | return d; |
| 996 | /* This part ignores errors */ |
| 997 | for (e = _wenviron; *e != NULL; e++) { |
| 998 | PyObject *k; |
| 999 | PyObject *v; |
| 1000 | wchar_t *p = wcschr(*e, L'='); |
| 1001 | if (p == NULL) |
| 1002 | continue; |
| 1003 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1004 | if (k == NULL) { |
| 1005 | PyErr_Clear(); |
| 1006 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1007 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1008 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1009 | if (v == NULL) { |
| 1010 | PyErr_Clear(); |
| 1011 | Py_DECREF(k); |
| 1012 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1013 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1014 | if (PyDict_GetItem(d, k) == NULL) { |
| 1015 | if (PyDict_SetItem(d, k, v) != 0) |
| 1016 | PyErr_Clear(); |
| 1017 | } |
| 1018 | Py_DECREF(k); |
| 1019 | Py_DECREF(v); |
| 1020 | } |
| 1021 | #else |
| 1022 | if (environ == NULL) |
| 1023 | return d; |
| 1024 | /* This part ignores errors */ |
| 1025 | for (e = environ; *e != NULL; e++) { |
| 1026 | PyObject *k; |
| 1027 | PyObject *v; |
| 1028 | char *p = strchr(*e, '='); |
| 1029 | if (p == NULL) |
| 1030 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1031 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1032 | if (k == NULL) { |
| 1033 | PyErr_Clear(); |
| 1034 | continue; |
| 1035 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1036 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1037 | if (v == NULL) { |
| 1038 | PyErr_Clear(); |
| 1039 | Py_DECREF(k); |
| 1040 | continue; |
| 1041 | } |
| 1042 | if (PyDict_GetItem(d, k) == NULL) { |
| 1043 | if (PyDict_SetItem(d, k, v) != 0) |
| 1044 | PyErr_Clear(); |
| 1045 | } |
| 1046 | Py_DECREF(k); |
| 1047 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1048 | } |
| 1049 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1050 | #if defined(PYOS_OS2) |
| 1051 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 1052 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 1053 | PyObject *v = PyBytes_FromString(buffer); |
| 1054 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 1055 | Py_DECREF(v); |
| 1056 | } |
| 1057 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 1058 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 1059 | PyObject *v = PyBytes_FromString(buffer); |
| 1060 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 1061 | Py_DECREF(v); |
| 1062 | } |
| 1063 | #endif |
| 1064 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1067 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1068 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1069 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1070 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1071 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1072 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1073 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1074 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1075 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1076 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1077 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1080 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1081 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1082 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1083 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 1084 | PyObject *name_str, *rc; |
| 1085 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 1086 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1087 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 1088 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 1089 | name_str); |
| 1090 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1091 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1094 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1095 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1096 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1097 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1098 | /* XXX We should pass the function name along in the future. |
| 1099 | (winreg.c also wants to pass the function name.) |
| 1100 | This would however require an additional param to the |
| 1101 | Windows error object, which is non-trivial. |
| 1102 | */ |
| 1103 | errno = GetLastError(); |
| 1104 | if (filename) |
| 1105 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1106 | else |
| 1107 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1108 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1109 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1110 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1111 | win32_error_unicode(char* function, wchar_t* filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1112 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1113 | /* XXX - see win32_error for comments on 'function' */ |
| 1114 | errno = GetLastError(); |
| 1115 | if (filename) |
| 1116 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 1117 | else |
| 1118 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1121 | static PyObject * |
| 1122 | win32_error_object(char* function, PyObject* filename) |
| 1123 | { |
| 1124 | /* XXX - see win32_error for comments on 'function' */ |
| 1125 | errno = GetLastError(); |
| 1126 | if (filename) |
| 1127 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1128 | PyExc_WindowsError, |
| 1129 | errno, |
| 1130 | filename); |
| 1131 | else |
| 1132 | return PyErr_SetFromWindowsErr(errno); |
| 1133 | } |
| 1134 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1135 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1136 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1137 | /* |
| 1138 | * Some functions return Win32 errors, others only ever use posix_error |
| 1139 | * (this is for backwards compatibility with exceptions) |
| 1140 | */ |
| 1141 | static PyObject * |
| 1142 | path_posix_error(char *function_name, path_t *path) |
| 1143 | { |
| 1144 | if (path->narrow) |
| 1145 | return posix_error_with_filename(path->narrow); |
| 1146 | return posix_error(); |
| 1147 | } |
| 1148 | |
| 1149 | static PyObject * |
| 1150 | path_error(char *function_name, path_t *path) |
| 1151 | { |
| 1152 | #ifdef MS_WINDOWS |
| 1153 | if (path->narrow) |
| 1154 | return win32_error(function_name, path->narrow); |
| 1155 | if (path->wide) |
| 1156 | return win32_error_unicode(function_name, path->wide); |
| 1157 | return win32_error(function_name, NULL); |
| 1158 | #else |
| 1159 | return path_posix_error(function_name, path); |
| 1160 | #endif |
| 1161 | } |
| 1162 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1163 | #if defined(PYOS_OS2) |
| 1164 | /********************************************************************** |
| 1165 | * Helper Function to Trim and Format OS/2 Messages |
| 1166 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1167 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1168 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 1169 | { |
| 1170 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 1171 | |
| 1172 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 1173 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 1174 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 1175 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1176 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 1177 | } |
| 1178 | |
| 1179 | /* Add Optional Reason Text */ |
| 1180 | if (reason) { |
| 1181 | strcat(msgbuf, " : "); |
| 1182 | strcat(msgbuf, reason); |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | /********************************************************************** |
| 1187 | * Decode an OS/2 Operating System Error Code |
| 1188 | * |
| 1189 | * A convenience function to lookup an OS/2 error code and return a |
| 1190 | * text message we can use to raise a Python exception. |
| 1191 | * |
| 1192 | * Notes: |
| 1193 | * The messages for errors returned from the OS/2 kernel reside in |
| 1194 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 1195 | * |
| 1196 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1197 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1198 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 1199 | { |
| 1200 | APIRET rc; |
| 1201 | ULONG msglen; |
| 1202 | |
| 1203 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 1204 | Py_BEGIN_ALLOW_THREADS |
| 1205 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 1206 | errorcode, "oso001.msg", &msglen); |
| 1207 | Py_END_ALLOW_THREADS |
| 1208 | |
| 1209 | if (rc == NO_ERROR) |
| 1210 | os2_formatmsg(msgbuf, msglen, reason); |
| 1211 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 1212 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1213 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1214 | |
| 1215 | return msgbuf; |
| 1216 | } |
| 1217 | |
| 1218 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 1219 | errors are not in a global variable e.g. 'errno' nor are |
| 1220 | they congruent with posix error numbers. */ |
| 1221 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1222 | static PyObject * |
| 1223 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1224 | { |
| 1225 | char text[1024]; |
| 1226 | PyObject *v; |
| 1227 | |
| 1228 | os2_strerror(text, sizeof(text), code, ""); |
| 1229 | |
| 1230 | v = Py_BuildValue("(is)", code, text); |
| 1231 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 1232 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1233 | Py_DECREF(v); |
| 1234 | } |
| 1235 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 1236 | } |
| 1237 | |
| 1238 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1239 | |
| 1240 | /* POSIX generic methods */ |
| 1241 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1242 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1243 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1244 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1245 | int fd; |
| 1246 | int res; |
| 1247 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1248 | if (fd < 0) |
| 1249 | return NULL; |
| 1250 | if (!_PyVerify_fd(fd)) |
| 1251 | return posix_error(); |
| 1252 | Py_BEGIN_ALLOW_THREADS |
| 1253 | res = (*func)(fd); |
| 1254 | Py_END_ALLOW_THREADS |
| 1255 | if (res < 0) |
| 1256 | return posix_error(); |
| 1257 | Py_INCREF(Py_None); |
| 1258 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1259 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1260 | |
| 1261 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1262 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1263 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1264 | PyObject *opath1 = NULL; |
| 1265 | char *path1; |
| 1266 | int res; |
| 1267 | if (!PyArg_ParseTuple(args, format, |
| 1268 | PyUnicode_FSConverter, &opath1)) |
| 1269 | return NULL; |
| 1270 | path1 = PyBytes_AsString(opath1); |
| 1271 | Py_BEGIN_ALLOW_THREADS |
| 1272 | res = (*func)(path1); |
| 1273 | Py_END_ALLOW_THREADS |
| 1274 | if (res < 0) |
| 1275 | return posix_error_with_allocated_filename(opath1); |
| 1276 | Py_DECREF(opath1); |
| 1277 | Py_INCREF(Py_None); |
| 1278 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1281 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1282 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1283 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1284 | win32_1str(PyObject* args, char* func, |
| 1285 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 1286 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1287 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1288 | PyObject *uni; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1289 | const char *ansi; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1290 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1291 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1292 | if (PyArg_ParseTuple(args, wformat, &uni)) |
| 1293 | { |
| 1294 | wchar_t *wstr = PyUnicode_AsUnicode(uni); |
| 1295 | if (wstr == NULL) |
| 1296 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1297 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1298 | result = funcW(wstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1299 | Py_END_ALLOW_THREADS |
| 1300 | if (!result) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1301 | return win32_error_object(func, uni); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1302 | Py_INCREF(Py_None); |
| 1303 | return Py_None; |
| 1304 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1305 | PyErr_Clear(); |
| 1306 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1307 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 1308 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1309 | if (win32_warn_bytes_api()) |
| 1310 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1311 | Py_BEGIN_ALLOW_THREADS |
| 1312 | result = funcA(ansi); |
| 1313 | Py_END_ALLOW_THREADS |
| 1314 | if (!result) |
| 1315 | return win32_error(func, ansi); |
| 1316 | Py_INCREF(Py_None); |
| 1317 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1318 | |
| 1319 | } |
| 1320 | |
| 1321 | /* This is a reimplementation of the C library's chdir function, |
| 1322 | but one that produces Win32 errors instead of DOS error codes. |
| 1323 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1324 | it also needs to set "magic" environment variables indicating |
| 1325 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1326 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1327 | win32_chdir(LPCSTR path) |
| 1328 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1329 | char new_path[MAX_PATH+1]; |
| 1330 | int result; |
| 1331 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1332 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1333 | if(!SetCurrentDirectoryA(path)) |
| 1334 | return FALSE; |
| 1335 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 1336 | if (!result) |
| 1337 | return FALSE; |
| 1338 | /* In the ANSI API, there should not be any paths longer |
| 1339 | than MAX_PATH. */ |
| 1340 | assert(result <= MAX_PATH+1); |
| 1341 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1342 | strncmp(new_path, "//", 2) == 0) |
| 1343 | /* UNC path, nothing to do. */ |
| 1344 | return TRUE; |
| 1345 | env[1] = new_path[0]; |
| 1346 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | /* The Unicode version differs from the ANSI version |
| 1350 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1351 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1352 | win32_wchdir(LPCWSTR path) |
| 1353 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1354 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 1355 | int result; |
| 1356 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1357 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1358 | if(!SetCurrentDirectoryW(path)) |
| 1359 | return FALSE; |
| 1360 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 1361 | if (!result) |
| 1362 | return FALSE; |
| 1363 | if (result > MAX_PATH+1) { |
| 1364 | new_path = malloc(result * sizeof(wchar_t)); |
| 1365 | if (!new_path) { |
| 1366 | SetLastError(ERROR_OUTOFMEMORY); |
| 1367 | return FALSE; |
| 1368 | } |
| 1369 | result = GetCurrentDirectoryW(result, new_path); |
| 1370 | if (!result) { |
| 1371 | free(new_path); |
| 1372 | return FALSE; |
| 1373 | } |
| 1374 | } |
| 1375 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1376 | wcsncmp(new_path, L"//", 2) == 0) |
| 1377 | /* UNC path, nothing to do. */ |
| 1378 | return TRUE; |
| 1379 | env[1] = new_path[0]; |
| 1380 | result = SetEnvironmentVariableW(env, new_path); |
| 1381 | if (new_path != _new_path) |
| 1382 | free(new_path); |
| 1383 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1384 | } |
| 1385 | #endif |
| 1386 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1387 | #ifdef MS_WINDOWS |
| 1388 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1389 | - time stamps are restricted to second resolution |
| 1390 | - file modification times suffer from forth-and-back conversions between |
| 1391 | UTC and local time |
| 1392 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1393 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1394 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1395 | |
| 1396 | struct win32_stat{ |
| 1397 | int st_dev; |
| 1398 | __int64 st_ino; |
| 1399 | unsigned short st_mode; |
| 1400 | int st_nlink; |
| 1401 | int st_uid; |
| 1402 | int st_gid; |
| 1403 | int st_rdev; |
| 1404 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1405 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1406 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1407 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1408 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1409 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1410 | int st_ctime_nsec; |
| 1411 | }; |
| 1412 | |
| 1413 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1414 | |
| 1415 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1416 | 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] | 1417 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1418 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1419 | /* Cannot simply cast and dereference in_ptr, |
| 1420 | since it might not be aligned properly */ |
| 1421 | __int64 in; |
| 1422 | memcpy(&in, in_ptr, sizeof(in)); |
| 1423 | *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] | 1424 | *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] | 1425 | } |
| 1426 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1427 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1428 | 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] | 1429 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1430 | /* XXX endianness */ |
| 1431 | __int64 out; |
| 1432 | out = time_in + secs_between_epochs; |
| 1433 | out = out * 10000000 + nsec_in / 100; |
| 1434 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1437 | /* Below, we *know* that ugo+r is 0444 */ |
| 1438 | #if _S_IREAD != 0400 |
| 1439 | #error Unsupported C library |
| 1440 | #endif |
| 1441 | static int |
| 1442 | attributes_to_mode(DWORD attr) |
| 1443 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1444 | int m = 0; |
| 1445 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1446 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1447 | else |
| 1448 | m |= _S_IFREG; |
| 1449 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1450 | m |= 0444; |
| 1451 | else |
| 1452 | m |= 0666; |
| 1453 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1457 | 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] | 1458 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1459 | memset(result, 0, sizeof(*result)); |
| 1460 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1461 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1462 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1463 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1464 | 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] | 1465 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1466 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1467 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1468 | /* first clear the S_IFMT bits */ |
| 1469 | result->st_mode ^= (result->st_mode & 0170000); |
| 1470 | /* now set the bits that make this a symlink */ |
| 1471 | result->st_mode |= 0120000; |
| 1472 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1473 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1474 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1477 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1478 | 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] | 1479 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1480 | HANDLE hFindFile; |
| 1481 | WIN32_FIND_DATAA FileData; |
| 1482 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1483 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1484 | return FALSE; |
| 1485 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1486 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1487 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1488 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1489 | info->ftCreationTime = FileData.ftCreationTime; |
| 1490 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1491 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1492 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1493 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1494 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1495 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1496 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1497 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1501 | 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] | 1502 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1503 | HANDLE hFindFile; |
| 1504 | WIN32_FIND_DATAW FileData; |
| 1505 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1506 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1507 | return FALSE; |
| 1508 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1509 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1510 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1511 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1512 | info->ftCreationTime = FileData.ftCreationTime; |
| 1513 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1514 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1515 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1516 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1517 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1518 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1519 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1520 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1523 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1524 | static int has_GetFinalPathNameByHandle = 0; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1525 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1526 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1527 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1528 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1529 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1530 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1531 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1532 | DWORD); |
| 1533 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1534 | /* only recheck */ |
| 1535 | if (!has_GetFinalPathNameByHandle) |
| 1536 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1537 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1538 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1539 | "GetFinalPathNameByHandleA"); |
| 1540 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1541 | "GetFinalPathNameByHandleW"); |
| 1542 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1543 | Py_GetFinalPathNameByHandleW; |
| 1544 | } |
| 1545 | return has_GetFinalPathNameByHandle; |
| 1546 | } |
| 1547 | |
| 1548 | static BOOL |
| 1549 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1550 | { |
| 1551 | int buf_size, result_length; |
| 1552 | wchar_t *buf; |
| 1553 | |
| 1554 | /* We have a good handle to the target, use it to determine |
| 1555 | the target path name (then we'll call lstat on it). */ |
| 1556 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1557 | VOLUME_NAME_DOS); |
| 1558 | if(!buf_size) |
| 1559 | return FALSE; |
| 1560 | |
| 1561 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1562 | if (!buf) { |
| 1563 | SetLastError(ERROR_OUTOFMEMORY); |
| 1564 | return FALSE; |
| 1565 | } |
| 1566 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1567 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1568 | buf, buf_size, VOLUME_NAME_DOS); |
| 1569 | |
| 1570 | if(!result_length) { |
| 1571 | free(buf); |
| 1572 | return FALSE; |
| 1573 | } |
| 1574 | |
| 1575 | if(!CloseHandle(hdl)) { |
| 1576 | free(buf); |
| 1577 | return FALSE; |
| 1578 | } |
| 1579 | |
| 1580 | buf[result_length] = 0; |
| 1581 | |
| 1582 | *target_path = buf; |
| 1583 | return TRUE; |
| 1584 | } |
| 1585 | |
| 1586 | static int |
| 1587 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1588 | BOOL traverse); |
| 1589 | static int |
| 1590 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1591 | BOOL traverse) |
| 1592 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1593 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1594 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1595 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1596 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1597 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1598 | const char *dot; |
| 1599 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1600 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1601 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1602 | traverse reparse point. */ |
| 1603 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1606 | hFile = CreateFileA( |
| 1607 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1608 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1609 | 0, /* share mode */ |
| 1610 | NULL, /* security attributes */ |
| 1611 | OPEN_EXISTING, |
| 1612 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1613 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1614 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1615 | the symlink path agin and not the actual final path. */ |
| 1616 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1617 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1618 | NULL); |
| 1619 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1620 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1621 | /* Either the target doesn't exist, or we don't have access to |
| 1622 | get a handle to it. If the former, we need to return an error. |
| 1623 | If the latter, we can use attributes_from_dir. */ |
| 1624 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1625 | return -1; |
| 1626 | /* Could not get attributes on open file. Fall back to |
| 1627 | reading the directory. */ |
| 1628 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1629 | /* Very strange. This should not fail now */ |
| 1630 | return -1; |
| 1631 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1632 | if (traverse) { |
| 1633 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1634 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1635 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1636 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1637 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1638 | } else { |
| 1639 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1640 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1641 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1642 | } |
| 1643 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1644 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1645 | return -1; |
| 1646 | |
| 1647 | /* Close the outer open file handle now that we're about to |
| 1648 | reopen it with different flags. */ |
| 1649 | if (!CloseHandle(hFile)) |
| 1650 | return -1; |
| 1651 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1652 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1653 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1654 | the file without the reparse handling flag set. */ |
| 1655 | hFile2 = CreateFileA( |
| 1656 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1657 | NULL, OPEN_EXISTING, |
| 1658 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1659 | NULL); |
| 1660 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1661 | return -1; |
| 1662 | |
| 1663 | if (!get_target_path(hFile2, &target_path)) |
| 1664 | return -1; |
| 1665 | |
| 1666 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1667 | free(target_path); |
| 1668 | return code; |
| 1669 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1670 | } else |
| 1671 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1672 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1673 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1674 | |
| 1675 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1676 | dot = strrchr(path, '.'); |
| 1677 | if (dot) { |
| 1678 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1679 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1680 | result->st_mode |= 0111; |
| 1681 | } |
| 1682 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1686 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1687 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1688 | { |
| 1689 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1690 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1691 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1692 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1693 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1694 | const wchar_t *dot; |
| 1695 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1696 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1697 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1698 | traverse reparse point. */ |
| 1699 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1702 | hFile = CreateFileW( |
| 1703 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1704 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1705 | 0, /* share mode */ |
| 1706 | NULL, /* security attributes */ |
| 1707 | OPEN_EXISTING, |
| 1708 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1709 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1710 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1711 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1712 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1713 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1714 | NULL); |
| 1715 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1716 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1717 | /* Either the target doesn't exist, or we don't have access to |
| 1718 | get a handle to it. If the former, we need to return an error. |
| 1719 | If the latter, we can use attributes_from_dir. */ |
| 1720 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1721 | return -1; |
| 1722 | /* Could not get attributes on open file. Fall back to |
| 1723 | reading the directory. */ |
| 1724 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1725 | /* Very strange. This should not fail now */ |
| 1726 | return -1; |
| 1727 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1728 | if (traverse) { |
| 1729 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1730 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1731 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1732 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1733 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1734 | } else { |
| 1735 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1736 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1737 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1738 | } |
| 1739 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1740 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1741 | return -1; |
| 1742 | |
| 1743 | /* Close the outer open file handle now that we're about to |
| 1744 | reopen it with different flags. */ |
| 1745 | if (!CloseHandle(hFile)) |
| 1746 | return -1; |
| 1747 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1748 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1749 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1750 | the file without the reparse handling flag set. */ |
| 1751 | hFile2 = CreateFileW( |
| 1752 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1753 | NULL, OPEN_EXISTING, |
| 1754 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1755 | NULL); |
| 1756 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1757 | return -1; |
| 1758 | |
| 1759 | if (!get_target_path(hFile2, &target_path)) |
| 1760 | return -1; |
| 1761 | |
| 1762 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1763 | free(target_path); |
| 1764 | return code; |
| 1765 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1766 | } else |
| 1767 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1768 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1769 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1770 | |
| 1771 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1772 | dot = wcsrchr(path, '.'); |
| 1773 | if (dot) { |
| 1774 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1775 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1776 | result->st_mode |= 0111; |
| 1777 | } |
| 1778 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1782 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1783 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1784 | /* Protocol violation: we explicitly clear errno, instead of |
| 1785 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1786 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1787 | errno = 0; |
| 1788 | return code; |
| 1789 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1790 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1791 | static int |
| 1792 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1793 | { |
| 1794 | /* Protocol violation: we explicitly clear errno, instead of |
| 1795 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1796 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1797 | errno = 0; |
| 1798 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1799 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1800 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1801 | |
| 1802 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1803 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1804 | default does not traverse symlinks and instead returns attributes for |
| 1805 | the symlink. |
| 1806 | |
| 1807 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1808 | win32_stat will first explicitly resolve the symlink target and then will |
| 1809 | call win32_lstat on that result. |
| 1810 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1811 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1812 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1813 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1814 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1815 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1816 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1819 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1820 | 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] | 1821 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1822 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | static int |
| 1826 | win32_stat(const char* path, struct win32_stat *result) |
| 1827 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1828 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1831 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1832 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1833 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1834 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | static int |
| 1838 | win32_fstat(int file_number, struct win32_stat *result) |
| 1839 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1840 | BY_HANDLE_FILE_INFORMATION info; |
| 1841 | HANDLE h; |
| 1842 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1843 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 1844 | if (!_PyVerify_fd(file_number)) |
| 1845 | h = INVALID_HANDLE_VALUE; |
| 1846 | else |
| 1847 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1848 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1849 | /* Protocol violation: we explicitly clear errno, instead of |
| 1850 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1851 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1852 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1853 | if (h == INVALID_HANDLE_VALUE) { |
| 1854 | /* This is really a C library error (invalid file handle). |
| 1855 | We set the Win32 error to the closes one matching. */ |
| 1856 | SetLastError(ERROR_INVALID_HANDLE); |
| 1857 | return -1; |
| 1858 | } |
| 1859 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1860 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1861 | type = GetFileType(h); |
| 1862 | if (type == FILE_TYPE_UNKNOWN) { |
| 1863 | DWORD error = GetLastError(); |
| 1864 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1865 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1866 | } |
| 1867 | /* else: valid but unknown file */ |
| 1868 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1869 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1870 | if (type != FILE_TYPE_DISK) { |
| 1871 | if (type == FILE_TYPE_CHAR) |
| 1872 | result->st_mode = _S_IFCHR; |
| 1873 | else if (type == FILE_TYPE_PIPE) |
| 1874 | result->st_mode = _S_IFIFO; |
| 1875 | return 0; |
| 1876 | } |
| 1877 | |
| 1878 | if (!GetFileInformationByHandle(h, &info)) { |
| 1879 | return -1; |
| 1880 | } |
| 1881 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1882 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1883 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1884 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1885 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | #endif /* MS_WINDOWS */ |
| 1889 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1890 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1891 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1892 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1893 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1894 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1895 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1896 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1897 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1898 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1899 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1900 | |
| 1901 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1902 | {"st_mode", "protection bits"}, |
| 1903 | {"st_ino", "inode"}, |
| 1904 | {"st_dev", "device"}, |
| 1905 | {"st_nlink", "number of hard links"}, |
| 1906 | {"st_uid", "user ID of owner"}, |
| 1907 | {"st_gid", "group ID of owner"}, |
| 1908 | {"st_size", "total size, in bytes"}, |
| 1909 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1910 | {NULL, "integer time of last access"}, |
| 1911 | {NULL, "integer time of last modification"}, |
| 1912 | {NULL, "integer time of last change"}, |
| 1913 | {"st_atime", "time of last access"}, |
| 1914 | {"st_mtime", "time of last modification"}, |
| 1915 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1916 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1917 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1918 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1919 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1920 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1921 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1922 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1923 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1924 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1925 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1926 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1927 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1928 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1929 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1930 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1931 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1932 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1933 | #endif |
| 1934 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1935 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1936 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1937 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1938 | }; |
| 1939 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1940 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1941 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1942 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1943 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1944 | #endif |
| 1945 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1946 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1947 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1948 | #else |
| 1949 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1950 | #endif |
| 1951 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1952 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1953 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1954 | #else |
| 1955 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1956 | #endif |
| 1957 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1958 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1959 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1960 | #else |
| 1961 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1962 | #endif |
| 1963 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1964 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1965 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1966 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1967 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1968 | #endif |
| 1969 | |
| 1970 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1971 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1972 | #else |
| 1973 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1974 | #endif |
| 1975 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1976 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1977 | "stat_result", /* name */ |
| 1978 | stat_result__doc__, /* doc */ |
| 1979 | stat_result_fields, |
| 1980 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1981 | }; |
| 1982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1983 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1984 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1985 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1986 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1987 | 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] | 1988 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1989 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1990 | |
| 1991 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1992 | {"f_bsize", }, |
| 1993 | {"f_frsize", }, |
| 1994 | {"f_blocks", }, |
| 1995 | {"f_bfree", }, |
| 1996 | {"f_bavail", }, |
| 1997 | {"f_files", }, |
| 1998 | {"f_ffree", }, |
| 1999 | {"f_favail", }, |
| 2000 | {"f_flag", }, |
| 2001 | {"f_namemax",}, |
| 2002 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2003 | }; |
| 2004 | |
| 2005 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2006 | "statvfs_result", /* name */ |
| 2007 | statvfs_result__doc__, /* doc */ |
| 2008 | statvfs_result_fields, |
| 2009 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2010 | }; |
| 2011 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2012 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2013 | PyDoc_STRVAR(waitid_result__doc__, |
| 2014 | "waitid_result: Result from waitid.\n\n\ |
| 2015 | This object may be accessed either as a tuple of\n\ |
| 2016 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2017 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2018 | \n\ |
| 2019 | See os.waitid for more information."); |
| 2020 | |
| 2021 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2022 | {"si_pid", }, |
| 2023 | {"si_uid", }, |
| 2024 | {"si_signo", }, |
| 2025 | {"si_status", }, |
| 2026 | {"si_code", }, |
| 2027 | {0} |
| 2028 | }; |
| 2029 | |
| 2030 | static PyStructSequence_Desc waitid_result_desc = { |
| 2031 | "waitid_result", /* name */ |
| 2032 | waitid_result__doc__, /* doc */ |
| 2033 | waitid_result_fields, |
| 2034 | 5 |
| 2035 | }; |
| 2036 | static PyTypeObject WaitidResultType; |
| 2037 | #endif |
| 2038 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2039 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2040 | static PyTypeObject StatResultType; |
| 2041 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2042 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 2043 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2044 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2045 | static newfunc structseq_new; |
| 2046 | |
| 2047 | static PyObject * |
| 2048 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2049 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2050 | PyStructSequence *result; |
| 2051 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2052 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2053 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2054 | if (!result) |
| 2055 | return NULL; |
| 2056 | /* If we have been initialized from a tuple, |
| 2057 | st_?time might be set to None. Initialize it |
| 2058 | from the int slots. */ |
| 2059 | for (i = 7; i <= 9; i++) { |
| 2060 | if (result->ob_item[i+3] == Py_None) { |
| 2061 | Py_DECREF(Py_None); |
| 2062 | Py_INCREF(result->ob_item[i]); |
| 2063 | result->ob_item[i+3] = result->ob_item[i]; |
| 2064 | } |
| 2065 | } |
| 2066 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | |
| 2070 | |
| 2071 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 2072 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2073 | |
| 2074 | PyDoc_STRVAR(stat_float_times__doc__, |
| 2075 | "stat_float_times([newval]) -> oldval\n\n\ |
| 2076 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 2077 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 2078 | future calls return ints. \n\ |
| 2079 | If newval is omitted, return the current setting.\n"); |
| 2080 | |
| 2081 | static PyObject* |
| 2082 | stat_float_times(PyObject* self, PyObject *args) |
| 2083 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2084 | int newval = -1; |
| 2085 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 2086 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 2087 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2088 | "stat_float_times() is deprecated", |
| 2089 | 1)) |
| 2090 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2091 | if (newval == -1) |
| 2092 | /* Return old value */ |
| 2093 | return PyBool_FromLong(_stat_float_times); |
| 2094 | _stat_float_times = newval; |
| 2095 | Py_INCREF(Py_None); |
| 2096 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2097 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2098 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2099 | static PyObject *billion = NULL; |
| 2100 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2101 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2102 | 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] | 2103 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2104 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2105 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2106 | PyObject *s_in_ns = NULL; |
| 2107 | PyObject *ns_total = NULL; |
| 2108 | PyObject *float_s = NULL; |
| 2109 | |
| 2110 | if (!(s && ns_fractional)) |
| 2111 | goto exit; |
| 2112 | |
| 2113 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2114 | if (!s_in_ns) |
| 2115 | goto exit; |
| 2116 | |
| 2117 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2118 | if (!ns_total) |
| 2119 | goto exit; |
| 2120 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2121 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2122 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2123 | if (!float_s) |
| 2124 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2125 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2126 | else { |
| 2127 | float_s = s; |
| 2128 | Py_INCREF(float_s); |
| 2129 | } |
| 2130 | |
| 2131 | PyStructSequence_SET_ITEM(v, index, s); |
| 2132 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2133 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2134 | s = NULL; |
| 2135 | float_s = NULL; |
| 2136 | ns_total = NULL; |
| 2137 | exit: |
| 2138 | Py_XDECREF(s); |
| 2139 | Py_XDECREF(ns_fractional); |
| 2140 | Py_XDECREF(s_in_ns); |
| 2141 | Py_XDECREF(ns_total); |
| 2142 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2145 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2146 | (used by posix_stat() and posix_fstat()) */ |
| 2147 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2148 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2149 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2150 | unsigned long ansec, mnsec, cnsec; |
| 2151 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2152 | if (v == NULL) |
| 2153 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2154 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2155 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2156 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2157 | PyStructSequence_SET_ITEM(v, 1, |
| 2158 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2159 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2160 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2161 | #endif |
| 2162 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2163 | PyStructSequence_SET_ITEM(v, 2, |
| 2164 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2165 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2166 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2167 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2168 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 2169 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 2170 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2171 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2172 | PyStructSequence_SET_ITEM(v, 6, |
| 2173 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2174 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2175 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2176 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2177 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2178 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2179 | ansec = st->st_atim.tv_nsec; |
| 2180 | mnsec = st->st_mtim.tv_nsec; |
| 2181 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2182 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2183 | ansec = st->st_atimespec.tv_nsec; |
| 2184 | mnsec = st->st_mtimespec.tv_nsec; |
| 2185 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2186 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2187 | ansec = st->st_atime_nsec; |
| 2188 | mnsec = st->st_mtime_nsec; |
| 2189 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2190 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2191 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2192 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2193 | fill_time(v, 7, st->st_atime, ansec); |
| 2194 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2195 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2196 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2197 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2198 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2199 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2200 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2201 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2202 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2203 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2204 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2205 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2206 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2207 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2208 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2209 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2210 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2211 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2212 | #endif |
| 2213 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2214 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2215 | PyObject *val; |
| 2216 | unsigned long bsec,bnsec; |
| 2217 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2218 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2219 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2220 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2221 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2222 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2223 | if (_stat_float_times) { |
| 2224 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2225 | } else { |
| 2226 | val = PyLong_FromLong((long)bsec); |
| 2227 | } |
| 2228 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2229 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2230 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2231 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2232 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2233 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2234 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2235 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2236 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2237 | if (PyErr_Occurred()) { |
| 2238 | Py_DECREF(v); |
| 2239 | return NULL; |
| 2240 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2241 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2242 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2245 | /* POSIX methods */ |
| 2246 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2247 | |
| 2248 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2249 | posix_do_stat(char *function_name, path_t *path, |
| 2250 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2251 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2252 | STRUCT_STAT st; |
| 2253 | int result; |
| 2254 | |
| 2255 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2256 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2257 | return NULL; |
| 2258 | #endif |
| 2259 | |
| 2260 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2261 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2262 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2263 | return NULL; |
| 2264 | |
| 2265 | Py_BEGIN_ALLOW_THREADS |
| 2266 | if (path->fd != -1) |
| 2267 | result = FSTAT(path->fd, &st); |
| 2268 | else |
| 2269 | #ifdef MS_WINDOWS |
| 2270 | if (path->wide) { |
| 2271 | if (follow_symlinks) |
| 2272 | result = win32_stat_w(path->wide, &st); |
| 2273 | else |
| 2274 | result = win32_lstat_w(path->wide, &st); |
| 2275 | } |
| 2276 | else |
| 2277 | #endif |
| 2278 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2279 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2280 | result = LSTAT(path->narrow, &st); |
| 2281 | else |
| 2282 | #endif |
| 2283 | #ifdef HAVE_FSTATAT |
| 2284 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2285 | result = fstatat(dir_fd, path->narrow, &st, |
| 2286 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2287 | else |
| 2288 | #endif |
| 2289 | result = STAT(path->narrow, &st); |
| 2290 | Py_END_ALLOW_THREADS |
| 2291 | |
| 2292 | if (result != 0) |
| 2293 | return path_error("stat", path); |
| 2294 | |
| 2295 | return _pystat_fromstructstat(&st); |
| 2296 | } |
| 2297 | |
| 2298 | PyDoc_STRVAR(posix_stat__doc__, |
| 2299 | "stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result\n\n\ |
| 2300 | Perform a stat system call on the given path.\n\ |
| 2301 | \n\ |
| 2302 | path may be specified as either a string or as an open file descriptor.\n\ |
| 2303 | \n\ |
| 2304 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2305 | and path should be relative; path will then be relative to that directory.\n\ |
| 2306 | dir_fd may not be supported on your platform; if it is unavailable, using\n\ |
| 2307 | it will raise a NotImplementedError.\n\ |
| 2308 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2309 | link, stat will examine the symbolic link itself instead of the file the\n\ |
| 2310 | link points to.\n\ |
| 2311 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2312 | an open file descriptor."); |
| 2313 | |
| 2314 | static PyObject * |
| 2315 | posix_stat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2316 | { |
| 2317 | static char *keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2318 | path_t path; |
| 2319 | int dir_fd = DEFAULT_DIR_FD; |
| 2320 | int follow_symlinks = 1; |
| 2321 | PyObject *return_value; |
| 2322 | |
| 2323 | memset(&path, 0, sizeof(path)); |
| 2324 | path.allow_fd = 1; |
| 2325 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords, |
| 2326 | path_converter, &path, |
| 2327 | #ifdef HAVE_FSTATAT |
| 2328 | dir_fd_converter, &dir_fd, |
| 2329 | #else |
| 2330 | dir_fd_unavailable, &dir_fd, |
| 2331 | #endif |
| 2332 | &follow_symlinks)) |
| 2333 | return NULL; |
| 2334 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2335 | path_cleanup(&path); |
| 2336 | return return_value; |
| 2337 | } |
| 2338 | |
| 2339 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2340 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2341 | Like stat(), but do not follow symbolic links.\n\ |
| 2342 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2343 | |
| 2344 | static PyObject * |
| 2345 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2346 | { |
| 2347 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2348 | path_t path; |
| 2349 | int dir_fd = DEFAULT_DIR_FD; |
| 2350 | int follow_symlinks = 0; |
| 2351 | PyObject *return_value; |
| 2352 | |
| 2353 | memset(&path, 0, sizeof(path)); |
| 2354 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2355 | path_converter, &path, |
| 2356 | #ifdef HAVE_FSTATAT |
| 2357 | dir_fd_converter, &dir_fd |
| 2358 | #else |
| 2359 | dir_fd_unavailable, &dir_fd |
| 2360 | #endif |
| 2361 | )) |
| 2362 | return NULL; |
| 2363 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2364 | path_cleanup(&path); |
| 2365 | return return_value; |
| 2366 | } |
| 2367 | |
| 2368 | PyDoc_STRVAR(posix_access__doc__, |
| 2369 | "access(path, mode, *, dir_fd=None, effective_ids=False,\ |
| 2370 | follow_symlinks=True)\n\n\ |
| 2371 | Use the real uid/gid to test for access to a path. Returns True if granted,\n\ |
| 2372 | False otherwise.\n\ |
| 2373 | \n\ |
| 2374 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2375 | and path should be relative; path will then be relative to that directory.\n\ |
| 2376 | If effective_ids is True, access will use the effective uid/gid instead of\n\ |
| 2377 | the real uid/gid.\n\ |
| 2378 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2379 | link, access will examine the symbolic link itself instead of the file the\n\ |
| 2380 | link points to.\n\ |
| 2381 | dir_fd, effective_ids, and follow_symlinks may not be implemented\n\ |
| 2382 | on your platform. If they are unavailable, using them will raise a\n\ |
| 2383 | NotImplementedError.\n\ |
| 2384 | \n\ |
| 2385 | Note that most operations will use the effective uid/gid, therefore this\n\ |
| 2386 | routine can be used in a suid/sgid environment to test if the invoking user\n\ |
| 2387 | has the specified access to the path.\n\ |
| 2388 | The mode argument can be F_OK to test existence, or the inclusive-OR\n\ |
| 2389 | of R_OK, W_OK, and X_OK."); |
| 2390 | |
| 2391 | static PyObject * |
| 2392 | posix_access(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2393 | { |
| 2394 | static char *keywords[] = {"path", "mode", "dir_fd", "effective_ids", |
| 2395 | "follow_symlinks", NULL}; |
| 2396 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2397 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2398 | int dir_fd = DEFAULT_DIR_FD; |
| 2399 | int effective_ids = 0; |
| 2400 | int follow_symlinks = 1; |
| 2401 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2402 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2403 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2404 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2405 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2406 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2407 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2408 | |
| 2409 | memset(&path, 0, sizeof(path)); |
| 2410 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", keywords, |
| 2411 | path_converter, &path, &mode, |
| 2412 | #ifdef HAVE_FACCESSAT |
| 2413 | dir_fd_converter, &dir_fd, |
| 2414 | #else |
| 2415 | dir_fd_unavailable, &dir_fd, |
| 2416 | #endif |
| 2417 | &effective_ids, &follow_symlinks)) |
| 2418 | return NULL; |
| 2419 | |
| 2420 | #ifndef HAVE_FACCESSAT |
| 2421 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2422 | goto exit; |
| 2423 | |
| 2424 | if (effective_ids) { |
| 2425 | argument_unavailable_error("access", "effective_ids"); |
| 2426 | goto exit; |
| 2427 | } |
| 2428 | #endif |
| 2429 | |
| 2430 | #ifdef MS_WINDOWS |
| 2431 | Py_BEGIN_ALLOW_THREADS |
| 2432 | if (path.wide != NULL) |
| 2433 | attr = GetFileAttributesW(path.wide); |
| 2434 | else |
| 2435 | attr = GetFileAttributesA(path.narrow); |
| 2436 | Py_END_ALLOW_THREADS |
| 2437 | |
| 2438 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2439 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2440 | * * we didn't get a -1, and |
| 2441 | * * write access wasn't requested, |
| 2442 | * * or the file isn't read-only, |
| 2443 | * * or it's a directory. |
| 2444 | * (Directories cannot be read-only on Windows.) |
| 2445 | */ |
| 2446 | return_value = PyBool_FromLong( |
| 2447 | (attr != 0xFFFFFFFF) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2448 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2449 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2450 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2451 | #else |
| 2452 | |
| 2453 | Py_BEGIN_ALLOW_THREADS |
| 2454 | #ifdef HAVE_FACCESSAT |
| 2455 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2456 | effective_ids || |
| 2457 | !follow_symlinks) { |
| 2458 | int flags = 0; |
| 2459 | if (!follow_symlinks) |
| 2460 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2461 | if (effective_ids) |
| 2462 | flags |= AT_EACCESS; |
| 2463 | result = faccessat(dir_fd, path.narrow, mode, flags); |
| 2464 | } |
| 2465 | else |
| 2466 | #endif |
| 2467 | result = access(path.narrow, mode); |
| 2468 | Py_END_ALLOW_THREADS |
| 2469 | return_value = PyBool_FromLong(!result); |
| 2470 | #endif |
| 2471 | |
| 2472 | #ifndef HAVE_FACCESSAT |
| 2473 | exit: |
| 2474 | #endif |
| 2475 | path_cleanup(&path); |
| 2476 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2479 | #ifndef F_OK |
| 2480 | #define F_OK 0 |
| 2481 | #endif |
| 2482 | #ifndef R_OK |
| 2483 | #define R_OK 4 |
| 2484 | #endif |
| 2485 | #ifndef W_OK |
| 2486 | #define W_OK 2 |
| 2487 | #endif |
| 2488 | #ifndef X_OK |
| 2489 | #define X_OK 1 |
| 2490 | #endif |
| 2491 | |
| 2492 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2493 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2494 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2495 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2496 | |
| 2497 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2498 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2499 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2500 | int id; |
| 2501 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2502 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2503 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 2504 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2505 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2506 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2507 | /* file descriptor 0 only, the default input device (stdin) */ |
| 2508 | if (id == 0) { |
| 2509 | ret = ttyname(); |
| 2510 | } |
| 2511 | else { |
| 2512 | ret = NULL; |
| 2513 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2514 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2515 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2516 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2517 | if (ret == NULL) |
| 2518 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2519 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2520 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2521 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2522 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2523 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2524 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2525 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2526 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2527 | |
| 2528 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2529 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2530 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2531 | char *ret; |
| 2532 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2533 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2534 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2535 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2536 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2537 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2538 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2539 | if (ret == NULL) |
| 2540 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2541 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2542 | } |
| 2543 | #endif |
| 2544 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2545 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2546 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2547 | Change the current working directory to the specified path.\n\ |
| 2548 | \n\ |
| 2549 | path may always be specified as a string.\n\ |
| 2550 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2551 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2552 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2553 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2554 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2555 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2556 | path_t path; |
| 2557 | int result; |
| 2558 | PyObject *return_value = NULL; |
| 2559 | static char *keywords[] = {"path", NULL}; |
| 2560 | |
| 2561 | memset(&path, 0, sizeof(path)); |
| 2562 | #ifdef HAVE_FCHDIR |
| 2563 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2564 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2565 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2566 | path_converter, &path |
| 2567 | )) |
| 2568 | return NULL; |
| 2569 | |
| 2570 | Py_BEGIN_ALLOW_THREADS |
| 2571 | #ifdef MS_WINDOWS |
| 2572 | if (path.wide) |
| 2573 | result = win32_wchdir(path.wide); |
| 2574 | else |
| 2575 | result = win32_chdir(path.narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2576 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2577 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2578 | result = _chdir2(path.narrow); |
| 2579 | #else |
| 2580 | #ifdef HAVE_FCHDIR |
| 2581 | if (path.fd != -1) |
| 2582 | result = fchdir(path.fd); |
| 2583 | else |
| 2584 | #endif |
| 2585 | result = chdir(path.narrow); |
| 2586 | #endif |
| 2587 | Py_END_ALLOW_THREADS |
| 2588 | |
| 2589 | if (result) { |
| 2590 | return_value = path_error("chdir", &path); |
| 2591 | goto exit; |
| 2592 | } |
| 2593 | |
| 2594 | return_value = Py_None; |
| 2595 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2596 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2597 | exit: |
| 2598 | path_cleanup(&path); |
| 2599 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2602 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2603 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2604 | "fchdir(fd)\n\n\ |
| 2605 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2606 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2607 | |
| 2608 | static PyObject * |
| 2609 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2610 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2611 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2612 | } |
| 2613 | #endif /* HAVE_FCHDIR */ |
| 2614 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2616 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2617 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2618 | Change the access permissions of a file.\n\ |
| 2619 | \n\ |
| 2620 | path may always be specified as a string.\n\ |
| 2621 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2622 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2623 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2624 | and path should be relative; path will then be relative to that directory.\n\ |
| 2625 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2626 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2627 | link points to.\n\ |
| 2628 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2629 | an open file descriptor.\n\ |
| 2630 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2631 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2632 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2633 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2634 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2635 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2636 | path_t path; |
| 2637 | int mode; |
| 2638 | int dir_fd = DEFAULT_DIR_FD; |
| 2639 | int follow_symlinks = 1; |
| 2640 | int result; |
| 2641 | PyObject *return_value = NULL; |
| 2642 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2643 | "follow_symlinks", NULL}; |
| 2644 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2645 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2646 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2647 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2648 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2649 | #ifdef HAVE_FCHMODAT |
| 2650 | int fchmodat_nofollow_unsupported = 0; |
| 2651 | #endif |
| 2652 | |
| 2653 | memset(&path, 0, sizeof(path)); |
| 2654 | #ifdef HAVE_FCHMOD |
| 2655 | path.allow_fd = 1; |
| 2656 | #endif |
| 2657 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2658 | path_converter, &path, |
| 2659 | &mode, |
| 2660 | #ifdef HAVE_FCHMODAT |
| 2661 | dir_fd_converter, &dir_fd, |
| 2662 | #else |
| 2663 | dir_fd_unavailable, &dir_fd, |
| 2664 | #endif |
| 2665 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2666 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2667 | |
| 2668 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2669 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2670 | goto exit; |
| 2671 | #endif |
| 2672 | |
| 2673 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2674 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2675 | if (path.wide) |
| 2676 | attr = GetFileAttributesW(path.wide); |
| 2677 | else |
| 2678 | attr = GetFileAttributesA(path.narrow); |
| 2679 | if (attr == 0xFFFFFFFF) |
| 2680 | result = 0; |
| 2681 | else { |
| 2682 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2683 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2684 | else |
| 2685 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2686 | if (path.wide) |
| 2687 | result = SetFileAttributesW(path.wide, attr); |
| 2688 | else |
| 2689 | result = SetFileAttributesA(path.narrow, attr); |
| 2690 | } |
| 2691 | Py_END_ALLOW_THREADS |
| 2692 | |
| 2693 | if (!result) { |
| 2694 | return_value = win32_error_object("chmod", path.object); |
| 2695 | goto exit; |
| 2696 | } |
| 2697 | #else /* MS_WINDOWS */ |
| 2698 | Py_BEGIN_ALLOW_THREADS |
| 2699 | #ifdef HAVE_FCHMOD |
| 2700 | if (path.fd != -1) |
| 2701 | result = fchmod(path.fd, mode); |
| 2702 | else |
| 2703 | #endif |
| 2704 | #ifdef HAVE_LCHMOD |
| 2705 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2706 | result = lchmod(path.narrow, mode); |
| 2707 | else |
| 2708 | #endif |
| 2709 | #ifdef HAVE_FCHMODAT |
| 2710 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2711 | /* |
| 2712 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2713 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2714 | * and then says it isn't implemented yet. |
| 2715 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2716 | * |
| 2717 | * Once it is supported, os.chmod will automatically |
| 2718 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2719 | * Until then, we need to be careful what exception we raise. |
| 2720 | */ |
| 2721 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2722 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2723 | /* |
| 2724 | * But wait! We can't throw the exception without allowing threads, |
| 2725 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2726 | */ |
| 2727 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2728 | result && |
| 2729 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2730 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2731 | } |
| 2732 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2733 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2734 | result = chmod(path.narrow, mode); |
| 2735 | Py_END_ALLOW_THREADS |
| 2736 | |
| 2737 | if (result) { |
| 2738 | #ifdef HAVE_FCHMODAT |
| 2739 | if (fchmodat_nofollow_unsupported) { |
| 2740 | if (dir_fd != DEFAULT_DIR_FD) |
| 2741 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2742 | dir_fd, follow_symlinks); |
| 2743 | else |
| 2744 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2745 | } |
| 2746 | else |
| 2747 | #endif |
| 2748 | return_value = path_error("chmod", &path); |
| 2749 | goto exit; |
| 2750 | } |
| 2751 | #endif |
| 2752 | |
| 2753 | Py_INCREF(Py_None); |
| 2754 | return_value = Py_None; |
| 2755 | exit: |
| 2756 | path_cleanup(&path); |
| 2757 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2760 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2761 | #ifdef HAVE_FCHMOD |
| 2762 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2763 | "fchmod(fd, mode)\n\n\ |
| 2764 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2765 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2766 | |
| 2767 | static PyObject * |
| 2768 | posix_fchmod(PyObject *self, PyObject *args) |
| 2769 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2770 | int fd, mode, res; |
| 2771 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2772 | return NULL; |
| 2773 | Py_BEGIN_ALLOW_THREADS |
| 2774 | res = fchmod(fd, mode); |
| 2775 | Py_END_ALLOW_THREADS |
| 2776 | if (res < 0) |
| 2777 | return posix_error(); |
| 2778 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2779 | } |
| 2780 | #endif /* HAVE_FCHMOD */ |
| 2781 | |
| 2782 | #ifdef HAVE_LCHMOD |
| 2783 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2784 | "lchmod(path, mode)\n\n\ |
| 2785 | 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] | 2786 | affects the link itself rather than the target.\n\ |
| 2787 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2788 | |
| 2789 | static PyObject * |
| 2790 | posix_lchmod(PyObject *self, PyObject *args) |
| 2791 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2792 | PyObject *opath; |
| 2793 | char *path; |
| 2794 | int i; |
| 2795 | int res; |
| 2796 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2797 | &opath, &i)) |
| 2798 | return NULL; |
| 2799 | path = PyBytes_AsString(opath); |
| 2800 | Py_BEGIN_ALLOW_THREADS |
| 2801 | res = lchmod(path, i); |
| 2802 | Py_END_ALLOW_THREADS |
| 2803 | if (res < 0) |
| 2804 | return posix_error_with_allocated_filename(opath); |
| 2805 | Py_DECREF(opath); |
| 2806 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2807 | } |
| 2808 | #endif /* HAVE_LCHMOD */ |
| 2809 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2810 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2811 | #ifdef HAVE_CHFLAGS |
| 2812 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2813 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 2814 | Set file flags.\n\ |
| 2815 | \n\ |
| 2816 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2817 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 2818 | file the link points to.\n\ |
| 2819 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 2820 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2821 | |
| 2822 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2823 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2824 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2825 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2826 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2827 | int follow_symlinks = 1; |
| 2828 | int result; |
| 2829 | PyObject *return_value; |
| 2830 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 2831 | |
| 2832 | memset(&path, 0, sizeof(path)); |
| 2833 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 2834 | path_converter, &path, |
| 2835 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2836 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2837 | |
| 2838 | #ifndef HAVE_LCHFLAGS |
| 2839 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 2840 | goto exit; |
| 2841 | #endif |
| 2842 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2843 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2844 | #ifdef HAVE_LCHFLAGS |
| 2845 | if (!follow_symlinks) |
| 2846 | result = lchflags(path.narrow, flags); |
| 2847 | else |
| 2848 | #endif |
| 2849 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2850 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2851 | |
| 2852 | if (result) { |
| 2853 | return_value = path_posix_error("chflags", &path); |
| 2854 | goto exit; |
| 2855 | } |
| 2856 | |
| 2857 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2858 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2859 | |
| 2860 | exit: |
| 2861 | path_cleanup(&path); |
| 2862 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2863 | } |
| 2864 | #endif /* HAVE_CHFLAGS */ |
| 2865 | |
| 2866 | #ifdef HAVE_LCHFLAGS |
| 2867 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2868 | "lchflags(path, flags)\n\n\ |
| 2869 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2870 | This function will not follow symbolic links.\n\ |
| 2871 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2872 | |
| 2873 | static PyObject * |
| 2874 | posix_lchflags(PyObject *self, PyObject *args) |
| 2875 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2876 | PyObject *opath; |
| 2877 | char *path; |
| 2878 | unsigned long flags; |
| 2879 | int res; |
| 2880 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2881 | PyUnicode_FSConverter, &opath, &flags)) |
| 2882 | return NULL; |
| 2883 | path = PyBytes_AsString(opath); |
| 2884 | Py_BEGIN_ALLOW_THREADS |
| 2885 | res = lchflags(path, flags); |
| 2886 | Py_END_ALLOW_THREADS |
| 2887 | if (res < 0) |
| 2888 | return posix_error_with_allocated_filename(opath); |
| 2889 | Py_DECREF(opath); |
| 2890 | Py_INCREF(Py_None); |
| 2891 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2892 | } |
| 2893 | #endif /* HAVE_LCHFLAGS */ |
| 2894 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2895 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2896 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2897 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2898 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2899 | |
| 2900 | static PyObject * |
| 2901 | posix_chroot(PyObject *self, PyObject *args) |
| 2902 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2903 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2904 | } |
| 2905 | #endif |
| 2906 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2907 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2908 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2909 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2910 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2911 | |
| 2912 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2913 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2914 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2915 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2916 | } |
| 2917 | #endif /* HAVE_FSYNC */ |
| 2918 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2919 | #ifdef HAVE_SYNC |
| 2920 | PyDoc_STRVAR(posix_sync__doc__, |
| 2921 | "sync()\n\n\ |
| 2922 | Force write of everything to disk."); |
| 2923 | |
| 2924 | static PyObject * |
| 2925 | posix_sync(PyObject *self, PyObject *noargs) |
| 2926 | { |
| 2927 | Py_BEGIN_ALLOW_THREADS |
| 2928 | sync(); |
| 2929 | Py_END_ALLOW_THREADS |
| 2930 | Py_RETURN_NONE; |
| 2931 | } |
| 2932 | #endif |
| 2933 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2934 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2935 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2936 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2937 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2938 | #endif |
| 2939 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2940 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2941 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2942 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2943 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2944 | |
| 2945 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2946 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2947 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2948 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2949 | } |
| 2950 | #endif /* HAVE_FDATASYNC */ |
| 2951 | |
| 2952 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2953 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2954 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2955 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2956 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2957 | \n\ |
| 2958 | path may always be specified as a string.\n\ |
| 2959 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2960 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2961 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2962 | and path should be relative; path will then be relative to that directory.\n\ |
| 2963 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2964 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 2965 | link points to.\n\ |
| 2966 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2967 | an open file descriptor.\n\ |
| 2968 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2969 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2970 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2971 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2972 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2973 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2974 | path_t path; |
| 2975 | long uid_l, gid_l; |
| 2976 | uid_t uid; |
| 2977 | gid_t gid; |
| 2978 | int dir_fd = DEFAULT_DIR_FD; |
| 2979 | int follow_symlinks = 1; |
| 2980 | int result; |
| 2981 | PyObject *return_value = NULL; |
| 2982 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 2983 | "follow_symlinks", NULL}; |
| 2984 | |
| 2985 | memset(&path, 0, sizeof(path)); |
| 2986 | #ifdef HAVE_FCHOWN |
| 2987 | path.allow_fd = 1; |
| 2988 | #endif |
| 2989 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&ll|$O&p:chown", keywords, |
| 2990 | path_converter, &path, |
| 2991 | &uid_l, &gid_l, |
| 2992 | #ifdef HAVE_FCHOWNAT |
| 2993 | dir_fd_converter, &dir_fd, |
| 2994 | #else |
| 2995 | dir_fd_unavailable, &dir_fd, |
| 2996 | #endif |
| 2997 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2998 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2999 | |
| 3000 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3001 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 3002 | goto exit; |
| 3003 | #endif |
| 3004 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 3005 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 3006 | goto exit; |
| 3007 | |
| 3008 | #ifdef __APPLE__ |
| 3009 | /* |
| 3010 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3011 | * (But we still have an lchown symbol because of weak-linking.) |
| 3012 | * It doesn't have fchownat either. So there's no possibility |
| 3013 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3014 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3015 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3016 | follow_symlinks_specified("chown", follow_symlinks); |
| 3017 | goto exit; |
| 3018 | } |
| 3019 | #endif |
| 3020 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3021 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3022 | uid = (uid_t)uid_l; |
| 3023 | gid = (uid_t)gid_l; |
| 3024 | #ifdef HAVE_FCHOWN |
| 3025 | if (path.fd != -1) |
| 3026 | result = fchown(path.fd, uid, gid); |
| 3027 | else |
| 3028 | #endif |
| 3029 | #ifdef HAVE_LCHOWN |
| 3030 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 3031 | result = lchown(path.narrow, uid, gid); |
| 3032 | else |
| 3033 | #endif |
| 3034 | #ifdef HAVE_FCHOWNAT |
| 3035 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 3036 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 3037 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3038 | else |
| 3039 | #endif |
| 3040 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3041 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3042 | |
| 3043 | if (result) { |
| 3044 | return_value = path_posix_error("chown", &path); |
| 3045 | goto exit; |
| 3046 | } |
| 3047 | |
| 3048 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3049 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3050 | |
| 3051 | exit: |
| 3052 | path_cleanup(&path); |
| 3053 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3054 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3055 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3056 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3057 | #ifdef HAVE_FCHOWN |
| 3058 | PyDoc_STRVAR(posix_fchown__doc__, |
| 3059 | "fchown(fd, uid, gid)\n\n\ |
| 3060 | 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] | 3061 | 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] | 3062 | |
| 3063 | static PyObject * |
| 3064 | posix_fchown(PyObject *self, PyObject *args) |
| 3065 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3066 | int fd; |
| 3067 | long uid, gid; |
| 3068 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 3069 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3070 | return NULL; |
| 3071 | Py_BEGIN_ALLOW_THREADS |
| 3072 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 3073 | Py_END_ALLOW_THREADS |
| 3074 | if (res < 0) |
| 3075 | return posix_error(); |
| 3076 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3077 | } |
| 3078 | #endif /* HAVE_FCHOWN */ |
| 3079 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3080 | #ifdef HAVE_LCHOWN |
| 3081 | PyDoc_STRVAR(posix_lchown__doc__, |
| 3082 | "lchown(path, uid, gid)\n\n\ |
| 3083 | 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] | 3084 | This function will not follow symbolic links.\n\ |
| 3085 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3086 | |
| 3087 | static PyObject * |
| 3088 | posix_lchown(PyObject *self, PyObject *args) |
| 3089 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3090 | PyObject *opath; |
| 3091 | char *path; |
| 3092 | long uid, gid; |
| 3093 | int res; |
| 3094 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 3095 | PyUnicode_FSConverter, &opath, |
| 3096 | &uid, &gid)) |
| 3097 | return NULL; |
| 3098 | path = PyBytes_AsString(opath); |
| 3099 | Py_BEGIN_ALLOW_THREADS |
| 3100 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 3101 | Py_END_ALLOW_THREADS |
| 3102 | if (res < 0) |
| 3103 | return posix_error_with_allocated_filename(opath); |
| 3104 | Py_DECREF(opath); |
| 3105 | Py_INCREF(Py_None); |
| 3106 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3107 | } |
| 3108 | #endif /* HAVE_LCHOWN */ |
| 3109 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3110 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3111 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3112 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3113 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3114 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3115 | char buf[1026]; |
| 3116 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3117 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3118 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3119 | if (!use_bytes) { |
| 3120 | wchar_t wbuf[1026]; |
| 3121 | wchar_t *wbuf2 = wbuf; |
| 3122 | PyObject *resobj; |
| 3123 | DWORD len; |
| 3124 | Py_BEGIN_ALLOW_THREADS |
| 3125 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 3126 | /* If the buffer is large enough, len does not include the |
| 3127 | terminating \0. If the buffer is too small, len includes |
| 3128 | the space needed for the terminator. */ |
| 3129 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 3130 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 3131 | if (wbuf2) |
| 3132 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3133 | } |
| 3134 | Py_END_ALLOW_THREADS |
| 3135 | if (!wbuf2) { |
| 3136 | PyErr_NoMemory(); |
| 3137 | return NULL; |
| 3138 | } |
| 3139 | if (!len) { |
| 3140 | if (wbuf2 != wbuf) free(wbuf2); |
| 3141 | return win32_error("getcwdu", NULL); |
| 3142 | } |
| 3143 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3144 | if (wbuf2 != wbuf) free(wbuf2); |
| 3145 | return resobj; |
| 3146 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3147 | |
| 3148 | if (win32_warn_bytes_api()) |
| 3149 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3150 | #endif |
| 3151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3152 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3153 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3154 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3155 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3156 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3157 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3158 | Py_END_ALLOW_THREADS |
| 3159 | if (res == NULL) |
| 3160 | return posix_error(); |
| 3161 | if (use_bytes) |
| 3162 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3163 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3164 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3165 | |
| 3166 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 3167 | "getcwd() -> path\n\n\ |
| 3168 | Return a unicode string representing the current working directory."); |
| 3169 | |
| 3170 | static PyObject * |
| 3171 | posix_getcwd_unicode(PyObject *self) |
| 3172 | { |
| 3173 | return posix_getcwd(0); |
| 3174 | } |
| 3175 | |
| 3176 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 3177 | "getcwdb() -> path\n\n\ |
| 3178 | Return a bytes string representing the current working directory."); |
| 3179 | |
| 3180 | static PyObject * |
| 3181 | posix_getcwd_bytes(PyObject *self) |
| 3182 | { |
| 3183 | return posix_getcwd(1); |
| 3184 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3185 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3186 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3187 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3188 | #define HAVE_LINK 1 |
| 3189 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3190 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3191 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3192 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3193 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 3194 | Create a hard link to a file.\n\ |
| 3195 | \n\ |
| 3196 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3197 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3198 | should be relative; the path will then be relative to that directory.\n\ |
| 3199 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3200 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3201 | file the link points to.\n\ |
| 3202 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3203 | platform. If they are unavailable, using them will raise a\n\ |
| 3204 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3205 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3206 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3207 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3208 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3209 | path_t src, dst; |
| 3210 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3211 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3212 | int follow_symlinks = 1; |
| 3213 | PyObject *return_value = NULL; |
| 3214 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3215 | "follow_symlinks", NULL}; |
| 3216 | #ifdef MS_WINDOWS |
| 3217 | BOOL result; |
| 3218 | #else |
| 3219 | int result; |
| 3220 | #endif |
| 3221 | |
| 3222 | memset(&src, 0, sizeof(src)); |
| 3223 | memset(&dst, 0, sizeof(dst)); |
| 3224 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3225 | path_converter, &src, |
| 3226 | path_converter, &dst, |
| 3227 | dir_fd_converter, &src_dir_fd, |
| 3228 | dir_fd_converter, &dst_dir_fd, |
| 3229 | &follow_symlinks)) |
| 3230 | return NULL; |
| 3231 | |
| 3232 | #ifndef HAVE_LINKAT |
| 3233 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3234 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3235 | goto exit; |
| 3236 | } |
| 3237 | #endif |
| 3238 | |
| 3239 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3240 | PyErr_SetString(PyExc_NotImplementedError, |
| 3241 | "link: src and dst must be the same type"); |
| 3242 | goto exit; |
| 3243 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3244 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3245 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3246 | Py_BEGIN_ALLOW_THREADS |
| 3247 | if (src.wide) |
| 3248 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3249 | else |
| 3250 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3251 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3252 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3253 | if (!result) { |
| 3254 | return_value = win32_error_object("link", dst.object); |
| 3255 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3256 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3257 | #else |
| 3258 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3259 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3260 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3261 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3262 | (!follow_symlinks)) |
| 3263 | result = linkat(src_dir_fd, src.narrow, |
| 3264 | dst_dir_fd, dst.narrow, |
| 3265 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3266 | else |
| 3267 | #endif |
| 3268 | result = link(src.narrow, dst.narrow); |
| 3269 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3270 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3271 | if (result) { |
| 3272 | return_value = path_error("link", &dst); |
| 3273 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3274 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3275 | #endif |
| 3276 | |
| 3277 | return_value = Py_None; |
| 3278 | Py_INCREF(Py_None); |
| 3279 | |
| 3280 | exit: |
| 3281 | path_cleanup(&src); |
| 3282 | path_cleanup(&dst); |
| 3283 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3284 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3285 | #endif |
| 3286 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3287 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3288 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3289 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3290 | "listdir(path='.') -> list_of_filenames\n\n\ |
| 3291 | 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] | 3292 | 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] | 3293 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3294 | \n\ |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3295 | path can be specified as either str or bytes. If path is bytes,\n\ |
| 3296 | the filenames returned will also be bytes; in all other circumstances\n\ |
| 3297 | the filenames returned will be str.\n\ |
| 3298 | On some platforms, path may also be specified as an open file descriptor;\n\ |
| 3299 | the file descriptor must refer to a directory.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3300 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3301 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3302 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3303 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3304 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3305 | path_t path; |
| 3306 | PyObject *list = NULL; |
| 3307 | static char *keywords[] = {"path", NULL}; |
| 3308 | int fd = -1; |
| 3309 | |
| 3310 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3311 | PyObject *v; |
| 3312 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3313 | BOOL result; |
| 3314 | WIN32_FIND_DATA FileData; |
| 3315 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 3316 | char *bufptr = namebuf; |
| 3317 | /* only claim to have space for MAX_PATH */ |
| 3318 | Py_ssize_t len = sizeof(namebuf)-5; |
| 3319 | PyObject *po = NULL; |
| 3320 | wchar_t *wnamebuf = NULL; |
| 3321 | #elif defined(PYOS_OS2) |
| 3322 | #ifndef MAX_PATH |
| 3323 | #define MAX_PATH CCHMAXPATH |
| 3324 | #endif |
| 3325 | char *pt; |
| 3326 | PyObject *v; |
| 3327 | char namebuf[MAX_PATH+5]; |
| 3328 | HDIR hdir = 1; |
| 3329 | ULONG srchcnt = 1; |
| 3330 | FILEFINDBUF3 ep; |
| 3331 | APIRET rc; |
| 3332 | #else |
| 3333 | PyObject *v; |
| 3334 | DIR *dirp = NULL; |
| 3335 | struct dirent *ep; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3336 | int return_str; /* if false, return bytes */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3337 | #endif |
| 3338 | |
| 3339 | memset(&path, 0, sizeof(path)); |
| 3340 | path.nullable = 1; |
| 3341 | #ifdef HAVE_FDOPENDIR |
| 3342 | path.allow_fd = 1; |
| 3343 | path.fd = -1; |
| 3344 | #endif |
| 3345 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3346 | path_converter, &path |
| 3347 | )) |
| 3348 | return NULL; |
| 3349 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3350 | /* XXX Should redo this putting the (now four) versions of opendir |
| 3351 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3352 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3353 | if (!path.narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3354 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3355 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3356 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3357 | if (!path.wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3358 | po_wchars = L"."; |
| 3359 | len = 1; |
| 3360 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3361 | po_wchars = path.wide; |
| 3362 | len = wcslen(path.wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3363 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3364 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3365 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 3366 | if (!wnamebuf) { |
| 3367 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3368 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3369 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3370 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3371 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3372 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3373 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 3374 | wnamebuf[len++] = L'\\'; |
| 3375 | wcscpy(wnamebuf + len, L"*.*"); |
| 3376 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3377 | if ((list = PyList_New(0)) == NULL) { |
| 3378 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3379 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3380 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3381 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3382 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3383 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3384 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3385 | if (error == ERROR_FILE_NOT_FOUND) |
| 3386 | goto exit; |
| 3387 | Py_DECREF(list); |
| 3388 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3389 | win32_error_unicode("FindFirstFileW", wnamebuf); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3390 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3391 | } |
| 3392 | do { |
| 3393 | /* Skip over . and .. */ |
| 3394 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3395 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3396 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3397 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3398 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3399 | Py_DECREF(list); |
| 3400 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3401 | break; |
| 3402 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3403 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3404 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3405 | Py_DECREF(list); |
| 3406 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3407 | break; |
| 3408 | } |
| 3409 | Py_DECREF(v); |
| 3410 | } |
| 3411 | Py_BEGIN_ALLOW_THREADS |
| 3412 | result = FindNextFileW(hFindFile, &wFileData); |
| 3413 | Py_END_ALLOW_THREADS |
| 3414 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3415 | it got to the end of the directory. */ |
| 3416 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3417 | Py_DECREF(list); |
| 3418 | list = win32_error_unicode("FindNextFileW", wnamebuf); |
| 3419 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3420 | } |
| 3421 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3422 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3423 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3424 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3425 | strcpy(namebuf, path.narrow); |
| 3426 | len = path.length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3427 | if (len > 0) { |
| 3428 | char ch = namebuf[len-1]; |
| 3429 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 3430 | namebuf[len++] = '/'; |
| 3431 | strcpy(namebuf + len, "*.*"); |
| 3432 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3433 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3434 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3435 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3436 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3437 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3438 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3439 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3440 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3441 | int error = GetLastError(); |
| 3442 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3443 | goto exit; |
| 3444 | Py_DECREF(list); |
| 3445 | list = win32_error("FindFirstFile", namebuf); |
| 3446 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3447 | } |
| 3448 | do { |
| 3449 | /* Skip over . and .. */ |
| 3450 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3451 | strcmp(FileData.cFileName, "..") != 0) { |
| 3452 | v = PyBytes_FromString(FileData.cFileName); |
| 3453 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3454 | Py_DECREF(list); |
| 3455 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3456 | break; |
| 3457 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3458 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3459 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3460 | Py_DECREF(list); |
| 3461 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3462 | break; |
| 3463 | } |
| 3464 | Py_DECREF(v); |
| 3465 | } |
| 3466 | Py_BEGIN_ALLOW_THREADS |
| 3467 | result = FindNextFile(hFindFile, &FileData); |
| 3468 | Py_END_ALLOW_THREADS |
| 3469 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3470 | it got to the end of the directory. */ |
| 3471 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3472 | Py_DECREF(list); |
| 3473 | list = win32_error("FindNextFile", namebuf); |
| 3474 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3475 | } |
| 3476 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3477 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3478 | exit: |
| 3479 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3480 | if (FindClose(hFindFile) == FALSE) { |
| 3481 | if (list != NULL) { |
| 3482 | Py_DECREF(list); |
| 3483 | list = win32_error_object("FindClose", path.object); |
| 3484 | } |
| 3485 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3486 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3487 | if (wnamebuf) |
| 3488 | free(wnamebuf); |
| 3489 | path_cleanup(&path); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3490 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3491 | return list; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3492 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 3493 | #elif defined(PYOS_OS2) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3494 | if (path.length >= MAX_PATH) { |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 3495 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3496 | goto exit; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3497 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3498 | strcpy(namebuf, path.narrow); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3499 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3500 | if (*pt == ALTSEP) |
| 3501 | *pt = SEP; |
| 3502 | if (namebuf[len-1] != SEP) |
| 3503 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3504 | strcpy(namebuf + len, "*.*"); |
| 3505 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3506 | if ((list = PyList_New(0)) == NULL) { |
| 3507 | goto exit; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 3508 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3509 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3510 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 3511 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3512 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3513 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 3514 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 3515 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3516 | |
| 3517 | if (rc != NO_ERROR) { |
| 3518 | errno = ENOENT; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3519 | Py_DECREF(list); |
| 3520 | list = posix_error_with_filename(path.narrow); |
| 3521 | goto exit; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3524 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3525 | do { |
| 3526 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3527 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3528 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3529 | |
| 3530 | strcpy(namebuf, ep.achName); |
| 3531 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3532 | /* Leave Case of Name Alone -- In Native Form */ |
| 3533 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3534 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3535 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3536 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3537 | Py_DECREF(list); |
| 3538 | list = NULL; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3539 | break; |
| 3540 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3541 | if (PyList_Append(list, v) != 0) { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3542 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3543 | Py_DECREF(list); |
| 3544 | list = NULL; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3545 | break; |
| 3546 | } |
| 3547 | Py_DECREF(v); |
| 3548 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 3549 | } |
| 3550 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3551 | exit: |
| 3552 | path_cleanup(&path); |
| 3553 | |
| 3554 | return list; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3555 | #else |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 3556 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3557 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3558 | #ifdef HAVE_FDOPENDIR |
| 3559 | if (path.fd != -1) { |
| 3560 | /* closedir() closes the FD, so we duplicate it */ |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3561 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3562 | fd = dup(path.fd); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3563 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3564 | |
| 3565 | if (fd == -1) { |
| 3566 | list = posix_error(); |
| 3567 | goto exit; |
| 3568 | } |
| 3569 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3570 | return_str = 1; |
| 3571 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3572 | Py_BEGIN_ALLOW_THREADS |
| 3573 | dirp = fdopendir(fd); |
| 3574 | Py_END_ALLOW_THREADS |
| 3575 | } |
| 3576 | else |
| 3577 | #endif |
| 3578 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3579 | char *name; |
| 3580 | if (path.narrow) { |
| 3581 | name = path.narrow; |
| 3582 | /* only return bytes if they specified a bytes object */ |
| 3583 | return_str = !(PyBytes_Check(path.object)); |
| 3584 | } |
| 3585 | else { |
| 3586 | name = "."; |
| 3587 | return_str = 1; |
| 3588 | } |
| 3589 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3590 | Py_BEGIN_ALLOW_THREADS |
| 3591 | dirp = opendir(name); |
| 3592 | Py_END_ALLOW_THREADS |
| 3593 | } |
| 3594 | |
| 3595 | if (dirp == NULL) { |
| 3596 | list = path_error("listdir", &path); |
| 3597 | goto exit; |
| 3598 | } |
| 3599 | if ((list = PyList_New(0)) == NULL) { |
| 3600 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3601 | } |
| 3602 | for (;;) { |
| 3603 | errno = 0; |
| 3604 | Py_BEGIN_ALLOW_THREADS |
| 3605 | ep = readdir(dirp); |
| 3606 | Py_END_ALLOW_THREADS |
| 3607 | if (ep == NULL) { |
| 3608 | if (errno == 0) { |
| 3609 | break; |
| 3610 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3611 | Py_DECREF(list); |
| 3612 | list = path_error("listdir", &path); |
| 3613 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3614 | } |
| 3615 | } |
| 3616 | if (ep->d_name[0] == '.' && |
| 3617 | (NAMLEN(ep) == 1 || |
| 3618 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3619 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3620 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3621 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3622 | else |
| 3623 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3624 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3625 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3626 | break; |
| 3627 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3628 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3629 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3630 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3631 | break; |
| 3632 | } |
| 3633 | Py_DECREF(v); |
| 3634 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3635 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3636 | exit: |
| 3637 | if (dirp != NULL) { |
| 3638 | Py_BEGIN_ALLOW_THREADS |
| 3639 | if (fd > -1) |
| 3640 | rewinddir(dirp); |
| 3641 | closedir(dirp); |
| 3642 | Py_END_ALLOW_THREADS |
| 3643 | } |
| 3644 | |
| 3645 | path_cleanup(&path); |
| 3646 | |
| 3647 | return list; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3648 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 3649 | #endif /* which OS */ |
| 3650 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3651 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3652 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3653 | /* A helper function for abspath on win32 */ |
| 3654 | static PyObject * |
| 3655 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3656 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3657 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3658 | char outbuf[MAX_PATH*2]; |
| 3659 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3660 | PyObject *po; |
| 3661 | |
| 3662 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3663 | { |
| 3664 | wchar_t *wpath; |
| 3665 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 3666 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3667 | DWORD result; |
| 3668 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3669 | |
| 3670 | wpath = PyUnicode_AsUnicode(po); |
| 3671 | if (wpath == NULL) |
| 3672 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3673 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3674 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3675 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3676 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3677 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3678 | if (!woutbufp) |
| 3679 | return PyErr_NoMemory(); |
| 3680 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3681 | } |
| 3682 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3683 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3684 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3685 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3686 | if (woutbufp != woutbuf) |
| 3687 | free(woutbufp); |
| 3688 | return v; |
| 3689 | } |
| 3690 | /* Drop the argument parsing error as narrow strings |
| 3691 | are also valid. */ |
| 3692 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3693 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3694 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3695 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3696 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3697 | if (win32_warn_bytes_api()) |
| 3698 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3699 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3700 | outbuf, &temp)) { |
| 3701 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3702 | return NULL; |
| 3703 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3704 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3705 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3706 | Py_FileSystemDefaultEncoding, NULL); |
| 3707 | } |
| 3708 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3709 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3710 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3711 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3712 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3713 | /* A helper function for samepath on windows */ |
| 3714 | static PyObject * |
| 3715 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3716 | { |
| 3717 | HANDLE hFile; |
| 3718 | int buf_size; |
| 3719 | wchar_t *target_path; |
| 3720 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3721 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3722 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3723 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3724 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3725 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3726 | path = PyUnicode_AsUnicode(po); |
| 3727 | if (path == NULL) |
| 3728 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3729 | |
| 3730 | if(!check_GetFinalPathNameByHandle()) { |
| 3731 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3732 | NotImplementedError. */ |
| 3733 | return PyErr_Format(PyExc_NotImplementedError, |
| 3734 | "GetFinalPathNameByHandle not available on this platform"); |
| 3735 | } |
| 3736 | |
| 3737 | hFile = CreateFileW( |
| 3738 | path, |
| 3739 | 0, /* desired access */ |
| 3740 | 0, /* share mode */ |
| 3741 | NULL, /* security attributes */ |
| 3742 | OPEN_EXISTING, |
| 3743 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3744 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3745 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3746 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3747 | if(hFile == INVALID_HANDLE_VALUE) |
| 3748 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3749 | |
| 3750 | /* We have a good handle to the target, use it to determine the |
| 3751 | target path name. */ |
| 3752 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3753 | |
| 3754 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3755 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3756 | |
| 3757 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3758 | if(!target_path) |
| 3759 | return PyErr_NoMemory(); |
| 3760 | |
| 3761 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3762 | buf_size, VOLUME_NAME_DOS); |
| 3763 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3764 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3765 | |
| 3766 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3767 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3768 | |
| 3769 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3770 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3771 | free(target_path); |
| 3772 | return result; |
| 3773 | |
| 3774 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3775 | |
| 3776 | static PyObject * |
| 3777 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3778 | { |
| 3779 | HANDLE hFile; |
| 3780 | BY_HANDLE_FILE_INFORMATION info; |
| 3781 | int fd; |
| 3782 | |
| 3783 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3784 | return NULL; |
| 3785 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3786 | if (!_PyVerify_fd(fd)) |
| 3787 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3788 | |
| 3789 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3790 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3791 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3792 | |
| 3793 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3794 | return win32_error("_getfileinformation", NULL); |
| 3795 | |
| 3796 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3797 | info.nFileIndexHigh, |
| 3798 | info.nFileIndexLow); |
| 3799 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3800 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3801 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3802 | "Return true if the pathname refers to an existing directory."); |
| 3803 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3804 | static PyObject * |
| 3805 | posix__isdir(PyObject *self, PyObject *args) |
| 3806 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3807 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3808 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3809 | DWORD attributes; |
| 3810 | |
| 3811 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3812 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3813 | if (wpath == NULL) |
| 3814 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3815 | |
| 3816 | attributes = GetFileAttributesW(wpath); |
| 3817 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3818 | Py_RETURN_FALSE; |
| 3819 | goto check; |
| 3820 | } |
| 3821 | /* Drop the argument parsing error as narrow strings |
| 3822 | are also valid. */ |
| 3823 | PyErr_Clear(); |
| 3824 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3825 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3826 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3827 | if (win32_warn_bytes_api()) |
| 3828 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3829 | attributes = GetFileAttributesA(path); |
| 3830 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3831 | Py_RETURN_FALSE; |
| 3832 | |
| 3833 | check: |
| 3834 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3835 | Py_RETURN_TRUE; |
| 3836 | else |
| 3837 | Py_RETURN_FALSE; |
| 3838 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3839 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3840 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3841 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3842 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3843 | Create a directory.\n\ |
| 3844 | \n\ |
| 3845 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3846 | and path should be relative; path will then be relative to that directory.\n\ |
| 3847 | dir_fd may not be implemented on your platform.\n\ |
| 3848 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 3849 | \n\ |
| 3850 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3851 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3852 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3853 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3854 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3855 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3856 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3857 | int dir_fd = DEFAULT_DIR_FD; |
| 3858 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 3859 | PyObject *return_value = NULL; |
| 3860 | int result; |
| 3861 | |
| 3862 | memset(&path, 0, sizeof(path)); |
| 3863 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 3864 | path_converter, &path, &mode, |
| 3865 | #ifdef HAVE_MKDIRAT |
| 3866 | dir_fd_converter, &dir_fd |
| 3867 | #else |
| 3868 | dir_fd_unavailable, &dir_fd |
| 3869 | #endif |
| 3870 | )) |
| 3871 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3872 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3873 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3874 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3875 | if (path.wide) |
| 3876 | result = CreateDirectoryW(path.wide, NULL); |
| 3877 | else |
| 3878 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3879 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3880 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3881 | if (!result) { |
| 3882 | return_value = win32_error_object("mkdir", path.object); |
| 3883 | goto exit; |
| 3884 | } |
| 3885 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3886 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3887 | #if HAVE_MKDIRAT |
| 3888 | if (dir_fd != DEFAULT_DIR_FD) |
| 3889 | result = mkdirat(dir_fd, path.narrow, mode); |
| 3890 | else |
| 3891 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3892 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3893 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3894 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3895 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3896 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3897 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3898 | if (result < 0) { |
| 3899 | return_value = path_error("mkdir", &path); |
| 3900 | goto exit; |
| 3901 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3902 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3903 | return_value = Py_None; |
| 3904 | Py_INCREF(Py_None); |
| 3905 | exit: |
| 3906 | path_cleanup(&path); |
| 3907 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3910 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3911 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3912 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3913 | #include <sys/resource.h> |
| 3914 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3915 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3916 | |
| 3917 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3918 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3919 | "nice(inc) -> new_priority\n\n\ |
| 3920 | 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] | 3921 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3922 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3923 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3924 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3925 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3926 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3927 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3928 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3929 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3930 | /* There are two flavours of 'nice': one that returns the new |
| 3931 | priority (as required by almost all standards out there) and the |
| 3932 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3933 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3934 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3935 | If we are of the nice family that returns the new priority, we |
| 3936 | need to clear errno before the call, and check if errno is filled |
| 3937 | before calling posix_error() on a returnvalue of -1, because the |
| 3938 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3939 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3940 | errno = 0; |
| 3941 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3942 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3943 | if (value == 0) |
| 3944 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3945 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3946 | if (value == -1 && errno != 0) |
| 3947 | /* either nice() or getpriority() returned an error */ |
| 3948 | return posix_error(); |
| 3949 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3950 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3951 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3952 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3953 | |
| 3954 | #ifdef HAVE_GETPRIORITY |
| 3955 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3956 | "getpriority(which, who) -> current_priority\n\n\ |
| 3957 | Get program scheduling priority."); |
| 3958 | |
| 3959 | static PyObject * |
| 3960 | posix_getpriority(PyObject *self, PyObject *args) |
| 3961 | { |
| 3962 | int which, who, retval; |
| 3963 | |
| 3964 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3965 | return NULL; |
| 3966 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3967 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3968 | if (errno != 0) |
| 3969 | return posix_error(); |
| 3970 | return PyLong_FromLong((long)retval); |
| 3971 | } |
| 3972 | #endif /* HAVE_GETPRIORITY */ |
| 3973 | |
| 3974 | |
| 3975 | #ifdef HAVE_SETPRIORITY |
| 3976 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3977 | "setpriority(which, who, prio) -> None\n\n\ |
| 3978 | Set program scheduling priority."); |
| 3979 | |
| 3980 | static PyObject * |
| 3981 | posix_setpriority(PyObject *self, PyObject *args) |
| 3982 | { |
| 3983 | int which, who, prio, retval; |
| 3984 | |
| 3985 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3986 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3987 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3988 | if (retval == -1) |
| 3989 | return posix_error(); |
| 3990 | Py_RETURN_NONE; |
| 3991 | } |
| 3992 | #endif /* HAVE_SETPRIORITY */ |
| 3993 | |
| 3994 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3995 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3996 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3997 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3998 | char *function_name = is_replace ? "replace" : "rename"; |
| 3999 | path_t src; |
| 4000 | path_t dst; |
| 4001 | int src_dir_fd = DEFAULT_DIR_FD; |
| 4002 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 4003 | int dir_fd_specified; |
| 4004 | PyObject *return_value = NULL; |
| 4005 | char format[24]; |
| 4006 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 4007 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4008 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4009 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4010 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4011 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4012 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4013 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4014 | |
| 4015 | memset(&src, 0, sizeof(src)); |
| 4016 | memset(&dst, 0, sizeof(dst)); |
| 4017 | strcpy(format, "O&O&|$O&O&:"); |
| 4018 | strcat(format, function_name); |
| 4019 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 4020 | path_converter, &src, |
| 4021 | path_converter, &dst, |
| 4022 | dir_fd_converter, &src_dir_fd, |
| 4023 | dir_fd_converter, &dst_dir_fd)) |
| 4024 | return NULL; |
| 4025 | |
| 4026 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4027 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4028 | #ifndef HAVE_RENAMEAT |
| 4029 | if (dir_fd_specified) { |
| 4030 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 4031 | goto exit; |
| 4032 | } |
| 4033 | #endif |
| 4034 | |
| 4035 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 4036 | PyErr_Format(PyExc_ValueError, |
| 4037 | "%s: src and dst must be the same type", function_name); |
| 4038 | goto exit; |
| 4039 | } |
| 4040 | |
| 4041 | #ifdef MS_WINDOWS |
| 4042 | Py_BEGIN_ALLOW_THREADS |
| 4043 | if (src.wide) |
| 4044 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 4045 | else |
| 4046 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 4047 | Py_END_ALLOW_THREADS |
| 4048 | |
| 4049 | if (!result) { |
| 4050 | return_value = win32_error_object(function_name, dst.object); |
| 4051 | goto exit; |
| 4052 | } |
| 4053 | |
| 4054 | #else |
| 4055 | Py_BEGIN_ALLOW_THREADS |
| 4056 | #ifdef HAVE_RENAMEAT |
| 4057 | if (dir_fd_specified) |
| 4058 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 4059 | else |
| 4060 | #endif |
| 4061 | result = rename(src.narrow, dst.narrow); |
| 4062 | Py_END_ALLOW_THREADS |
| 4063 | |
| 4064 | if (result) { |
| 4065 | return_value = path_error(function_name, &dst); |
| 4066 | goto exit; |
| 4067 | } |
| 4068 | #endif |
| 4069 | |
| 4070 | Py_INCREF(Py_None); |
| 4071 | return_value = Py_None; |
| 4072 | exit: |
| 4073 | path_cleanup(&src); |
| 4074 | path_cleanup(&dst); |
| 4075 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4078 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4079 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4080 | Rename a file or directory.\n\ |
| 4081 | \n\ |
| 4082 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4083 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4084 | should be relative; the path will then be relative to that directory.\n\ |
| 4085 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4086 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4087 | |
| 4088 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4089 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4090 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4091 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4095 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4096 | Rename a file or directory, overwriting the destination.\n\ |
| 4097 | \n\ |
| 4098 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4099 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4100 | should be relative; the path will then be relative to that directory.\n\ |
| 4101 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4102 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4103 | |
| 4104 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4105 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4106 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4107 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4108 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4109 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4110 | PyDoc_STRVAR(posix_rmdir__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4111 | "rmdir(path, *, dir_fd=None)\n\n\ |
| 4112 | Remove a directory.\n\ |
| 4113 | \n\ |
| 4114 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4115 | and path should be relative; path will then be relative to that directory.\n\ |
| 4116 | dir_fd may not be implemented on your platform.\n\ |
| 4117 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4118 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4119 | static PyObject * |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4120 | posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4121 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4122 | path_t path; |
| 4123 | int dir_fd = DEFAULT_DIR_FD; |
| 4124 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 4125 | int result; |
| 4126 | PyObject *return_value = NULL; |
| 4127 | |
| 4128 | memset(&path, 0, sizeof(path)); |
| 4129 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords, |
| 4130 | path_converter, &path, |
| 4131 | #ifdef HAVE_UNLINKAT |
| 4132 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4133 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4134 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4135 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4136 | )) |
| 4137 | return NULL; |
| 4138 | |
| 4139 | Py_BEGIN_ALLOW_THREADS |
| 4140 | #ifdef MS_WINDOWS |
| 4141 | if (path.wide) |
| 4142 | result = RemoveDirectoryW(path.wide); |
| 4143 | else |
| 4144 | result = RemoveDirectoryA(path.narrow); |
| 4145 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4146 | #else |
| 4147 | #ifdef HAVE_UNLINKAT |
| 4148 | if (dir_fd != DEFAULT_DIR_FD) |
| 4149 | result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR); |
| 4150 | else |
| 4151 | #endif |
| 4152 | result = rmdir(path.narrow); |
| 4153 | #endif |
| 4154 | Py_END_ALLOW_THREADS |
| 4155 | |
| 4156 | if (result) { |
| 4157 | return_value = path_error("rmdir", &path); |
| 4158 | goto exit; |
| 4159 | } |
| 4160 | |
| 4161 | return_value = Py_None; |
| 4162 | Py_INCREF(Py_None); |
| 4163 | |
| 4164 | exit: |
| 4165 | path_cleanup(&path); |
| 4166 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4169 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4170 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4171 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4172 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4173 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4174 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4175 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4176 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4177 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4178 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 4179 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4180 | wchar_t *command; |
| 4181 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 4182 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4184 | Py_BEGIN_ALLOW_THREADS |
| 4185 | sts = _wsystem(command); |
| 4186 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4187 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4188 | PyObject *command_obj; |
| 4189 | char *command; |
| 4190 | if (!PyArg_ParseTuple(args, "O&:system", |
| 4191 | PyUnicode_FSConverter, &command_obj)) |
| 4192 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4193 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4194 | command = PyBytes_AsString(command_obj); |
| 4195 | Py_BEGIN_ALLOW_THREADS |
| 4196 | sts = system(command); |
| 4197 | Py_END_ALLOW_THREADS |
| 4198 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4199 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4200 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4201 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4202 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4203 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4205 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4206 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4207 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4208 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4209 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4210 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4211 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4212 | int i; |
| 4213 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 4214 | return NULL; |
| 4215 | i = (int)umask(i); |
| 4216 | if (i < 0) |
| 4217 | return posix_error(); |
| 4218 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4221 | #ifdef MS_WINDOWS |
| 4222 | |
| 4223 | /* override the default DeleteFileW behavior so that directory |
| 4224 | symlinks can be removed with this function, the same as with |
| 4225 | Unix symlinks */ |
| 4226 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4227 | { |
| 4228 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4229 | WIN32_FIND_DATAW find_data; |
| 4230 | HANDLE find_data_handle; |
| 4231 | int is_directory = 0; |
| 4232 | int is_link = 0; |
| 4233 | |
| 4234 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4235 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4236 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4237 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4238 | it is a symlink */ |
| 4239 | if(is_directory && |
| 4240 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4241 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4242 | |
| 4243 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 4244 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 4245 | FindClose(find_data_handle); |
| 4246 | } |
| 4247 | } |
| 4248 | } |
| 4249 | |
| 4250 | if (is_directory && is_link) |
| 4251 | return RemoveDirectoryW(lpFileName); |
| 4252 | |
| 4253 | return DeleteFileW(lpFileName); |
| 4254 | } |
| 4255 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4257 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4258 | "unlink(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4259 | Remove a file (same as remove()).\n\ |
| 4260 | \n\ |
| 4261 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4262 | and path should be relative; path will then be relative to that directory.\n\ |
| 4263 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4264 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4266 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4267 | "remove(path, *, dir_fd=None)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4268 | Remove a file (same as unlink()).\n\ |
| 4269 | \n\ |
| 4270 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4271 | and path should be relative; path will then be relative to that directory.\n\ |
| 4272 | dir_fd may not be implemented on your platform.\n\ |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4273 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4274 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4275 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4276 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4277 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4278 | path_t path; |
| 4279 | int dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4280 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4281 | int result; |
| 4282 | PyObject *return_value = NULL; |
| 4283 | |
| 4284 | memset(&path, 0, sizeof(path)); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4285 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4286 | path_converter, &path, |
| 4287 | #ifdef HAVE_UNLINKAT |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4288 | dir_fd_converter, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4289 | #else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4290 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4291 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4292 | )) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4293 | return NULL; |
| 4294 | |
| 4295 | Py_BEGIN_ALLOW_THREADS |
| 4296 | #ifdef MS_WINDOWS |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4297 | if (path.wide) |
| 4298 | result = Py_DeleteFileW(path.wide); |
| 4299 | else |
| 4300 | result = DeleteFileA(path.narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4301 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4302 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4303 | #ifdef HAVE_UNLINKAT |
| 4304 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4305 | result = unlinkat(dir_fd, path.narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4306 | else |
| 4307 | #endif /* HAVE_UNLINKAT */ |
| 4308 | result = unlink(path.narrow); |
| 4309 | #endif |
| 4310 | Py_END_ALLOW_THREADS |
| 4311 | |
| 4312 | if (result) { |
| 4313 | return_value = path_error("unlink", &path); |
| 4314 | goto exit; |
| 4315 | } |
| 4316 | |
| 4317 | return_value = Py_None; |
| 4318 | Py_INCREF(Py_None); |
| 4319 | |
| 4320 | exit: |
| 4321 | path_cleanup(&path); |
| 4322 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4326 | PyDoc_STRVAR(posix_uname__doc__, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4327 | "uname() -> uname_result\n\n\ |
| 4328 | Return an object identifying the current operating system.\n\ |
| 4329 | The object behaves like a named tuple with the following fields:\n\ |
| 4330 | (sysname, nodename, release, version, machine)"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4331 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4332 | static PyStructSequence_Field uname_result_fields[] = { |
| 4333 | {"sysname", "operating system name"}, |
| 4334 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4335 | {"release", "operating system release"}, |
| 4336 | {"version", "operating system version"}, |
| 4337 | {"machine", "hardware identifier"}, |
| 4338 | {NULL} |
| 4339 | }; |
| 4340 | |
| 4341 | PyDoc_STRVAR(uname_result__doc__, |
| 4342 | "uname_result: Result from os.uname().\n\n\ |
| 4343 | This object may be accessed either as a tuple of\n\ |
| 4344 | (sysname, nodename, release, version, machine),\n\ |
| 4345 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4346 | \n\ |
| 4347 | See os.uname for more information."); |
| 4348 | |
| 4349 | static PyStructSequence_Desc uname_result_desc = { |
| 4350 | "uname_result", /* name */ |
| 4351 | uname_result__doc__, /* doc */ |
| 4352 | uname_result_fields, |
| 4353 | 5 |
| 4354 | }; |
| 4355 | |
| 4356 | static PyTypeObject UnameResultType; |
| 4357 | |
| 4358 | |
| 4359 | #ifdef HAVE_UNAME |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4360 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4361 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4362 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4363 | struct utsname u; |
| 4364 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4365 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4366 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4367 | Py_BEGIN_ALLOW_THREADS |
| 4368 | res = uname(&u); |
| 4369 | Py_END_ALLOW_THREADS |
| 4370 | if (res < 0) |
| 4371 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4372 | |
| 4373 | value = PyStructSequence_New(&UnameResultType); |
| 4374 | if (value == NULL) |
| 4375 | return NULL; |
| 4376 | |
| 4377 | #define SET(i, field) \ |
| 4378 | { \ |
| 4379 | PyObject *o = PyUnicode_DecodeASCII(field, strlen(field), NULL); \ |
| 4380 | if (!o) { \ |
| 4381 | Py_DECREF(value); \ |
| 4382 | return NULL; \ |
| 4383 | } \ |
| 4384 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4385 | } \ |
| 4386 | |
| 4387 | SET(0, u.sysname); |
| 4388 | SET(1, u.nodename); |
| 4389 | SET(2, u.release); |
| 4390 | SET(3, u.version); |
| 4391 | SET(4, u.machine); |
| 4392 | |
| 4393 | #undef SET |
| 4394 | |
| 4395 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4396 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4397 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4398 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4399 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4400 | PyDoc_STRVAR(posix_utime__doc__, |
| 4401 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4402 | Set the access and modified time of path.\n\ |
| 4403 | \n\ |
| 4404 | path may always be specified as a string.\n\ |
| 4405 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4406 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4407 | \n\ |
| 4408 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4409 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4410 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4411 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4412 | since the epoch.\n\ |
| 4413 | If both times and ns are None, utime uses the current time.\n\ |
| 4414 | Specifying tuples for both times and ns is an error.\n\ |
| 4415 | \n\ |
| 4416 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4417 | and path should be relative; path will then be relative to that directory.\n\ |
| 4418 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4419 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4420 | link points to.\n\ |
| 4421 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4422 | as an open file descriptor.\n\ |
| 4423 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4424 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4425 | |
| 4426 | typedef struct { |
| 4427 | int now; |
| 4428 | time_t atime_s; |
| 4429 | long atime_ns; |
| 4430 | time_t mtime_s; |
| 4431 | long mtime_ns; |
| 4432 | } utime_t; |
| 4433 | |
| 4434 | /* |
| 4435 | * these macros assume that "utime" is a pointer to a utime_t |
| 4436 | * they also intentionally leak the declaration of a pointer named "time" |
| 4437 | */ |
| 4438 | #define UTIME_TO_TIMESPEC \ |
| 4439 | struct timespec ts[2]; \ |
| 4440 | struct timespec *time; \ |
| 4441 | if (utime->now) \ |
| 4442 | time = NULL; \ |
| 4443 | else { \ |
| 4444 | ts[0].tv_sec = utime->atime_s; \ |
| 4445 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4446 | ts[1].tv_sec = utime->mtime_s; \ |
| 4447 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4448 | time = ts; \ |
| 4449 | } \ |
| 4450 | |
| 4451 | #define UTIME_TO_TIMEVAL \ |
| 4452 | struct timeval tv[2]; \ |
| 4453 | struct timeval *time; \ |
| 4454 | if (utime->now) \ |
| 4455 | time = NULL; \ |
| 4456 | else { \ |
| 4457 | tv[0].tv_sec = utime->atime_s; \ |
| 4458 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4459 | tv[1].tv_sec = utime->mtime_s; \ |
| 4460 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4461 | time = tv; \ |
| 4462 | } \ |
| 4463 | |
| 4464 | #define UTIME_TO_UTIMBUF \ |
| 4465 | struct utimbuf u[2]; \ |
| 4466 | struct utimbuf *time; \ |
| 4467 | if (utime->now) \ |
| 4468 | time = NULL; \ |
| 4469 | else { \ |
| 4470 | u.actime = utime->atime_s; \ |
| 4471 | u.modtime = utime->mtime_s; \ |
| 4472 | time = u; \ |
| 4473 | } |
| 4474 | |
| 4475 | #define UTIME_TO_TIME_T \ |
| 4476 | time_t timet[2]; \ |
| 4477 | struct timet time; \ |
| 4478 | if (utime->now) \ |
| 4479 | time = NULL; \ |
| 4480 | else { \ |
| 4481 | timet[0] = utime->atime_s; \ |
| 4482 | timet[1] = utime->mtime_s; \ |
| 4483 | time = &timet; \ |
| 4484 | } \ |
| 4485 | |
| 4486 | |
| 4487 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4488 | |
| 4489 | #if UTIME_HAVE_DIR_FD |
| 4490 | |
| 4491 | static int |
| 4492 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4493 | { |
| 4494 | #ifdef HAVE_UTIMENSAT |
| 4495 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4496 | UTIME_TO_TIMESPEC; |
| 4497 | return utimensat(dir_fd, path, time, flags); |
| 4498 | #elif defined(HAVE_FUTIMESAT) |
| 4499 | UTIME_TO_TIMEVAL; |
| 4500 | /* |
| 4501 | * follow_symlinks will never be false here; |
| 4502 | * we only allow !follow_symlinks and dir_fd together |
| 4503 | * if we have utimensat() |
| 4504 | */ |
| 4505 | assert(follow_symlinks); |
| 4506 | return futimesat(dir_fd, path, time); |
| 4507 | #endif |
| 4508 | } |
| 4509 | |
| 4510 | #endif |
| 4511 | |
| 4512 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4513 | |
| 4514 | #if UTIME_HAVE_FD |
| 4515 | |
| 4516 | static int |
| 4517 | utime_fd(utime_t *utime, int fd) |
| 4518 | { |
| 4519 | #ifdef HAVE_FUTIMENS |
| 4520 | UTIME_TO_TIMESPEC; |
| 4521 | return futimens(fd, time); |
| 4522 | #else |
| 4523 | UTIME_TO_TIMEVAL; |
| 4524 | return futimes(fd, time); |
| 4525 | #endif |
| 4526 | } |
| 4527 | |
| 4528 | #endif |
| 4529 | |
| 4530 | |
| 4531 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4532 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4533 | |
| 4534 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4535 | |
| 4536 | static int |
| 4537 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4538 | { |
| 4539 | #ifdef HAVE_UTIMENSAT |
| 4540 | UTIME_TO_TIMESPEC; |
| 4541 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4542 | #else |
| 4543 | UTIME_TO_TIMEVAL; |
| 4544 | return lutimes(path, time); |
| 4545 | #endif |
| 4546 | } |
| 4547 | |
| 4548 | #endif |
| 4549 | |
| 4550 | #ifndef MS_WINDOWS |
| 4551 | |
| 4552 | static int |
| 4553 | utime_default(utime_t *utime, char *path) |
| 4554 | { |
| 4555 | #ifdef HAVE_UTIMENSAT |
| 4556 | UTIME_TO_TIMESPEC; |
| 4557 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4558 | #elif defined(HAVE_UTIMES) |
| 4559 | UTIME_TO_TIMEVAL; |
| 4560 | return utimes(path, time); |
| 4561 | #elif defined(HAVE_UTIME_H) |
| 4562 | UTIME_TO_UTIMBUF; |
| 4563 | return utime(path, time); |
| 4564 | #else |
| 4565 | UTIME_TO_TIME_T; |
| 4566 | return utime(path, time); |
| 4567 | #endif |
| 4568 | } |
| 4569 | |
| 4570 | #endif |
| 4571 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4572 | static int |
| 4573 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4574 | { |
| 4575 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4576 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4577 | divmod = PyNumber_Divmod(py_long, billion); |
| 4578 | if (!divmod) |
| 4579 | goto exit; |
| 4580 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4581 | if ((*s == -1) && PyErr_Occurred()) |
| 4582 | goto exit; |
| 4583 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4584 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4585 | goto exit; |
| 4586 | |
| 4587 | result = 1; |
| 4588 | exit: |
| 4589 | Py_XDECREF(divmod); |
| 4590 | return result; |
| 4591 | } |
| 4592 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4593 | static PyObject * |
| 4594 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4595 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4596 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4597 | PyObject *times = NULL; |
| 4598 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4599 | int dir_fd = DEFAULT_DIR_FD; |
| 4600 | int follow_symlinks = 1; |
| 4601 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4602 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4603 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4604 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4605 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4606 | #ifdef MS_WINDOWS |
| 4607 | HANDLE hFile; |
| 4608 | FILETIME atime, mtime; |
| 4609 | #else |
| 4610 | int result; |
| 4611 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4612 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4613 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4614 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4615 | memset(&path, 0, sizeof(path)); |
| 4616 | #if UTIME_HAVE_FD |
| 4617 | path.allow_fd = 1; |
| 4618 | #endif |
| 4619 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4620 | "O&|O$OO&p:utime", keywords, |
| 4621 | path_converter, &path, |
| 4622 | ×, &ns, |
| 4623 | #if UTIME_HAVE_DIR_FD |
| 4624 | dir_fd_converter, &dir_fd, |
| 4625 | #else |
| 4626 | dir_fd_unavailable, &dir_fd, |
| 4627 | #endif |
| 4628 | &follow_symlinks |
| 4629 | )) |
| 4630 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4631 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4632 | if (times && (times != Py_None) && ns) { |
| 4633 | PyErr_SetString(PyExc_ValueError, |
| 4634 | "utime: you may specify either 'times'" |
| 4635 | " or 'ns' but not both"); |
| 4636 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | if (times && (times != Py_None)) { |
| 4640 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4641 | PyErr_SetString(PyExc_TypeError, |
| 4642 | "utime: 'times' must be either" |
| 4643 | " a tuple of two ints or None"); |
| 4644 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4645 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4646 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4647 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4648 | &utime.atime_s, &utime.atime_ns) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4649 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4650 | &utime.mtime_s, &utime.mtime_ns) == -1) { |
| 4651 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4652 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4653 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4654 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4655 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4656 | PyErr_SetString(PyExc_TypeError, |
| 4657 | "utime: 'ns' must be a tuple of two ints"); |
| 4658 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4659 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4660 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4661 | 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] | 4662 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4663 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4664 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4665 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4666 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4667 | } |
| 4668 | else { |
| 4669 | /* times and ns are both None/unspecified. use "now". */ |
| 4670 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4671 | } |
| 4672 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4673 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4674 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4675 | goto exit; |
| 4676 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4677 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4678 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4679 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4680 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4681 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4682 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4683 | #if !defined(HAVE_UTIMENSAT) |
| 4684 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4685 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4686 | "utime: cannot use dir_fd and follow_symlinks " |
| 4687 | "together on this platform"); |
| 4688 | goto exit; |
| 4689 | } |
| 4690 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4691 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4692 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4693 | Py_BEGIN_ALLOW_THREADS |
| 4694 | if (path.wide) |
| 4695 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4696 | NULL, OPEN_EXISTING, |
| 4697 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4698 | else |
| 4699 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4700 | NULL, OPEN_EXISTING, |
| 4701 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4702 | Py_END_ALLOW_THREADS |
| 4703 | if (hFile == INVALID_HANDLE_VALUE) { |
| 4704 | win32_error_object("utime", path.object); |
| 4705 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4706 | } |
| 4707 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4708 | if (utime.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4709 | SYSTEMTIME now; |
| 4710 | GetSystemTime(&now); |
| 4711 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 4712 | !SystemTimeToFileTime(&now, &atime)) { |
| 4713 | win32_error("utime", NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4714 | goto exit; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4715 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4716 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4717 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4718 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4719 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4720 | } |
| 4721 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4722 | /* Avoid putting the file name into the error here, |
| 4723 | as that may confuse the user into believing that |
| 4724 | something is wrong with the file, when it also |
| 4725 | could be the time stamp that gives a problem. */ |
| 4726 | win32_error("utime", NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4727 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4728 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4729 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4730 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4731 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4732 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4733 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4734 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4735 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4736 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4737 | |
| 4738 | #if UTIME_HAVE_DIR_FD |
| 4739 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4740 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4741 | else |
| 4742 | #endif |
| 4743 | |
| 4744 | #if UTIME_HAVE_FD |
| 4745 | if (path.fd != -1) |
| 4746 | result = utime_fd(&utime, path.fd); |
| 4747 | else |
| 4748 | #endif |
| 4749 | |
| 4750 | result = utime_default(&utime, path.narrow); |
| 4751 | |
| 4752 | Py_END_ALLOW_THREADS |
| 4753 | |
| 4754 | if (result < 0) { |
| 4755 | /* see previous comment about not putting filename in error here */ |
| 4756 | return_value = posix_error(); |
| 4757 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4758 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4759 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4760 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4761 | |
| 4762 | Py_INCREF(Py_None); |
| 4763 | return_value = Py_None; |
| 4764 | |
| 4765 | exit: |
| 4766 | path_cleanup(&path); |
| 4767 | #ifdef MS_WINDOWS |
| 4768 | if (hFile != INVALID_HANDLE_VALUE) |
| 4769 | CloseHandle(hFile); |
| 4770 | #endif |
| 4771 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4774 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4775 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4776 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4777 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4778 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4779 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4780 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4781 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4782 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4783 | int sts; |
| 4784 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 4785 | return NULL; |
| 4786 | _exit(sts); |
| 4787 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4790 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4791 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4792 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4793 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4794 | Py_ssize_t i; |
| 4795 | for (i = 0; i < count; i++) |
| 4796 | PyMem_Free(array[i]); |
| 4797 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4798 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4799 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4800 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4801 | int fsconvert_strdup(PyObject *o, char**out) |
| 4802 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4803 | PyObject *bytes; |
| 4804 | Py_ssize_t size; |
| 4805 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4806 | return 0; |
| 4807 | size = PyBytes_GET_SIZE(bytes); |
| 4808 | *out = PyMem_Malloc(size+1); |
| 4809 | if (!*out) |
| 4810 | return 0; |
| 4811 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4812 | Py_DECREF(bytes); |
| 4813 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4814 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4815 | #endif |
| 4816 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4817 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4818 | static char** |
| 4819 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4820 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4821 | char **envlist; |
| 4822 | Py_ssize_t i, pos, envc; |
| 4823 | PyObject *keys=NULL, *vals=NULL; |
| 4824 | PyObject *key, *val, *key2, *val2; |
| 4825 | char *p, *k, *v; |
| 4826 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4827 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4828 | i = PyMapping_Size(env); |
| 4829 | if (i < 0) |
| 4830 | return NULL; |
| 4831 | envlist = PyMem_NEW(char *, i + 1); |
| 4832 | if (envlist == NULL) { |
| 4833 | PyErr_NoMemory(); |
| 4834 | return NULL; |
| 4835 | } |
| 4836 | envc = 0; |
| 4837 | keys = PyMapping_Keys(env); |
| 4838 | vals = PyMapping_Values(env); |
| 4839 | if (!keys || !vals) |
| 4840 | goto error; |
| 4841 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4842 | PyErr_Format(PyExc_TypeError, |
| 4843 | "env.keys() or env.values() is not a list"); |
| 4844 | goto error; |
| 4845 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4846 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4847 | for (pos = 0; pos < i; pos++) { |
| 4848 | key = PyList_GetItem(keys, pos); |
| 4849 | val = PyList_GetItem(vals, pos); |
| 4850 | if (!key || !val) |
| 4851 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4852 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4853 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4854 | goto error; |
| 4855 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4856 | Py_DECREF(key2); |
| 4857 | goto error; |
| 4858 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4859 | |
| 4860 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4861 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 4862 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4863 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4864 | k = PyBytes_AsString(key2); |
| 4865 | v = PyBytes_AsString(val2); |
| 4866 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4867 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4868 | p = PyMem_NEW(char, len); |
| 4869 | if (p == NULL) { |
| 4870 | PyErr_NoMemory(); |
| 4871 | Py_DECREF(key2); |
| 4872 | Py_DECREF(val2); |
| 4873 | goto error; |
| 4874 | } |
| 4875 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4876 | envlist[envc++] = p; |
| 4877 | Py_DECREF(key2); |
| 4878 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4879 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4880 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4881 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4882 | } |
| 4883 | Py_DECREF(vals); |
| 4884 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4885 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4886 | envlist[envc] = 0; |
| 4887 | *envc_ptr = envc; |
| 4888 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4889 | |
| 4890 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4891 | Py_XDECREF(keys); |
| 4892 | Py_XDECREF(vals); |
| 4893 | while (--envc >= 0) |
| 4894 | PyMem_DEL(envlist[envc]); |
| 4895 | PyMem_DEL(envlist); |
| 4896 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4897 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4898 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4899 | static char** |
| 4900 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4901 | { |
| 4902 | int i; |
| 4903 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4904 | if (argvlist == NULL) { |
| 4905 | PyErr_NoMemory(); |
| 4906 | return NULL; |
| 4907 | } |
| 4908 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4909 | PyObject* item = PySequence_ITEM(argv, i); |
| 4910 | if (item == NULL) |
| 4911 | goto fail; |
| 4912 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4913 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4914 | goto fail; |
| 4915 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4916 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4917 | } |
| 4918 | argvlist[*argc] = NULL; |
| 4919 | return argvlist; |
| 4920 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4921 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4922 | free_string_array(argvlist, *argc); |
| 4923 | return NULL; |
| 4924 | } |
| 4925 | #endif |
| 4926 | |
| 4927 | #ifdef HAVE_EXECV |
| 4928 | PyDoc_STRVAR(posix_execv__doc__, |
| 4929 | "execv(path, args)\n\n\ |
| 4930 | Execute an executable path with arguments, replacing current process.\n\ |
| 4931 | \n\ |
| 4932 | path: path of executable file\n\ |
| 4933 | args: tuple or list of strings"); |
| 4934 | |
| 4935 | static PyObject * |
| 4936 | posix_execv(PyObject *self, PyObject *args) |
| 4937 | { |
| 4938 | PyObject *opath; |
| 4939 | char *path; |
| 4940 | PyObject *argv; |
| 4941 | char **argvlist; |
| 4942 | Py_ssize_t argc; |
| 4943 | |
| 4944 | /* execv has two arguments: (path, argv), where |
| 4945 | argv is a list or tuple of strings. */ |
| 4946 | |
| 4947 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4948 | PyUnicode_FSConverter, |
| 4949 | &opath, &argv)) |
| 4950 | return NULL; |
| 4951 | path = PyBytes_AsString(opath); |
| 4952 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4953 | PyErr_SetString(PyExc_TypeError, |
| 4954 | "execv() arg 2 must be a tuple or list"); |
| 4955 | Py_DECREF(opath); |
| 4956 | return NULL; |
| 4957 | } |
| 4958 | argc = PySequence_Size(argv); |
| 4959 | if (argc < 1) { |
| 4960 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4961 | Py_DECREF(opath); |
| 4962 | return NULL; |
| 4963 | } |
| 4964 | |
| 4965 | argvlist = parse_arglist(argv, &argc); |
| 4966 | if (argvlist == NULL) { |
| 4967 | Py_DECREF(opath); |
| 4968 | return NULL; |
| 4969 | } |
| 4970 | |
| 4971 | execv(path, argvlist); |
| 4972 | |
| 4973 | /* If we get here it's definitely an error */ |
| 4974 | |
| 4975 | free_string_array(argvlist, argc); |
| 4976 | Py_DECREF(opath); |
| 4977 | return posix_error(); |
| 4978 | } |
| 4979 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4980 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4981 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4982 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4983 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4984 | path: path of executable file\n\ |
| 4985 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4986 | env: dictionary of strings mapping to strings\n\ |
| 4987 | \n\ |
| 4988 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 4989 | execve will execute the program the file descriptor is open to.\n\ |
| 4990 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4991 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4992 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4993 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4994 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4995 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4996 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4997 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4998 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4999 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5000 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5001 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5002 | /* execve has three arguments: (path, argv, env), where |
| 5003 | argv is a list or tuple of strings and env is a dictionary |
| 5004 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5005 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5006 | memset(&path, 0, sizeof(path)); |
| 5007 | #ifdef HAVE_FEXECVE |
| 5008 | path.allow_fd = 1; |
| 5009 | #endif |
| 5010 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 5011 | path_converter, &path, |
| 5012 | &argv, &env |
| 5013 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5014 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5015 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5016 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5017 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5018 | "execve: argv must be a tuple or list"); |
| 5019 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5020 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5021 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5022 | if (!PyMapping_Check(env)) { |
| 5023 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5024 | "execve: environment must be a mapping object"); |
| 5025 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5026 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5027 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5028 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5029 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5030 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5031 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5032 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5033 | envlist = parse_envlist(env, &envc); |
| 5034 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5035 | goto fail; |
| 5036 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5037 | #ifdef HAVE_FEXECVE |
| 5038 | if (path.fd > -1) |
| 5039 | fexecve(path.fd, argvlist, envlist); |
| 5040 | else |
| 5041 | #endif |
| 5042 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5043 | |
| 5044 | /* If we get here it's definitely an error */ |
| 5045 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5046 | path_posix_error("execve", &path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5047 | |
| 5048 | while (--envc >= 0) |
| 5049 | PyMem_DEL(envlist[envc]); |
| 5050 | PyMem_DEL(envlist); |
| 5051 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5052 | if (argvlist) |
| 5053 | free_string_array(argvlist, argc); |
| 5054 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5055 | return NULL; |
| 5056 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5057 | #endif /* HAVE_EXECV */ |
| 5058 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5059 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5060 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5061 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5062 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5063 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5064 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5065 | mode: mode of process creation\n\ |
| 5066 | path: path of executable file\n\ |
| 5067 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5068 | |
| 5069 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5070 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5071 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5072 | PyObject *opath; |
| 5073 | char *path; |
| 5074 | PyObject *argv; |
| 5075 | char **argvlist; |
| 5076 | int mode, i; |
| 5077 | Py_ssize_t argc; |
| 5078 | Py_intptr_t spawnval; |
| 5079 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5080 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5081 | /* spawnv has three arguments: (mode, path, argv), where |
| 5082 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5083 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5084 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 5085 | PyUnicode_FSConverter, |
| 5086 | &opath, &argv)) |
| 5087 | return NULL; |
| 5088 | path = PyBytes_AsString(opath); |
| 5089 | if (PyList_Check(argv)) { |
| 5090 | argc = PyList_Size(argv); |
| 5091 | getitem = PyList_GetItem; |
| 5092 | } |
| 5093 | else if (PyTuple_Check(argv)) { |
| 5094 | argc = PyTuple_Size(argv); |
| 5095 | getitem = PyTuple_GetItem; |
| 5096 | } |
| 5097 | else { |
| 5098 | PyErr_SetString(PyExc_TypeError, |
| 5099 | "spawnv() arg 2 must be a tuple or list"); |
| 5100 | Py_DECREF(opath); |
| 5101 | return NULL; |
| 5102 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5103 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5104 | argvlist = PyMem_NEW(char *, argc+1); |
| 5105 | if (argvlist == NULL) { |
| 5106 | Py_DECREF(opath); |
| 5107 | return PyErr_NoMemory(); |
| 5108 | } |
| 5109 | for (i = 0; i < argc; i++) { |
| 5110 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5111 | &argvlist[i])) { |
| 5112 | free_string_array(argvlist, i); |
| 5113 | PyErr_SetString( |
| 5114 | PyExc_TypeError, |
| 5115 | "spawnv() arg 2 must contain only strings"); |
| 5116 | Py_DECREF(opath); |
| 5117 | return NULL; |
| 5118 | } |
| 5119 | } |
| 5120 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5121 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5122 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5123 | Py_BEGIN_ALLOW_THREADS |
| 5124 | spawnval = spawnv(mode, path, argvlist); |
| 5125 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5126 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5127 | if (mode == _OLD_P_OVERLAY) |
| 5128 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5129 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5130 | Py_BEGIN_ALLOW_THREADS |
| 5131 | spawnval = _spawnv(mode, path, argvlist); |
| 5132 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5133 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5134 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5135 | free_string_array(argvlist, argc); |
| 5136 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5137 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5138 | if (spawnval == -1) |
| 5139 | return posix_error(); |
| 5140 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 5141 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5142 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5143 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5144 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5145 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5149 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5150 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5151 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5152 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5153 | mode: mode of process creation\n\ |
| 5154 | path: path of executable file\n\ |
| 5155 | args: tuple or list of arguments\n\ |
| 5156 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5157 | |
| 5158 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5159 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5160 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5161 | PyObject *opath; |
| 5162 | char *path; |
| 5163 | PyObject *argv, *env; |
| 5164 | char **argvlist; |
| 5165 | char **envlist; |
| 5166 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5167 | int mode; |
| 5168 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5169 | Py_intptr_t spawnval; |
| 5170 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5171 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5172 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5173 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5174 | argv is a list or tuple of strings and env is a dictionary |
| 5175 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5176 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5177 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 5178 | PyUnicode_FSConverter, |
| 5179 | &opath, &argv, &env)) |
| 5180 | return NULL; |
| 5181 | path = PyBytes_AsString(opath); |
| 5182 | if (PyList_Check(argv)) { |
| 5183 | argc = PyList_Size(argv); |
| 5184 | getitem = PyList_GetItem; |
| 5185 | } |
| 5186 | else if (PyTuple_Check(argv)) { |
| 5187 | argc = PyTuple_Size(argv); |
| 5188 | getitem = PyTuple_GetItem; |
| 5189 | } |
| 5190 | else { |
| 5191 | PyErr_SetString(PyExc_TypeError, |
| 5192 | "spawnve() arg 2 must be a tuple or list"); |
| 5193 | goto fail_0; |
| 5194 | } |
| 5195 | if (!PyMapping_Check(env)) { |
| 5196 | PyErr_SetString(PyExc_TypeError, |
| 5197 | "spawnve() arg 3 must be a mapping object"); |
| 5198 | goto fail_0; |
| 5199 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5200 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5201 | argvlist = PyMem_NEW(char *, argc+1); |
| 5202 | if (argvlist == NULL) { |
| 5203 | PyErr_NoMemory(); |
| 5204 | goto fail_0; |
| 5205 | } |
| 5206 | for (i = 0; i < argc; i++) { |
| 5207 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5208 | &argvlist[i])) |
| 5209 | { |
| 5210 | lastarg = i; |
| 5211 | goto fail_1; |
| 5212 | } |
| 5213 | } |
| 5214 | lastarg = argc; |
| 5215 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5216 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5217 | envlist = parse_envlist(env, &envc); |
| 5218 | if (envlist == NULL) |
| 5219 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5220 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5221 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5222 | Py_BEGIN_ALLOW_THREADS |
| 5223 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 5224 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5225 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5226 | if (mode == _OLD_P_OVERLAY) |
| 5227 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5228 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5229 | Py_BEGIN_ALLOW_THREADS |
| 5230 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 5231 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5232 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5233 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5234 | if (spawnval == -1) |
| 5235 | (void) posix_error(); |
| 5236 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 5237 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5238 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5239 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5240 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5241 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5242 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5243 | while (--envc >= 0) |
| 5244 | PyMem_DEL(envlist[envc]); |
| 5245 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5246 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5247 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5248 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5249 | Py_DECREF(opath); |
| 5250 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5251 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5252 | |
| 5253 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 5254 | #if defined(PYOS_OS2) |
| 5255 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 5256 | "spawnvp(mode, file, args)\n\n\ |
| 5257 | Execute the program 'file' in a new process, using the environment\n\ |
| 5258 | search path to find the file.\n\ |
| 5259 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5260 | mode: mode of process creation\n\ |
| 5261 | file: executable file name\n\ |
| 5262 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5263 | |
| 5264 | static PyObject * |
| 5265 | posix_spawnvp(PyObject *self, PyObject *args) |
| 5266 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5267 | PyObject *opath; |
| 5268 | char *path; |
| 5269 | PyObject *argv; |
| 5270 | char **argvlist; |
| 5271 | int mode, i, argc; |
| 5272 | Py_intptr_t spawnval; |
| 5273 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5274 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5275 | /* spawnvp has three arguments: (mode, path, argv), where |
| 5276 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5277 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5278 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 5279 | PyUnicode_FSConverter, |
| 5280 | &opath, &argv)) |
| 5281 | return NULL; |
| 5282 | path = PyBytes_AsString(opath); |
| 5283 | if (PyList_Check(argv)) { |
| 5284 | argc = PyList_Size(argv); |
| 5285 | getitem = PyList_GetItem; |
| 5286 | } |
| 5287 | else if (PyTuple_Check(argv)) { |
| 5288 | argc = PyTuple_Size(argv); |
| 5289 | getitem = PyTuple_GetItem; |
| 5290 | } |
| 5291 | else { |
| 5292 | PyErr_SetString(PyExc_TypeError, |
| 5293 | "spawnvp() arg 2 must be a tuple or list"); |
| 5294 | Py_DECREF(opath); |
| 5295 | return NULL; |
| 5296 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5297 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5298 | argvlist = PyMem_NEW(char *, argc+1); |
| 5299 | if (argvlist == NULL) { |
| 5300 | Py_DECREF(opath); |
| 5301 | return PyErr_NoMemory(); |
| 5302 | } |
| 5303 | for (i = 0; i < argc; i++) { |
| 5304 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5305 | &argvlist[i])) { |
| 5306 | free_string_array(argvlist, i); |
| 5307 | PyErr_SetString( |
| 5308 | PyExc_TypeError, |
| 5309 | "spawnvp() arg 2 must contain only strings"); |
| 5310 | Py_DECREF(opath); |
| 5311 | return NULL; |
| 5312 | } |
| 5313 | } |
| 5314 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5316 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5317 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5318 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5319 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5320 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5321 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5322 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5323 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5324 | free_string_array(argvlist, argc); |
| 5325 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5326 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5327 | if (spawnval == -1) |
| 5328 | return posix_error(); |
| 5329 | else |
| 5330 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | |
| 5334 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 5335 | "spawnvpe(mode, file, args, env)\n\n\ |
| 5336 | Execute the program 'file' in a new process, using the environment\n\ |
| 5337 | search path to find the file.\n\ |
| 5338 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5339 | mode: mode of process creation\n\ |
| 5340 | file: executable file name\n\ |
| 5341 | args: tuple or list of arguments\n\ |
| 5342 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5343 | |
| 5344 | static PyObject * |
| 5345 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 5346 | { |
Ross Lagerwall | dcfde5a | 2011-11-04 07:09:14 +0200 | [diff] [blame] | 5347 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5348 | char *path; |
| 5349 | PyObject *argv, *env; |
| 5350 | char **argvlist; |
| 5351 | char **envlist; |
| 5352 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5353 | int mode; |
| 5354 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5355 | Py_intptr_t spawnval; |
| 5356 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5357 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5358 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5359 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 5360 | argv is a list or tuple of strings and env is a dictionary |
| 5361 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5362 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5363 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 5364 | PyUnicode_FSConverter, |
| 5365 | &opath, &argv, &env)) |
| 5366 | return NULL; |
| 5367 | path = PyBytes_AsString(opath); |
| 5368 | if (PyList_Check(argv)) { |
| 5369 | argc = PyList_Size(argv); |
| 5370 | getitem = PyList_GetItem; |
| 5371 | } |
| 5372 | else if (PyTuple_Check(argv)) { |
| 5373 | argc = PyTuple_Size(argv); |
| 5374 | getitem = PyTuple_GetItem; |
| 5375 | } |
| 5376 | else { |
| 5377 | PyErr_SetString(PyExc_TypeError, |
| 5378 | "spawnvpe() arg 2 must be a tuple or list"); |
| 5379 | goto fail_0; |
| 5380 | } |
| 5381 | if (!PyMapping_Check(env)) { |
| 5382 | PyErr_SetString(PyExc_TypeError, |
| 5383 | "spawnvpe() arg 3 must be a mapping object"); |
| 5384 | goto fail_0; |
| 5385 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5387 | argvlist = PyMem_NEW(char *, argc+1); |
| 5388 | if (argvlist == NULL) { |
| 5389 | PyErr_NoMemory(); |
| 5390 | goto fail_0; |
| 5391 | } |
| 5392 | for (i = 0; i < argc; i++) { |
| 5393 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5394 | &argvlist[i])) |
| 5395 | { |
| 5396 | lastarg = i; |
| 5397 | goto fail_1; |
| 5398 | } |
| 5399 | } |
| 5400 | lastarg = argc; |
| 5401 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5402 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5403 | envlist = parse_envlist(env, &envc); |
| 5404 | if (envlist == NULL) |
| 5405 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5406 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5407 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5408 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5409 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5410 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5411 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5412 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5413 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5415 | if (spawnval == -1) |
| 5416 | (void) posix_error(); |
| 5417 | else |
| 5418 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5419 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5420 | while (--envc >= 0) |
| 5421 | PyMem_DEL(envlist[envc]); |
| 5422 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5423 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5424 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5425 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5426 | Py_DECREF(opath); |
| 5427 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5428 | } |
| 5429 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5430 | #endif /* HAVE_SPAWNV */ |
| 5431 | |
| 5432 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5433 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5434 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5435 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5436 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 5437 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5438 | 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] | 5439 | |
| 5440 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5441 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5442 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5443 | pid_t pid; |
| 5444 | int result = 0; |
| 5445 | _PyImport_AcquireLock(); |
| 5446 | pid = fork1(); |
| 5447 | if (pid == 0) { |
| 5448 | /* child: this clobbers and resets the import lock. */ |
| 5449 | PyOS_AfterFork(); |
| 5450 | } else { |
| 5451 | /* parent: release the import lock. */ |
| 5452 | result = _PyImport_ReleaseLock(); |
| 5453 | } |
| 5454 | if (pid == -1) |
| 5455 | return posix_error(); |
| 5456 | if (result < 0) { |
| 5457 | /* Don't clobber the OSError if the fork failed. */ |
| 5458 | PyErr_SetString(PyExc_RuntimeError, |
| 5459 | "not holding the import lock"); |
| 5460 | return NULL; |
| 5461 | } |
| 5462 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5463 | } |
| 5464 | #endif |
| 5465 | |
| 5466 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5467 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5468 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5469 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5470 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5471 | 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] | 5472 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5473 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5474 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5475 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5476 | pid_t pid; |
| 5477 | int result = 0; |
| 5478 | _PyImport_AcquireLock(); |
| 5479 | pid = fork(); |
| 5480 | if (pid == 0) { |
| 5481 | /* child: this clobbers and resets the import lock. */ |
| 5482 | PyOS_AfterFork(); |
| 5483 | } else { |
| 5484 | /* parent: release the import lock. */ |
| 5485 | result = _PyImport_ReleaseLock(); |
| 5486 | } |
| 5487 | if (pid == -1) |
| 5488 | return posix_error(); |
| 5489 | if (result < 0) { |
| 5490 | /* Don't clobber the OSError if the fork failed. */ |
| 5491 | PyErr_SetString(PyExc_RuntimeError, |
| 5492 | "not holding the import lock"); |
| 5493 | return NULL; |
| 5494 | } |
| 5495 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5496 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5497 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5498 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5499 | #ifdef HAVE_SCHED_H |
| 5500 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5501 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5502 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5503 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5504 | "sched_get_priority_max(policy)\n\n\ |
| 5505 | Get the maximum scheduling priority for *policy*."); |
| 5506 | |
| 5507 | static PyObject * |
| 5508 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5509 | { |
| 5510 | int policy, max; |
| 5511 | |
| 5512 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5513 | return NULL; |
| 5514 | max = sched_get_priority_max(policy); |
| 5515 | if (max < 0) |
| 5516 | return posix_error(); |
| 5517 | return PyLong_FromLong(max); |
| 5518 | } |
| 5519 | |
| 5520 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5521 | "sched_get_priority_min(policy)\n\n\ |
| 5522 | Get the minimum scheduling priority for *policy*."); |
| 5523 | |
| 5524 | static PyObject * |
| 5525 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5526 | { |
| 5527 | int policy, min; |
| 5528 | |
| 5529 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5530 | return NULL; |
| 5531 | min = sched_get_priority_min(policy); |
| 5532 | if (min < 0) |
| 5533 | return posix_error(); |
| 5534 | return PyLong_FromLong(min); |
| 5535 | } |
| 5536 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5537 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5538 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5539 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5540 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5541 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5542 | "sched_getscheduler(pid)\n\n\ |
| 5543 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5544 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5545 | |
| 5546 | static PyObject * |
| 5547 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5548 | { |
| 5549 | pid_t pid; |
| 5550 | int policy; |
| 5551 | |
| 5552 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5553 | return NULL; |
| 5554 | policy = sched_getscheduler(pid); |
| 5555 | if (policy < 0) |
| 5556 | return posix_error(); |
| 5557 | return PyLong_FromLong(policy); |
| 5558 | } |
| 5559 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5560 | #endif |
| 5561 | |
| 5562 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5563 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5564 | static PyObject * |
| 5565 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5566 | { |
| 5567 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5568 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5569 | |
| 5570 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5571 | return NULL; |
| 5572 | res = PyStructSequence_New(type); |
| 5573 | if (!res) |
| 5574 | return NULL; |
| 5575 | Py_INCREF(priority); |
| 5576 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5577 | return res; |
| 5578 | } |
| 5579 | |
| 5580 | PyDoc_STRVAR(sched_param__doc__, |
| 5581 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5582 | Current has only one field: sched_priority"); |
| 5583 | |
| 5584 | static PyStructSequence_Field sched_param_fields[] = { |
| 5585 | {"sched_priority", "the scheduling priority"}, |
| 5586 | {0} |
| 5587 | }; |
| 5588 | |
| 5589 | static PyStructSequence_Desc sched_param_desc = { |
| 5590 | "sched_param", /* name */ |
| 5591 | sched_param__doc__, /* doc */ |
| 5592 | sched_param_fields, |
| 5593 | 1 |
| 5594 | }; |
| 5595 | |
| 5596 | static int |
| 5597 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5598 | { |
| 5599 | long priority; |
| 5600 | |
| 5601 | if (Py_TYPE(param) != &SchedParamType) { |
| 5602 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5603 | return 0; |
| 5604 | } |
| 5605 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5606 | if (priority == -1 && PyErr_Occurred()) |
| 5607 | return 0; |
| 5608 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5609 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5610 | return 0; |
| 5611 | } |
| 5612 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5613 | return 1; |
| 5614 | } |
| 5615 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5616 | #endif |
| 5617 | |
| 5618 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5619 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5620 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5621 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5622 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5623 | If *pid* is 0, the calling process is changed.\n\ |
| 5624 | *param* is an instance of sched_param."); |
| 5625 | |
| 5626 | static PyObject * |
| 5627 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5628 | { |
| 5629 | pid_t pid; |
| 5630 | int policy; |
| 5631 | struct sched_param param; |
| 5632 | |
| 5633 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5634 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5635 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5636 | |
| 5637 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5638 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5639 | ** scheduling policy under Solaris/Illumos, and others. |
| 5640 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5641 | */ |
| 5642 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5643 | return posix_error(); |
| 5644 | Py_RETURN_NONE; |
| 5645 | } |
| 5646 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5647 | #endif |
| 5648 | |
| 5649 | #ifdef HAVE_SCHED_SETPARAM |
| 5650 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5651 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5652 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5653 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5654 | sched_param class. A PID of 0 means the calling process."); |
| 5655 | |
| 5656 | static PyObject * |
| 5657 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5658 | { |
| 5659 | pid_t pid; |
| 5660 | struct sched_param param; |
| 5661 | PyObject *res, *priority; |
| 5662 | |
| 5663 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5664 | return NULL; |
| 5665 | if (sched_getparam(pid, ¶m)) |
| 5666 | return posix_error(); |
| 5667 | res = PyStructSequence_New(&SchedParamType); |
| 5668 | if (!res) |
| 5669 | return NULL; |
| 5670 | priority = PyLong_FromLong(param.sched_priority); |
| 5671 | if (!priority) { |
| 5672 | Py_DECREF(res); |
| 5673 | return NULL; |
| 5674 | } |
| 5675 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5676 | return res; |
| 5677 | } |
| 5678 | |
| 5679 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5680 | "sched_setparam(pid, param)\n\n\ |
| 5681 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5682 | A PID of 0 means the calling process."); |
| 5683 | |
| 5684 | static PyObject * |
| 5685 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5686 | { |
| 5687 | pid_t pid; |
| 5688 | struct sched_param param; |
| 5689 | |
| 5690 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5691 | &pid, &convert_sched_param, ¶m)) |
| 5692 | return NULL; |
| 5693 | if (sched_setparam(pid, ¶m)) |
| 5694 | return posix_error(); |
| 5695 | Py_RETURN_NONE; |
| 5696 | } |
| 5697 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5698 | #endif |
| 5699 | |
| 5700 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5701 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5702 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5703 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5704 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5705 | |
| 5706 | static PyObject * |
| 5707 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5708 | { |
| 5709 | pid_t pid; |
| 5710 | struct timespec interval; |
| 5711 | |
| 5712 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5713 | return NULL; |
| 5714 | if (sched_rr_get_interval(pid, &interval)) |
| 5715 | return posix_error(); |
| 5716 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5717 | } |
| 5718 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5719 | #endif |
| 5720 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5721 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5722 | "sched_yield()\n\n\ |
| 5723 | Voluntarily relinquish the CPU."); |
| 5724 | |
| 5725 | static PyObject * |
| 5726 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5727 | { |
| 5728 | if (sched_yield()) |
| 5729 | return posix_error(); |
| 5730 | Py_RETURN_NONE; |
| 5731 | } |
| 5732 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5733 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5734 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5735 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 5736 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5737 | |
| 5738 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5739 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5740 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5741 | |
| 5742 | static PyObject * |
| 5743 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5744 | { |
| 5745 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5746 | int ncpus; |
| 5747 | size_t setsize; |
| 5748 | cpu_set_t *mask = NULL; |
| 5749 | PyObject *iterable, *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5750 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5751 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity", |
| 5752 | &pid, &iterable)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5753 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5754 | |
| 5755 | iterator = PyObject_GetIter(iterable); |
| 5756 | if (iterator == NULL) |
| 5757 | return NULL; |
| 5758 | |
| 5759 | ncpus = NCPUS_START; |
| 5760 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5761 | mask = CPU_ALLOC(ncpus); |
| 5762 | if (mask == NULL) { |
| 5763 | PyErr_NoMemory(); |
| 5764 | goto error; |
| 5765 | } |
| 5766 | CPU_ZERO_S(setsize, mask); |
| 5767 | |
| 5768 | while ((item = PyIter_Next(iterator))) { |
| 5769 | long cpu; |
| 5770 | if (!PyLong_Check(item)) { |
| 5771 | PyErr_Format(PyExc_TypeError, |
| 5772 | "expected an iterator of ints, " |
| 5773 | "but iterator yielded %R", |
| 5774 | Py_TYPE(item)); |
| 5775 | Py_DECREF(item); |
| 5776 | goto error; |
| 5777 | } |
| 5778 | cpu = PyLong_AsLong(item); |
| 5779 | Py_DECREF(item); |
| 5780 | if (cpu < 0) { |
| 5781 | if (!PyErr_Occurred()) |
| 5782 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 5783 | goto error; |
| 5784 | } |
| 5785 | if (cpu > INT_MAX - 1) { |
| 5786 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 5787 | goto error; |
| 5788 | } |
| 5789 | if (cpu >= ncpus) { |
| 5790 | /* Grow CPU mask to fit the CPU number */ |
| 5791 | int newncpus = ncpus; |
| 5792 | cpu_set_t *newmask; |
| 5793 | size_t newsetsize; |
| 5794 | while (newncpus <= cpu) { |
| 5795 | if (newncpus > INT_MAX / 2) |
| 5796 | newncpus = cpu + 1; |
| 5797 | else |
| 5798 | newncpus = newncpus * 2; |
| 5799 | } |
| 5800 | newmask = CPU_ALLOC(newncpus); |
| 5801 | if (newmask == NULL) { |
| 5802 | PyErr_NoMemory(); |
| 5803 | goto error; |
| 5804 | } |
| 5805 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 5806 | CPU_ZERO_S(newsetsize, newmask); |
| 5807 | memcpy(newmask, mask, setsize); |
| 5808 | CPU_FREE(mask); |
| 5809 | setsize = newsetsize; |
| 5810 | mask = newmask; |
| 5811 | ncpus = newncpus; |
| 5812 | } |
| 5813 | CPU_SET_S(cpu, setsize, mask); |
| 5814 | } |
| 5815 | Py_CLEAR(iterator); |
| 5816 | |
| 5817 | if (sched_setaffinity(pid, setsize, mask)) { |
| 5818 | posix_error(); |
| 5819 | goto error; |
| 5820 | } |
| 5821 | CPU_FREE(mask); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5822 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5823 | |
| 5824 | error: |
| 5825 | if (mask) |
| 5826 | CPU_FREE(mask); |
| 5827 | Py_XDECREF(iterator); |
| 5828 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5832 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5833 | Return the affinity of the process with PID *pid*.\n\ |
| 5834 | The returned cpu_set will be of size *ncpus*."); |
| 5835 | |
| 5836 | static PyObject * |
| 5837 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5838 | { |
| 5839 | pid_t pid; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5840 | int cpu, ncpus, count; |
| 5841 | size_t setsize; |
| 5842 | cpu_set_t *mask = NULL; |
| 5843 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5844 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5845 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity", |
| 5846 | &pid)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5847 | return NULL; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5848 | |
| 5849 | ncpus = NCPUS_START; |
| 5850 | while (1) { |
| 5851 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 5852 | mask = CPU_ALLOC(ncpus); |
| 5853 | if (mask == NULL) |
| 5854 | return PyErr_NoMemory(); |
| 5855 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 5856 | break; |
| 5857 | CPU_FREE(mask); |
| 5858 | if (errno != EINVAL) |
| 5859 | return posix_error(); |
| 5860 | if (ncpus > INT_MAX / 2) { |
| 5861 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 5862 | "a large enough CPU set"); |
| 5863 | return NULL; |
| 5864 | } |
| 5865 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5866 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 5867 | |
| 5868 | res = PySet_New(NULL); |
| 5869 | if (res == NULL) |
| 5870 | goto error; |
| 5871 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 5872 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 5873 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 5874 | --count; |
| 5875 | if (cpu_num == NULL) |
| 5876 | goto error; |
| 5877 | if (PySet_Add(res, cpu_num)) { |
| 5878 | Py_DECREF(cpu_num); |
| 5879 | goto error; |
| 5880 | } |
| 5881 | Py_DECREF(cpu_num); |
| 5882 | } |
| 5883 | } |
| 5884 | CPU_FREE(mask); |
| 5885 | return res; |
| 5886 | |
| 5887 | error: |
| 5888 | if (mask) |
| 5889 | CPU_FREE(mask); |
| 5890 | Py_XDECREF(res); |
| 5891 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5892 | } |
| 5893 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5894 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5895 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5896 | #endif /* HAVE_SCHED_H */ |
| 5897 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5898 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5899 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5900 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5901 | #define DEV_PTY_FILE "/dev/ptc" |
| 5902 | #define HAVE_DEV_PTMX |
| 5903 | #else |
| 5904 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5905 | #endif |
| 5906 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5907 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5908 | #ifdef HAVE_PTY_H |
| 5909 | #include <pty.h> |
| 5910 | #else |
| 5911 | #ifdef HAVE_LIBUTIL_H |
| 5912 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5913 | #else |
| 5914 | #ifdef HAVE_UTIL_H |
| 5915 | #include <util.h> |
| 5916 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5917 | #endif /* HAVE_LIBUTIL_H */ |
| 5918 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5919 | #ifdef HAVE_STROPTS_H |
| 5920 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5921 | #endif |
| 5922 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5923 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5924 | #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] | 5925 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5926 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5927 | 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] | 5928 | |
| 5929 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5930 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5931 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5932 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5933 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5934 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5935 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5936 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5937 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5938 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5939 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5940 | #endif |
| 5941 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5942 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5943 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5944 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5945 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5946 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5947 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5948 | if (slave_name == NULL) |
| 5949 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5950 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5951 | slave_fd = open(slave_name, O_RDWR); |
| 5952 | if (slave_fd < 0) |
| 5953 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5954 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5955 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5956 | if (master_fd < 0) |
| 5957 | return posix_error(); |
| 5958 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5959 | /* change permission of slave */ |
| 5960 | if (grantpt(master_fd) < 0) { |
| 5961 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5962 | return posix_error(); |
| 5963 | } |
| 5964 | /* unlock slave */ |
| 5965 | if (unlockpt(master_fd) < 0) { |
| 5966 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5967 | return posix_error(); |
| 5968 | } |
| 5969 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5970 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5971 | if (slave_name == NULL) |
| 5972 | return posix_error(); |
| 5973 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5974 | if (slave_fd < 0) |
| 5975 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5976 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5977 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5978 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5979 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5980 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5981 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5982 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5983 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5984 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5985 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5986 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5987 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5988 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5989 | |
| 5990 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5991 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5992 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5993 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5994 | 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] | 5995 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5996 | |
| 5997 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5998 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5999 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6000 | int master_fd = -1, result = 0; |
| 6001 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6002 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6003 | _PyImport_AcquireLock(); |
| 6004 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6005 | if (pid == 0) { |
| 6006 | /* child: this clobbers and resets the import lock. */ |
| 6007 | PyOS_AfterFork(); |
| 6008 | } else { |
| 6009 | /* parent: release the import lock. */ |
| 6010 | result = _PyImport_ReleaseLock(); |
| 6011 | } |
| 6012 | if (pid == -1) |
| 6013 | return posix_error(); |
| 6014 | if (result < 0) { |
| 6015 | /* Don't clobber the OSError if the fork failed. */ |
| 6016 | PyErr_SetString(PyExc_RuntimeError, |
| 6017 | "not holding the import lock"); |
| 6018 | return NULL; |
| 6019 | } |
| 6020 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6021 | } |
| 6022 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6023 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6024 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6025 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6026 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6027 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6028 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6029 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6030 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6031 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6032 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6033 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6034 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6035 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6036 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6037 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6038 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6039 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6040 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6041 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6042 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6043 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6044 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6045 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6046 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6047 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6048 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6049 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6050 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6051 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6052 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6053 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6054 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6055 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6056 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6057 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6058 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6059 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6060 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6061 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6062 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6063 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6064 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6065 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6066 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6067 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6068 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6069 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6070 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6071 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6072 | } |
| 6073 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6074 | #ifdef HAVE_GETGROUPLIST |
| 6075 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6076 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6077 | Returns a list of groups to which a user belongs.\n\n\ |
| 6078 | user: username to lookup\n\ |
| 6079 | group: base group id of the user"); |
| 6080 | |
| 6081 | static PyObject * |
| 6082 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6083 | { |
| 6084 | #ifdef NGROUPS_MAX |
| 6085 | #define MAX_GROUPS NGROUPS_MAX |
| 6086 | #else |
| 6087 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6088 | #define MAX_GROUPS 64 |
| 6089 | #endif |
| 6090 | |
| 6091 | const char *user; |
| 6092 | int i, ngroups; |
| 6093 | PyObject *list; |
| 6094 | #ifdef __APPLE__ |
| 6095 | int *groups, basegid; |
| 6096 | #else |
| 6097 | gid_t *groups, basegid; |
| 6098 | #endif |
| 6099 | ngroups = MAX_GROUPS; |
| 6100 | |
| 6101 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 6102 | return NULL; |
| 6103 | |
| 6104 | #ifdef __APPLE__ |
| 6105 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 6106 | #else |
| 6107 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 6108 | #endif |
| 6109 | if (groups == NULL) |
| 6110 | return PyErr_NoMemory(); |
| 6111 | |
| 6112 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6113 | PyMem_Del(groups); |
| 6114 | return posix_error(); |
| 6115 | } |
| 6116 | |
| 6117 | list = PyList_New(ngroups); |
| 6118 | if (list == NULL) { |
| 6119 | PyMem_Del(groups); |
| 6120 | return NULL; |
| 6121 | } |
| 6122 | |
| 6123 | for (i = 0; i < ngroups; i++) { |
| 6124 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 6125 | if (o == NULL) { |
| 6126 | Py_DECREF(list); |
| 6127 | PyMem_Del(groups); |
| 6128 | return NULL; |
| 6129 | } |
| 6130 | PyList_SET_ITEM(list, i, o); |
| 6131 | } |
| 6132 | |
| 6133 | PyMem_Del(groups); |
| 6134 | |
| 6135 | return list; |
| 6136 | } |
| 6137 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6138 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6139 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6140 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6141 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6142 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6143 | |
| 6144 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6145 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6146 | { |
| 6147 | PyObject *result = NULL; |
| 6148 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6149 | #ifdef NGROUPS_MAX |
| 6150 | #define MAX_GROUPS NGROUPS_MAX |
| 6151 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6152 | /* 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] | 6153 | #define MAX_GROUPS 64 |
| 6154 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6155 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6156 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6157 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6158 | * This is a helper variable to store the intermediate result when |
| 6159 | * that happens. |
| 6160 | * |
| 6161 | * To keep the code readable the OSX behaviour is unconditional, |
| 6162 | * according to the POSIX spec this should be safe on all unix-y |
| 6163 | * systems. |
| 6164 | */ |
| 6165 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6166 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6167 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6168 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6169 | if (n < 0) { |
| 6170 | if (errno == EINVAL) { |
| 6171 | n = getgroups(0, NULL); |
| 6172 | if (n == -1) { |
| 6173 | return posix_error(); |
| 6174 | } |
| 6175 | if (n == 0) { |
| 6176 | /* Avoid malloc(0) */ |
| 6177 | alt_grouplist = grouplist; |
| 6178 | } else { |
| 6179 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6180 | if (alt_grouplist == NULL) { |
| 6181 | errno = EINVAL; |
| 6182 | return posix_error(); |
| 6183 | } |
| 6184 | n = getgroups(n, alt_grouplist); |
| 6185 | if (n == -1) { |
| 6186 | PyMem_Free(alt_grouplist); |
| 6187 | return posix_error(); |
| 6188 | } |
| 6189 | } |
| 6190 | } else { |
| 6191 | return posix_error(); |
| 6192 | } |
| 6193 | } |
| 6194 | result = PyList_New(n); |
| 6195 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6196 | int i; |
| 6197 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6198 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6199 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6200 | Py_DECREF(result); |
| 6201 | result = NULL; |
| 6202 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6203 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6204 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6205 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | if (alt_grouplist != grouplist) { |
| 6209 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6210 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6211 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6212 | return result; |
| 6213 | } |
| 6214 | #endif |
| 6215 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6216 | #ifdef HAVE_INITGROUPS |
| 6217 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6218 | "initgroups(username, gid) -> None\n\n\ |
| 6219 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6220 | the groups of which the specified username is a member, plus the specified\n\ |
| 6221 | group id."); |
| 6222 | |
| 6223 | static PyObject * |
| 6224 | posix_initgroups(PyObject *self, PyObject *args) |
| 6225 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6226 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6227 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6228 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6229 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6230 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6231 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 6232 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6233 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6234 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6235 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6236 | res = initgroups(username, (gid_t) gid); |
| 6237 | Py_DECREF(oname); |
| 6238 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6239 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6240 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6241 | Py_INCREF(Py_None); |
| 6242 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6243 | } |
| 6244 | #endif |
| 6245 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6246 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6247 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6248 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6249 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6250 | |
| 6251 | static PyObject * |
| 6252 | posix_getpgid(PyObject *self, PyObject *args) |
| 6253 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6254 | pid_t pid, pgid; |
| 6255 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 6256 | return NULL; |
| 6257 | pgid = getpgid(pid); |
| 6258 | if (pgid < 0) |
| 6259 | return posix_error(); |
| 6260 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6261 | } |
| 6262 | #endif /* HAVE_GETPGID */ |
| 6263 | |
| 6264 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6265 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6266 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6267 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6268 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6269 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6270 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6271 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6272 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6273 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6274 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6275 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6276 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6277 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6278 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6279 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6280 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6281 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6282 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6283 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6284 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 6285 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6286 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6287 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6288 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6289 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6290 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6291 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6292 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6293 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6294 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6295 | return posix_error(); |
| 6296 | Py_INCREF(Py_None); |
| 6297 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6300 | #endif /* HAVE_SETPGRP */ |
| 6301 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6302 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6303 | |
| 6304 | #ifdef MS_WINDOWS |
| 6305 | #include <tlhelp32.h> |
| 6306 | |
| 6307 | static PyObject* |
| 6308 | win32_getppid() |
| 6309 | { |
| 6310 | HANDLE snapshot; |
| 6311 | pid_t mypid; |
| 6312 | PyObject* result = NULL; |
| 6313 | BOOL have_record; |
| 6314 | PROCESSENTRY32 pe; |
| 6315 | |
| 6316 | mypid = getpid(); /* This function never fails */ |
| 6317 | |
| 6318 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6319 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6320 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6321 | |
| 6322 | pe.dwSize = sizeof(pe); |
| 6323 | have_record = Process32First(snapshot, &pe); |
| 6324 | while (have_record) { |
| 6325 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6326 | /* We could cache the ulong value in a static variable. */ |
| 6327 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6328 | break; |
| 6329 | } |
| 6330 | |
| 6331 | have_record = Process32Next(snapshot, &pe); |
| 6332 | } |
| 6333 | |
| 6334 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6335 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6336 | * error anyway, so let's raise it. */ |
| 6337 | if (!result) |
| 6338 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6339 | |
| 6340 | CloseHandle(snapshot); |
| 6341 | |
| 6342 | return result; |
| 6343 | } |
| 6344 | #endif /*MS_WINDOWS*/ |
| 6345 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6346 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6347 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6348 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6349 | Windows machines will still return its id; others systems will return the id\n\ |
| 6350 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6351 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6352 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6353 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6354 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6355 | #ifdef MS_WINDOWS |
| 6356 | return win32_getppid(); |
| 6357 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6358 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6359 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6360 | } |
| 6361 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6362 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6363 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6364 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6365 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6366 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6367 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6368 | |
| 6369 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6370 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6371 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6372 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6373 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6374 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6375 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6376 | |
| 6377 | if (GetUserNameW(user_name, &num_chars)) { |
| 6378 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6379 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6380 | } |
| 6381 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6382 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6383 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6384 | char *name; |
| 6385 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6387 | errno = 0; |
| 6388 | name = getlogin(); |
| 6389 | if (name == NULL) { |
| 6390 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6391 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6392 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6393 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6394 | } |
| 6395 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6396 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6397 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6398 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6399 | return result; |
| 6400 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6401 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6402 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6403 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6404 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6405 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6406 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6407 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6408 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6409 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6410 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6411 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6412 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6413 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6414 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6415 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6416 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6417 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6418 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6419 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6420 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6421 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6422 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6423 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6424 | pid_t pid; |
| 6425 | int sig; |
| 6426 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6427 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6428 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6429 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 6430 | APIRET rc; |
| 6431 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6432 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6433 | |
| 6434 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 6435 | APIRET rc; |
| 6436 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6437 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6438 | |
| 6439 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 6440 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6441 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6442 | if (kill(pid, sig) == -1) |
| 6443 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6444 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6445 | Py_INCREF(Py_None); |
| 6446 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6447 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6448 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6449 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6450 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6451 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6452 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6453 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6454 | |
| 6455 | static PyObject * |
| 6456 | posix_killpg(PyObject *self, PyObject *args) |
| 6457 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6458 | int sig; |
| 6459 | pid_t pgid; |
| 6460 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6461 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6462 | take the same type. Moreover, pid_t is always at least as wide as |
| 6463 | int (else compilation of this module fails), which is safe. */ |
| 6464 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6465 | return NULL; |
| 6466 | if (killpg(pgid, sig) == -1) |
| 6467 | return posix_error(); |
| 6468 | Py_INCREF(Py_None); |
| 6469 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6470 | } |
| 6471 | #endif |
| 6472 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6473 | #ifdef MS_WINDOWS |
| 6474 | PyDoc_STRVAR(win32_kill__doc__, |
| 6475 | "kill(pid, sig)\n\n\ |
| 6476 | Kill a process with a signal."); |
| 6477 | |
| 6478 | static PyObject * |
| 6479 | win32_kill(PyObject *self, PyObject *args) |
| 6480 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6481 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6482 | DWORD pid, sig, err; |
| 6483 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6484 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6485 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 6486 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6487 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6488 | /* Console processes which share a common console can be sent CTRL+C or |
| 6489 | CTRL+BREAK events, provided they handle said events. */ |
| 6490 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 6491 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 6492 | err = GetLastError(); |
| 6493 | PyErr_SetFromWindowsErr(err); |
| 6494 | } |
| 6495 | else |
| 6496 | Py_RETURN_NONE; |
| 6497 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6498 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6499 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6500 | attempt to open and terminate the process. */ |
| 6501 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 6502 | if (handle == NULL) { |
| 6503 | err = GetLastError(); |
| 6504 | return PyErr_SetFromWindowsErr(err); |
| 6505 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6506 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6507 | if (TerminateProcess(handle, sig) == 0) { |
| 6508 | err = GetLastError(); |
| 6509 | result = PyErr_SetFromWindowsErr(err); |
| 6510 | } else { |
| 6511 | Py_INCREF(Py_None); |
| 6512 | result = Py_None; |
| 6513 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6514 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6515 | CloseHandle(handle); |
| 6516 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6517 | } |
| 6518 | #endif /* MS_WINDOWS */ |
| 6519 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6520 | #ifdef HAVE_PLOCK |
| 6521 | |
| 6522 | #ifdef HAVE_SYS_LOCK_H |
| 6523 | #include <sys/lock.h> |
| 6524 | #endif |
| 6525 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6526 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6527 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6528 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6529 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6530 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6531 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6532 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6533 | int op; |
| 6534 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6535 | return NULL; |
| 6536 | if (plock(op) == -1) |
| 6537 | return posix_error(); |
| 6538 | Py_INCREF(Py_None); |
| 6539 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6540 | } |
| 6541 | #endif |
| 6542 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6543 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6544 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6545 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6546 | Set the current process's user id."); |
| 6547 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6548 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6549 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6550 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6551 | long uid_arg; |
| 6552 | uid_t uid; |
| 6553 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 6554 | return NULL; |
| 6555 | uid = uid_arg; |
| 6556 | if (uid != uid_arg) { |
| 6557 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6558 | return NULL; |
| 6559 | } |
| 6560 | if (setuid(uid) < 0) |
| 6561 | return posix_error(); |
| 6562 | Py_INCREF(Py_None); |
| 6563 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6564 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6565 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6566 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6567 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6568 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6569 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6570 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6571 | Set the current process's effective user id."); |
| 6572 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6573 | static PyObject * |
| 6574 | posix_seteuid (PyObject *self, PyObject *args) |
| 6575 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6576 | long euid_arg; |
| 6577 | uid_t euid; |
| 6578 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 6579 | return NULL; |
| 6580 | euid = euid_arg; |
| 6581 | if (euid != euid_arg) { |
| 6582 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6583 | return NULL; |
| 6584 | } |
| 6585 | if (seteuid(euid) < 0) { |
| 6586 | return posix_error(); |
| 6587 | } else { |
| 6588 | Py_INCREF(Py_None); |
| 6589 | return Py_None; |
| 6590 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6591 | } |
| 6592 | #endif /* HAVE_SETEUID */ |
| 6593 | |
| 6594 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6595 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6596 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6597 | Set the current process's effective group id."); |
| 6598 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6599 | static PyObject * |
| 6600 | posix_setegid (PyObject *self, PyObject *args) |
| 6601 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6602 | long egid_arg; |
| 6603 | gid_t egid; |
| 6604 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 6605 | return NULL; |
| 6606 | egid = egid_arg; |
| 6607 | if (egid != egid_arg) { |
| 6608 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6609 | return NULL; |
| 6610 | } |
| 6611 | if (setegid(egid) < 0) { |
| 6612 | return posix_error(); |
| 6613 | } else { |
| 6614 | Py_INCREF(Py_None); |
| 6615 | return Py_None; |
| 6616 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6617 | } |
| 6618 | #endif /* HAVE_SETEGID */ |
| 6619 | |
| 6620 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6621 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6622 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6623 | Set the current process's real and effective user ids."); |
| 6624 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6625 | static PyObject * |
| 6626 | posix_setreuid (PyObject *self, PyObject *args) |
| 6627 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6628 | long ruid_arg, euid_arg; |
| 6629 | uid_t ruid, euid; |
| 6630 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 6631 | return NULL; |
| 6632 | if (ruid_arg == -1) |
| 6633 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 6634 | else |
| 6635 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 6636 | if (euid_arg == -1) |
| 6637 | euid = (uid_t)-1; |
| 6638 | else |
| 6639 | euid = euid_arg; |
| 6640 | if ((euid_arg != -1 && euid != euid_arg) || |
| 6641 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 6642 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6643 | return NULL; |
| 6644 | } |
| 6645 | if (setreuid(ruid, euid) < 0) { |
| 6646 | return posix_error(); |
| 6647 | } else { |
| 6648 | Py_INCREF(Py_None); |
| 6649 | return Py_None; |
| 6650 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6651 | } |
| 6652 | #endif /* HAVE_SETREUID */ |
| 6653 | |
| 6654 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6655 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6656 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6657 | Set the current process's real and effective group ids."); |
| 6658 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6659 | static PyObject * |
| 6660 | posix_setregid (PyObject *self, PyObject *args) |
| 6661 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6662 | long rgid_arg, egid_arg; |
| 6663 | gid_t rgid, egid; |
| 6664 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 6665 | return NULL; |
| 6666 | if (rgid_arg == -1) |
| 6667 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 6668 | else |
| 6669 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6670 | if (egid_arg == -1) |
| 6671 | egid = (gid_t)-1; |
| 6672 | else |
| 6673 | egid = egid_arg; |
| 6674 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6675 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6676 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6677 | return NULL; |
| 6678 | } |
| 6679 | if (setregid(rgid, egid) < 0) { |
| 6680 | return posix_error(); |
| 6681 | } else { |
| 6682 | Py_INCREF(Py_None); |
| 6683 | return Py_None; |
| 6684 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6685 | } |
| 6686 | #endif /* HAVE_SETREGID */ |
| 6687 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6688 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6689 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6690 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6691 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6692 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6693 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6694 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6695 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6696 | long gid_arg; |
| 6697 | gid_t gid; |
| 6698 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6699 | return NULL; |
| 6700 | gid = gid_arg; |
| 6701 | if (gid != gid_arg) { |
| 6702 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6703 | return NULL; |
| 6704 | } |
| 6705 | if (setgid(gid) < 0) |
| 6706 | return posix_error(); |
| 6707 | Py_INCREF(Py_None); |
| 6708 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6709 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6710 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6711 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6712 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6713 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6714 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6715 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6716 | |
| 6717 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6718 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6719 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6720 | int i, len; |
| 6721 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6722 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6723 | if (!PySequence_Check(groups)) { |
| 6724 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6725 | return NULL; |
| 6726 | } |
| 6727 | len = PySequence_Size(groups); |
| 6728 | if (len > MAX_GROUPS) { |
| 6729 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6730 | return NULL; |
| 6731 | } |
| 6732 | for(i = 0; i < len; i++) { |
| 6733 | PyObject *elem; |
| 6734 | elem = PySequence_GetItem(groups, i); |
| 6735 | if (!elem) |
| 6736 | return NULL; |
| 6737 | if (!PyLong_Check(elem)) { |
| 6738 | PyErr_SetString(PyExc_TypeError, |
| 6739 | "groups must be integers"); |
| 6740 | Py_DECREF(elem); |
| 6741 | return NULL; |
| 6742 | } else { |
| 6743 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6744 | if (PyErr_Occurred()) { |
| 6745 | PyErr_SetString(PyExc_TypeError, |
| 6746 | "group id too big"); |
| 6747 | Py_DECREF(elem); |
| 6748 | return NULL; |
| 6749 | } |
| 6750 | grouplist[i] = x; |
| 6751 | /* read back the value to see if it fitted in gid_t */ |
| 6752 | if (grouplist[i] != x) { |
| 6753 | PyErr_SetString(PyExc_TypeError, |
| 6754 | "group id too big"); |
| 6755 | Py_DECREF(elem); |
| 6756 | return NULL; |
| 6757 | } |
| 6758 | } |
| 6759 | Py_DECREF(elem); |
| 6760 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6761 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6762 | if (setgroups(len, grouplist) < 0) |
| 6763 | return posix_error(); |
| 6764 | Py_INCREF(Py_None); |
| 6765 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6766 | } |
| 6767 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6768 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6769 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6770 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6771 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6772 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6773 | PyObject *result; |
| 6774 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6775 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6776 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6777 | if (pid == -1) |
| 6778 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6779 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6780 | if (struct_rusage == NULL) { |
| 6781 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6782 | if (m == NULL) |
| 6783 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6784 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6785 | Py_DECREF(m); |
| 6786 | if (struct_rusage == NULL) |
| 6787 | return NULL; |
| 6788 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6789 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6790 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6791 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6792 | if (!result) |
| 6793 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6794 | |
| 6795 | #ifndef doubletime |
| 6796 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6797 | #endif |
| 6798 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6799 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6800 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6801 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6802 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6803 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6804 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6805 | SET_INT(result, 2, ru->ru_maxrss); |
| 6806 | SET_INT(result, 3, ru->ru_ixrss); |
| 6807 | SET_INT(result, 4, ru->ru_idrss); |
| 6808 | SET_INT(result, 5, ru->ru_isrss); |
| 6809 | SET_INT(result, 6, ru->ru_minflt); |
| 6810 | SET_INT(result, 7, ru->ru_majflt); |
| 6811 | SET_INT(result, 8, ru->ru_nswap); |
| 6812 | SET_INT(result, 9, ru->ru_inblock); |
| 6813 | SET_INT(result, 10, ru->ru_oublock); |
| 6814 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6815 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6816 | SET_INT(result, 13, ru->ru_nsignals); |
| 6817 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6818 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6819 | #undef SET_INT |
| 6820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6821 | if (PyErr_Occurred()) { |
| 6822 | Py_DECREF(result); |
| 6823 | return NULL; |
| 6824 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6825 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6826 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6827 | } |
| 6828 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6829 | |
| 6830 | #ifdef HAVE_WAIT3 |
| 6831 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6832 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6833 | Wait for completion of a child process."); |
| 6834 | |
| 6835 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6836 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6837 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6838 | pid_t pid; |
| 6839 | int options; |
| 6840 | struct rusage ru; |
| 6841 | WAIT_TYPE status; |
| 6842 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6843 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6844 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6845 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6846 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6847 | Py_BEGIN_ALLOW_THREADS |
| 6848 | pid = wait3(&status, options, &ru); |
| 6849 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6850 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6851 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6852 | } |
| 6853 | #endif /* HAVE_WAIT3 */ |
| 6854 | |
| 6855 | #ifdef HAVE_WAIT4 |
| 6856 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6857 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6858 | Wait for completion of a given child process."); |
| 6859 | |
| 6860 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6861 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6862 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6863 | pid_t pid; |
| 6864 | int options; |
| 6865 | struct rusage ru; |
| 6866 | WAIT_TYPE status; |
| 6867 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6868 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6869 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6870 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6871 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6872 | Py_BEGIN_ALLOW_THREADS |
| 6873 | pid = wait4(pid, &status, options, &ru); |
| 6874 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6875 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6876 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6877 | } |
| 6878 | #endif /* HAVE_WAIT4 */ |
| 6879 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6880 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6881 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6882 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6883 | Wait for the completion of one or more child processes.\n\n\ |
| 6884 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6885 | id specifies the pid to wait on.\n\ |
| 6886 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6887 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6888 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6889 | no children in a waitable state."); |
| 6890 | |
| 6891 | static PyObject * |
| 6892 | posix_waitid(PyObject *self, PyObject *args) |
| 6893 | { |
| 6894 | PyObject *result; |
| 6895 | idtype_t idtype; |
| 6896 | id_t id; |
| 6897 | int options, res; |
| 6898 | siginfo_t si; |
| 6899 | si.si_pid = 0; |
| 6900 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6901 | return NULL; |
| 6902 | Py_BEGIN_ALLOW_THREADS |
| 6903 | res = waitid(idtype, id, &si, options); |
| 6904 | Py_END_ALLOW_THREADS |
| 6905 | if (res == -1) |
| 6906 | return posix_error(); |
| 6907 | |
| 6908 | if (si.si_pid == 0) |
| 6909 | Py_RETURN_NONE; |
| 6910 | |
| 6911 | result = PyStructSequence_New(&WaitidResultType); |
| 6912 | if (!result) |
| 6913 | return NULL; |
| 6914 | |
| 6915 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6916 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6917 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6918 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6919 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6920 | if (PyErr_Occurred()) { |
| 6921 | Py_DECREF(result); |
| 6922 | return NULL; |
| 6923 | } |
| 6924 | |
| 6925 | return result; |
| 6926 | } |
| 6927 | #endif |
| 6928 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6929 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6930 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6931 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6932 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6933 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6934 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6935 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6936 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6937 | pid_t pid; |
| 6938 | int options; |
| 6939 | WAIT_TYPE status; |
| 6940 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6941 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6942 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6943 | return NULL; |
| 6944 | Py_BEGIN_ALLOW_THREADS |
| 6945 | pid = waitpid(pid, &status, options); |
| 6946 | Py_END_ALLOW_THREADS |
| 6947 | if (pid == -1) |
| 6948 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6949 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6950 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6951 | } |
| 6952 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6953 | #elif defined(HAVE_CWAIT) |
| 6954 | |
| 6955 | /* 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] | 6956 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6957 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6958 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6959 | |
| 6960 | static PyObject * |
| 6961 | posix_waitpid(PyObject *self, PyObject *args) |
| 6962 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6963 | Py_intptr_t pid; |
| 6964 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6965 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6966 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6967 | return NULL; |
| 6968 | Py_BEGIN_ALLOW_THREADS |
| 6969 | pid = _cwait(&status, pid, options); |
| 6970 | Py_END_ALLOW_THREADS |
| 6971 | if (pid == -1) |
| 6972 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6973 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6974 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6975 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6976 | } |
| 6977 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6978 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6979 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6980 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6981 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6982 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6983 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6984 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6985 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6986 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6987 | pid_t pid; |
| 6988 | WAIT_TYPE status; |
| 6989 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6990 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6991 | Py_BEGIN_ALLOW_THREADS |
| 6992 | pid = wait(&status); |
| 6993 | Py_END_ALLOW_THREADS |
| 6994 | if (pid == -1) |
| 6995 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6996 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6997 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6998 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6999 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7000 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7001 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7002 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7003 | PyDoc_STRVAR(readlink__doc__, |
| 7004 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7005 | Return a string representing the path to which the symbolic link points.\n\ |
| 7006 | \n\ |
| 7007 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7008 | and path should be relative; path will then be relative to that directory.\n\ |
| 7009 | dir_fd may not be implemented on your platform.\n\ |
| 7010 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7011 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7012 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7013 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7014 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7015 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7016 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7017 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7018 | path_t path; |
| 7019 | int dir_fd = DEFAULT_DIR_FD; |
| 7020 | char buffer[MAXPATHLEN]; |
| 7021 | ssize_t length; |
| 7022 | PyObject *return_value = NULL; |
| 7023 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7024 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7025 | memset(&path, 0, sizeof(path)); |
| 7026 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7027 | path_converter, &path, |
| 7028 | #ifdef HAVE_READLINKAT |
| 7029 | dir_fd_converter, &dir_fd |
| 7030 | #else |
| 7031 | dir_fd_unavailable, &dir_fd |
| 7032 | #endif |
| 7033 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7034 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7035 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7036 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7037 | #ifdef HAVE_READLINKAT |
| 7038 | if (dir_fd != DEFAULT_DIR_FD) |
| 7039 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7040 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7041 | #endif |
| 7042 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7043 | Py_END_ALLOW_THREADS |
| 7044 | |
| 7045 | if (length < 0) { |
| 7046 | return_value = path_posix_error("readlink", &path); |
| 7047 | goto exit; |
| 7048 | } |
| 7049 | |
| 7050 | if (PyUnicode_Check(path.object)) |
| 7051 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7052 | else |
| 7053 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7054 | exit: |
| 7055 | path_cleanup(&path); |
| 7056 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7057 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7058 | |
| 7059 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7060 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7061 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7062 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7063 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7064 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7065 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7066 | Create a symbolic link pointing to src named dst.\n\n\ |
| 7067 | target_is_directory is required on Windows if the target is to be\n\ |
| 7068 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 7069 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 7070 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 7071 | \n\ |
| 7072 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7073 | and path should be relative; path will then be relative to that directory.\n\ |
| 7074 | dir_fd may not be implemented on your platform.\n\ |
| 7075 | If it is unavailable, using it will raise a NotImplementedError."); |
| 7076 | |
| 7077 | #if defined(MS_WINDOWS) |
| 7078 | |
| 7079 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7080 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7081 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
| 7082 | static int |
| 7083 | check_CreateSymbolicLink() |
| 7084 | { |
| 7085 | HINSTANCE hKernel32; |
| 7086 | /* only recheck */ |
| 7087 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7088 | return 1; |
| 7089 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7090 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7091 | "CreateSymbolicLinkW"); |
| 7092 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7093 | "CreateSymbolicLinkA"); |
| 7094 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7095 | } |
| 7096 | |
| 7097 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7098 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7099 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7100 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7101 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7102 | path_t src; |
| 7103 | path_t dst; |
| 7104 | int dir_fd = DEFAULT_DIR_FD; |
| 7105 | int target_is_directory = 0; |
| 7106 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 7107 | "dir_fd", NULL}; |
| 7108 | PyObject *return_value; |
| 7109 | #ifdef MS_WINDOWS |
| 7110 | DWORD result; |
| 7111 | #else |
| 7112 | int result; |
| 7113 | #endif |
| 7114 | |
| 7115 | memset(&src, 0, sizeof(src)); |
| 7116 | src.argument_name = "src"; |
| 7117 | memset(&dst, 0, sizeof(dst)); |
| 7118 | dst.argument_name = "dst"; |
| 7119 | |
| 7120 | #ifdef MS_WINDOWS |
| 7121 | if (!check_CreateSymbolicLink()) { |
| 7122 | PyErr_SetString(PyExc_NotImplementedError, |
| 7123 | "CreateSymbolicLink functions not found"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7124 | return NULL; |
| 7125 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7126 | if (!win32_can_symlink) { |
| 7127 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7128 | return NULL; |
| 7129 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7130 | #endif |
| 7131 | |
| 7132 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 7133 | keywords, |
| 7134 | path_converter, &src, |
| 7135 | path_converter, &dst, |
| 7136 | &target_is_directory, |
| 7137 | #ifdef HAVE_SYMLINKAT |
| 7138 | dir_fd_converter, &dir_fd |
| 7139 | #else |
| 7140 | dir_fd_unavailable, &dir_fd |
| 7141 | #endif |
| 7142 | )) |
| 7143 | return NULL; |
| 7144 | |
| 7145 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 7146 | PyErr_SetString(PyExc_ValueError, |
| 7147 | "symlink: src and dst must be the same type"); |
| 7148 | return_value = NULL; |
| 7149 | goto exit; |
| 7150 | } |
| 7151 | |
| 7152 | #ifdef MS_WINDOWS |
| 7153 | Py_BEGIN_ALLOW_THREADS |
| 7154 | if (dst.wide) |
| 7155 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 7156 | target_is_directory); |
| 7157 | else |
| 7158 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 7159 | target_is_directory); |
| 7160 | Py_END_ALLOW_THREADS |
| 7161 | |
| 7162 | if (!result) { |
| 7163 | return_value = win32_error_object("symlink", src.object); |
| 7164 | goto exit; |
| 7165 | } |
| 7166 | |
| 7167 | #else |
| 7168 | |
| 7169 | Py_BEGIN_ALLOW_THREADS |
| 7170 | #if HAVE_SYMLINKAT |
| 7171 | if (dir_fd != DEFAULT_DIR_FD) |
| 7172 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 7173 | else |
| 7174 | #endif |
| 7175 | result = symlink(src.narrow, dst.narrow); |
| 7176 | Py_END_ALLOW_THREADS |
| 7177 | |
| 7178 | if (result) { |
| 7179 | return_value = path_error("symlink", &dst); |
| 7180 | goto exit; |
| 7181 | } |
| 7182 | #endif |
| 7183 | |
| 7184 | return_value = Py_None; |
| 7185 | Py_INCREF(Py_None); |
| 7186 | goto exit; /* silence "unused label" warning */ |
| 7187 | exit: |
| 7188 | path_cleanup(&src); |
| 7189 | path_cleanup(&dst); |
| 7190 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7191 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7192 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7193 | #endif /* HAVE_SYMLINK */ |
| 7194 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7195 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7196 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7197 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7198 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7199 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7200 | { |
| 7201 | wchar_t *path; |
| 7202 | DWORD n_bytes_returned; |
| 7203 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7204 | PyObject *po, *result; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 7205 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7206 | HANDLE reparse_point_handle; |
| 7207 | |
| 7208 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7209 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7210 | wchar_t *print_name; |
| 7211 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7212 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7213 | |
| 7214 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7215 | &po, |
| 7216 | dir_fd_unavailable, &dir_fd |
| 7217 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7218 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7219 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7220 | path = PyUnicode_AsUnicode(po); |
| 7221 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7222 | return NULL; |
| 7223 | |
| 7224 | /* First get a handle to the reparse point */ |
| 7225 | Py_BEGIN_ALLOW_THREADS |
| 7226 | reparse_point_handle = CreateFileW( |
| 7227 | path, |
| 7228 | 0, |
| 7229 | 0, |
| 7230 | 0, |
| 7231 | OPEN_EXISTING, |
| 7232 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7233 | 0); |
| 7234 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7235 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7236 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7237 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7238 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7239 | Py_BEGIN_ALLOW_THREADS |
| 7240 | /* New call DeviceIoControl to read the reparse point */ |
| 7241 | io_result = DeviceIoControl( |
| 7242 | reparse_point_handle, |
| 7243 | FSCTL_GET_REPARSE_POINT, |
| 7244 | 0, 0, /* in buffer */ |
| 7245 | target_buffer, sizeof(target_buffer), |
| 7246 | &n_bytes_returned, |
| 7247 | 0 /* we're not using OVERLAPPED_IO */ |
| 7248 | ); |
| 7249 | CloseHandle(reparse_point_handle); |
| 7250 | Py_END_ALLOW_THREADS |
| 7251 | |
| 7252 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7253 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7254 | |
| 7255 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7256 | { |
| 7257 | PyErr_SetString(PyExc_ValueError, |
| 7258 | "not a symbolic link"); |
| 7259 | return NULL; |
| 7260 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 7261 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7262 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7263 | |
| 7264 | result = PyUnicode_FromWideChar(print_name, |
| 7265 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7266 | return result; |
| 7267 | } |
| 7268 | |
| 7269 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7270 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7271 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7272 | static PyStructSequence_Field times_result_fields[] = { |
| 7273 | {"user", "user time"}, |
| 7274 | {"system", "system time"}, |
| 7275 | {"children_user", "user time of children"}, |
| 7276 | {"children_system", "system time of children"}, |
| 7277 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 7278 | {NULL} |
| 7279 | }; |
| 7280 | |
| 7281 | PyDoc_STRVAR(times_result__doc__, |
| 7282 | "times_result: Result from os.times().\n\n\ |
| 7283 | This object may be accessed either as a tuple of\n\ |
| 7284 | (user, system, children_user, children_system, elapsed),\n\ |
| 7285 | or via the attributes user, system, children_user, children_system,\n\ |
| 7286 | and elapsed.\n\ |
| 7287 | \n\ |
| 7288 | See os.times for more information."); |
| 7289 | |
| 7290 | static PyStructSequence_Desc times_result_desc = { |
| 7291 | "times_result", /* name */ |
| 7292 | times_result__doc__, /* doc */ |
| 7293 | times_result_fields, |
| 7294 | 5 |
| 7295 | }; |
| 7296 | |
| 7297 | static PyTypeObject TimesResultType; |
| 7298 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7299 | #ifdef MS_WINDOWS |
| 7300 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 7301 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7302 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7303 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7304 | |
| 7305 | static PyObject * |
| 7306 | build_times_result(double user, double system, |
| 7307 | double children_user, double children_system, |
| 7308 | double elapsed) |
| 7309 | { |
| 7310 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 7311 | if (value == NULL) |
| 7312 | return NULL; |
| 7313 | |
| 7314 | #define SET(i, field) \ |
| 7315 | { \ |
| 7316 | PyObject *o = PyFloat_FromDouble(field); \ |
| 7317 | if (!o) { \ |
| 7318 | Py_DECREF(value); \ |
| 7319 | return NULL; \ |
| 7320 | } \ |
| 7321 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 7322 | } \ |
| 7323 | |
| 7324 | SET(0, user); |
| 7325 | SET(1, system); |
| 7326 | SET(2, children_user); |
| 7327 | SET(3, children_system); |
| 7328 | SET(4, elapsed); |
| 7329 | |
| 7330 | #undef SET |
| 7331 | |
| 7332 | return value; |
| 7333 | } |
| 7334 | |
| 7335 | PyDoc_STRVAR(posix_times__doc__, |
| 7336 | "times() -> times_result\n\n\ |
| 7337 | Return an object containing floating point numbers indicating process\n\ |
| 7338 | times. The object behaves like a named tuple with these fields:\n\ |
| 7339 | (utime, stime, cutime, cstime, elapsed_time)"); |
| 7340 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7341 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 7342 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7343 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7344 | { |
| 7345 | ULONG value = 0; |
| 7346 | |
| 7347 | Py_BEGIN_ALLOW_THREADS |
| 7348 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 7349 | Py_END_ALLOW_THREADS |
| 7350 | |
| 7351 | return value; |
| 7352 | } |
| 7353 | |
| 7354 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7355 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7356 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7357 | /* Currently Only Uptime is Provided -- Others Later */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7358 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7359 | (double)0 /* t.tms_utime / HZ */, |
| 7360 | (double)0 /* t.tms_stime / HZ */, |
| 7361 | (double)0 /* t.tms_cutime / HZ */, |
| 7362 | (double)0 /* t.tms_cstime / HZ */, |
| 7363 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7364 | } |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7365 | #elif defined(MS_WINDOWS) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7366 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7367 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7368 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7369 | FILETIME create, exit, kernel, user; |
| 7370 | HANDLE hProc; |
| 7371 | hProc = GetCurrentProcess(); |
| 7372 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7373 | /* The fields of a FILETIME structure are the hi and lo part |
| 7374 | of a 64-bit value expressed in 100 nanosecond units. |
| 7375 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7376 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7377 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 7378 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7379 | (double)(user.dwHighDateTime*429.4967296 + |
| 7380 | user.dwLowDateTime*1e-7), |
| 7381 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7382 | kernel.dwLowDateTime*1e-7), |
| 7383 | (double)0, |
| 7384 | (double)0, |
| 7385 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7386 | } |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7387 | #else /* Neither Windows nor OS/2 */ |
| 7388 | #define NEED_TICKS_PER_SECOND |
| 7389 | static long ticks_per_second = -1; |
| 7390 | static PyObject * |
| 7391 | posix_times(PyObject *self, PyObject *noargs) |
| 7392 | { |
| 7393 | struct tms t; |
| 7394 | clock_t c; |
| 7395 | errno = 0; |
| 7396 | c = times(&t); |
| 7397 | if (c == (clock_t) -1) |
| 7398 | return posix_error(); |
| 7399 | return build_times_result( |
| 7400 | (double)t.tms_utime / ticks_per_second, |
| 7401 | (double)t.tms_stime / ticks_per_second, |
| 7402 | (double)t.tms_cutime / ticks_per_second, |
| 7403 | (double)t.tms_cstime / ticks_per_second, |
| 7404 | (double)c / ticks_per_second); |
| 7405 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7406 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7407 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 7408 | #endif /* HAVE_TIMES */ |
| 7409 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7410 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7411 | #ifdef HAVE_GETSID |
| 7412 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7413 | "getsid(pid) -> sid\n\n\ |
| 7414 | Call the system call getsid()."); |
| 7415 | |
| 7416 | static PyObject * |
| 7417 | posix_getsid(PyObject *self, PyObject *args) |
| 7418 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7419 | pid_t pid; |
| 7420 | int sid; |
| 7421 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7422 | return NULL; |
| 7423 | sid = getsid(pid); |
| 7424 | if (sid < 0) |
| 7425 | return posix_error(); |
| 7426 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7427 | } |
| 7428 | #endif /* HAVE_GETSID */ |
| 7429 | |
| 7430 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7431 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7432 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7433 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7434 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7435 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7436 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7437 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7438 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7439 | if (setsid() < 0) |
| 7440 | return posix_error(); |
| 7441 | Py_INCREF(Py_None); |
| 7442 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7443 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7444 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7445 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7446 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7447 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7448 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7449 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7450 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7451 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7452 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7453 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7454 | pid_t pid; |
| 7455 | int pgrp; |
| 7456 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7457 | return NULL; |
| 7458 | if (setpgid(pid, pgrp) < 0) |
| 7459 | return posix_error(); |
| 7460 | Py_INCREF(Py_None); |
| 7461 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7462 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7463 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7464 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7465 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7466 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7467 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7468 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7469 | 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] | 7470 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7471 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7472 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7473 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7474 | int fd; |
| 7475 | pid_t pgid; |
| 7476 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7477 | return NULL; |
| 7478 | pgid = tcgetpgrp(fd); |
| 7479 | if (pgid < 0) |
| 7480 | return posix_error(); |
| 7481 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7482 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7483 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7484 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7485 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7486 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7487 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7488 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7489 | 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] | 7490 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7491 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7492 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7493 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7494 | int fd; |
| 7495 | pid_t pgid; |
| 7496 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7497 | return NULL; |
| 7498 | if (tcsetpgrp(fd, pgid) < 0) |
| 7499 | return posix_error(); |
| 7500 | Py_INCREF(Py_None); |
| 7501 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7502 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7503 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7504 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7505 | /* Functions acting on file descriptors */ |
| 7506 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7507 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7508 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7509 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7510 | \n\ |
| 7511 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7512 | and path should be relative; path will then be relative to that directory.\n\ |
| 7513 | dir_fd may not be implemented on your platform.\n\ |
| 7514 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7515 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7516 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7517 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7518 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7519 | path_t path; |
| 7520 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7521 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7522 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7523 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7524 | PyObject *return_value = NULL; |
| 7525 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7526 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7527 | memset(&path, 0, sizeof(path)); |
| 7528 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7529 | path_converter, &path, |
| 7530 | &flags, &mode, |
| 7531 | #ifdef HAVE_OPENAT |
| 7532 | dir_fd_converter, &dir_fd |
| 7533 | #else |
| 7534 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7535 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7536 | )) |
| 7537 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7538 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7539 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7540 | #ifdef MS_WINDOWS |
| 7541 | if (path.wide) |
| 7542 | fd = _wopen(path.wide, flags, mode); |
| 7543 | else |
| 7544 | #endif |
| 7545 | #ifdef HAVE_OPENAT |
| 7546 | if (dir_fd != DEFAULT_DIR_FD) |
| 7547 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7548 | else |
| 7549 | #endif |
| 7550 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7551 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7552 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7553 | if (fd == -1) { |
| 7554 | #ifdef MS_WINDOWS |
| 7555 | /* force use of posix_error here for exact backwards compatibility */ |
| 7556 | if (path.wide) |
| 7557 | return_value = posix_error(); |
| 7558 | else |
| 7559 | #endif |
| 7560 | return_value = path_error("open", &path); |
| 7561 | goto exit; |
| 7562 | } |
| 7563 | |
| 7564 | return_value = PyLong_FromLong((long)fd); |
| 7565 | |
| 7566 | exit: |
| 7567 | path_cleanup(&path); |
| 7568 | return return_value; |
| 7569 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7571 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7572 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7573 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7574 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7575 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7576 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7577 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7578 | int fd, res; |
| 7579 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7580 | return NULL; |
| 7581 | if (!_PyVerify_fd(fd)) |
| 7582 | return posix_error(); |
| 7583 | Py_BEGIN_ALLOW_THREADS |
| 7584 | res = close(fd); |
| 7585 | Py_END_ALLOW_THREADS |
| 7586 | if (res < 0) |
| 7587 | return posix_error(); |
| 7588 | Py_INCREF(Py_None); |
| 7589 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7590 | } |
| 7591 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7592 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7593 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7594 | "closerange(fd_low, fd_high)\n\n\ |
| 7595 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7596 | |
| 7597 | static PyObject * |
| 7598 | posix_closerange(PyObject *self, PyObject *args) |
| 7599 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7600 | int fd_from, fd_to, i; |
| 7601 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7602 | return NULL; |
| 7603 | Py_BEGIN_ALLOW_THREADS |
| 7604 | for (i = fd_from; i < fd_to; i++) |
| 7605 | if (_PyVerify_fd(i)) |
| 7606 | close(i); |
| 7607 | Py_END_ALLOW_THREADS |
| 7608 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7609 | } |
| 7610 | |
| 7611 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7612 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7613 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7614 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7615 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7616 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7617 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7618 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7619 | int fd; |
| 7620 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7621 | return NULL; |
| 7622 | if (!_PyVerify_fd(fd)) |
| 7623 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7624 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7625 | if (fd < 0) |
| 7626 | return posix_error(); |
| 7627 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7628 | } |
| 7629 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7631 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7632 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7633 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7634 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7635 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7636 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7637 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7638 | int fd, fd2, res; |
| 7639 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 7640 | return NULL; |
| 7641 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7642 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7643 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7644 | if (res < 0) |
| 7645 | return posix_error(); |
| 7646 | Py_INCREF(Py_None); |
| 7647 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7648 | } |
| 7649 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7650 | #ifdef HAVE_LOCKF |
| 7651 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7652 | "lockf(fd, cmd, len)\n\n\ |
| 7653 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7654 | fd is an open file descriptor.\n\ |
| 7655 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7656 | F_TEST.\n\ |
| 7657 | len specifies the section of the file to lock."); |
| 7658 | |
| 7659 | static PyObject * |
| 7660 | posix_lockf(PyObject *self, PyObject *args) |
| 7661 | { |
| 7662 | int fd, cmd, res; |
| 7663 | off_t len; |
| 7664 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7665 | &fd, &cmd, _parse_off_t, &len)) |
| 7666 | return NULL; |
| 7667 | |
| 7668 | Py_BEGIN_ALLOW_THREADS |
| 7669 | res = lockf(fd, cmd, len); |
| 7670 | Py_END_ALLOW_THREADS |
| 7671 | |
| 7672 | if (res < 0) |
| 7673 | return posix_error(); |
| 7674 | |
| 7675 | Py_RETURN_NONE; |
| 7676 | } |
| 7677 | #endif |
| 7678 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7680 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7681 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7682 | Set the current position of a file descriptor.\n\ |
| 7683 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7684 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7685 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7686 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7687 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7688 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7689 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7690 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7691 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7692 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7693 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7694 | PyObject *posobj; |
| 7695 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7696 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7697 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7698 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7699 | switch (how) { |
| 7700 | case 0: how = SEEK_SET; break; |
| 7701 | case 1: how = SEEK_CUR; break; |
| 7702 | case 2: how = SEEK_END; break; |
| 7703 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7704 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7705 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7706 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7707 | pos = PyLong_AsLong(posobj); |
| 7708 | #else |
| 7709 | pos = PyLong_AsLongLong(posobj); |
| 7710 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7711 | if (PyErr_Occurred()) |
| 7712 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7713 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7714 | if (!_PyVerify_fd(fd)) |
| 7715 | return posix_error(); |
| 7716 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7717 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7718 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7719 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7720 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7721 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7722 | Py_END_ALLOW_THREADS |
| 7723 | if (res < 0) |
| 7724 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7725 | |
| 7726 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7727 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7728 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7729 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7730 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7731 | } |
| 7732 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7734 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7735 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7736 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7737 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7738 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7739 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7740 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7741 | int fd, size; |
| 7742 | Py_ssize_t n; |
| 7743 | PyObject *buffer; |
| 7744 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7745 | return NULL; |
| 7746 | if (size < 0) { |
| 7747 | errno = EINVAL; |
| 7748 | return posix_error(); |
| 7749 | } |
| 7750 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7751 | if (buffer == NULL) |
| 7752 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7753 | if (!_PyVerify_fd(fd)) { |
| 7754 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7755 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7756 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7757 | Py_BEGIN_ALLOW_THREADS |
| 7758 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7759 | Py_END_ALLOW_THREADS |
| 7760 | if (n < 0) { |
| 7761 | Py_DECREF(buffer); |
| 7762 | return posix_error(); |
| 7763 | } |
| 7764 | if (n != size) |
| 7765 | _PyBytes_Resize(&buffer, n); |
| 7766 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7767 | } |
| 7768 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7769 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7770 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7771 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7772 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7773 | { |
| 7774 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7775 | Py_ssize_t blen, total = 0; |
| 7776 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7777 | *iov = PyMem_New(struct iovec, cnt); |
| 7778 | if (*iov == NULL) { |
| 7779 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7780 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7781 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7782 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7783 | *buf = PyMem_New(Py_buffer, cnt); |
| 7784 | if (*buf == NULL) { |
| 7785 | PyMem_Del(*iov); |
| 7786 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7787 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7788 | } |
| 7789 | |
| 7790 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7791 | PyObject *item = PySequence_GetItem(seq, i); |
| 7792 | if (item == NULL) |
| 7793 | goto fail; |
| 7794 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7795 | Py_DECREF(item); |
| 7796 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7797 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7798 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7799 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7800 | blen = (*buf)[i].len; |
| 7801 | (*iov)[i].iov_len = blen; |
| 7802 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7803 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7804 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7805 | |
| 7806 | fail: |
| 7807 | PyMem_Del(*iov); |
| 7808 | for (j = 0; j < i; j++) { |
| 7809 | PyBuffer_Release(&(*buf)[j]); |
| 7810 | } |
| 7811 | PyMem_Del(*buf); |
| 7812 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
| 7815 | static void |
| 7816 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7817 | { |
| 7818 | int i; |
| 7819 | PyMem_Del(iov); |
| 7820 | for (i = 0; i < cnt; i++) { |
| 7821 | PyBuffer_Release(&buf[i]); |
| 7822 | } |
| 7823 | PyMem_Del(buf); |
| 7824 | } |
| 7825 | #endif |
| 7826 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7827 | #ifdef HAVE_READV |
| 7828 | PyDoc_STRVAR(posix_readv__doc__, |
| 7829 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7830 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7831 | is an arbitrary sequence of writable buffers.\n\ |
| 7832 | Returns the total number of bytes read."); |
| 7833 | |
| 7834 | static PyObject * |
| 7835 | posix_readv(PyObject *self, PyObject *args) |
| 7836 | { |
| 7837 | int fd, cnt; |
| 7838 | Py_ssize_t n; |
| 7839 | PyObject *seq; |
| 7840 | struct iovec *iov; |
| 7841 | Py_buffer *buf; |
| 7842 | |
| 7843 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7844 | return NULL; |
| 7845 | if (!PySequence_Check(seq)) { |
| 7846 | PyErr_SetString(PyExc_TypeError, |
| 7847 | "readv() arg 2 must be a sequence"); |
| 7848 | return NULL; |
| 7849 | } |
| 7850 | cnt = PySequence_Size(seq); |
| 7851 | |
| 7852 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7853 | return NULL; |
| 7854 | |
| 7855 | Py_BEGIN_ALLOW_THREADS |
| 7856 | n = readv(fd, iov, cnt); |
| 7857 | Py_END_ALLOW_THREADS |
| 7858 | |
| 7859 | iov_cleanup(iov, buf, cnt); |
| 7860 | return PyLong_FromSsize_t(n); |
| 7861 | } |
| 7862 | #endif |
| 7863 | |
| 7864 | #ifdef HAVE_PREAD |
| 7865 | PyDoc_STRVAR(posix_pread__doc__, |
| 7866 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7867 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7868 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7869 | |
| 7870 | static PyObject * |
| 7871 | posix_pread(PyObject *self, PyObject *args) |
| 7872 | { |
| 7873 | int fd, size; |
| 7874 | off_t offset; |
| 7875 | Py_ssize_t n; |
| 7876 | PyObject *buffer; |
| 7877 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7878 | return NULL; |
| 7879 | |
| 7880 | if (size < 0) { |
| 7881 | errno = EINVAL; |
| 7882 | return posix_error(); |
| 7883 | } |
| 7884 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7885 | if (buffer == NULL) |
| 7886 | return NULL; |
| 7887 | if (!_PyVerify_fd(fd)) { |
| 7888 | Py_DECREF(buffer); |
| 7889 | return posix_error(); |
| 7890 | } |
| 7891 | Py_BEGIN_ALLOW_THREADS |
| 7892 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7893 | Py_END_ALLOW_THREADS |
| 7894 | if (n < 0) { |
| 7895 | Py_DECREF(buffer); |
| 7896 | return posix_error(); |
| 7897 | } |
| 7898 | if (n != size) |
| 7899 | _PyBytes_Resize(&buffer, n); |
| 7900 | return buffer; |
| 7901 | } |
| 7902 | #endif |
| 7903 | |
| 7904 | PyDoc_STRVAR(posix_write__doc__, |
| 7905 | "write(fd, string) -> byteswritten\n\n\ |
| 7906 | Write a string to a file descriptor."); |
| 7907 | |
| 7908 | static PyObject * |
| 7909 | posix_write(PyObject *self, PyObject *args) |
| 7910 | { |
| 7911 | Py_buffer pbuf; |
| 7912 | int fd; |
| 7913 | Py_ssize_t size, len; |
| 7914 | |
| 7915 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7916 | return NULL; |
| 7917 | if (!_PyVerify_fd(fd)) { |
| 7918 | PyBuffer_Release(&pbuf); |
| 7919 | return posix_error(); |
| 7920 | } |
| 7921 | len = pbuf.len; |
| 7922 | Py_BEGIN_ALLOW_THREADS |
| 7923 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7924 | if (len > INT_MAX) |
| 7925 | len = INT_MAX; |
| 7926 | size = write(fd, pbuf.buf, (int)len); |
| 7927 | #else |
| 7928 | size = write(fd, pbuf.buf, len); |
| 7929 | #endif |
| 7930 | Py_END_ALLOW_THREADS |
| 7931 | PyBuffer_Release(&pbuf); |
| 7932 | if (size < 0) |
| 7933 | return posix_error(); |
| 7934 | return PyLong_FromSsize_t(size); |
| 7935 | } |
| 7936 | |
| 7937 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7938 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7939 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7940 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7941 | -> byteswritten\n\ |
| 7942 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7943 | |
| 7944 | static PyObject * |
| 7945 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7946 | { |
| 7947 | int in, out; |
| 7948 | Py_ssize_t ret; |
| 7949 | off_t offset; |
| 7950 | |
| 7951 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7952 | #ifndef __APPLE__ |
| 7953 | Py_ssize_t len; |
| 7954 | #endif |
| 7955 | PyObject *headers = NULL, *trailers = NULL; |
| 7956 | Py_buffer *hbuf, *tbuf; |
| 7957 | off_t sbytes; |
| 7958 | struct sf_hdtr sf; |
| 7959 | int flags = 0; |
| 7960 | sf.headers = NULL; |
| 7961 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7962 | static char *keywords[] = {"out", "in", |
| 7963 | "offset", "count", |
| 7964 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7965 | |
| 7966 | #ifdef __APPLE__ |
| 7967 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7968 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7969 | #else |
| 7970 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7971 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7972 | #endif |
| 7973 | &headers, &trailers, &flags)) |
| 7974 | return NULL; |
| 7975 | if (headers != NULL) { |
| 7976 | if (!PySequence_Check(headers)) { |
| 7977 | PyErr_SetString(PyExc_TypeError, |
| 7978 | "sendfile() headers must be a sequence or None"); |
| 7979 | return NULL; |
| 7980 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7981 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7982 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7983 | if (sf.hdr_cnt > 0 && |
| 7984 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7985 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7986 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7987 | #ifdef __APPLE__ |
| 7988 | sbytes += i; |
| 7989 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7990 | } |
| 7991 | } |
| 7992 | if (trailers != NULL) { |
| 7993 | if (!PySequence_Check(trailers)) { |
| 7994 | PyErr_SetString(PyExc_TypeError, |
| 7995 | "sendfile() trailers must be a sequence or None"); |
| 7996 | return NULL; |
| 7997 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7998 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7999 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8000 | if (sf.trl_cnt > 0 && |
| 8001 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 8002 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8003 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8004 | #ifdef __APPLE__ |
| 8005 | sbytes += i; |
| 8006 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8007 | } |
| 8008 | } |
| 8009 | |
| 8010 | Py_BEGIN_ALLOW_THREADS |
| 8011 | #ifdef __APPLE__ |
| 8012 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 8013 | #else |
| 8014 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 8015 | #endif |
| 8016 | Py_END_ALLOW_THREADS |
| 8017 | |
| 8018 | if (sf.headers != NULL) |
| 8019 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8020 | if (sf.trailers != NULL) |
| 8021 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8022 | |
| 8023 | if (ret < 0) { |
| 8024 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8025 | if (sbytes != 0) { |
| 8026 | // some data has been sent |
| 8027 | goto done; |
| 8028 | } |
| 8029 | else { |
| 8030 | // no data has been sent; upper application is supposed |
| 8031 | // to retry on EAGAIN or EBUSY |
| 8032 | return posix_error(); |
| 8033 | } |
| 8034 | } |
| 8035 | return posix_error(); |
| 8036 | } |
| 8037 | goto done; |
| 8038 | |
| 8039 | done: |
| 8040 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8041 | return Py_BuildValue("l", sbytes); |
| 8042 | #else |
| 8043 | return Py_BuildValue("L", sbytes); |
| 8044 | #endif |
| 8045 | |
| 8046 | #else |
| 8047 | Py_ssize_t count; |
| 8048 | PyObject *offobj; |
| 8049 | static char *keywords[] = {"out", "in", |
| 8050 | "offset", "count", NULL}; |
| 8051 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8052 | keywords, &out, &in, &offobj, &count)) |
| 8053 | return NULL; |
| 8054 | #ifdef linux |
| 8055 | if (offobj == Py_None) { |
| 8056 | Py_BEGIN_ALLOW_THREADS |
| 8057 | ret = sendfile(out, in, NULL, count); |
| 8058 | Py_END_ALLOW_THREADS |
| 8059 | if (ret < 0) |
| 8060 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8061 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8062 | } |
| 8063 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8064 | if (!_parse_off_t(offobj, &offset)) |
| 8065 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8066 | Py_BEGIN_ALLOW_THREADS |
| 8067 | ret = sendfile(out, in, &offset, count); |
| 8068 | Py_END_ALLOW_THREADS |
| 8069 | if (ret < 0) |
| 8070 | return posix_error(); |
| 8071 | return Py_BuildValue("n", ret); |
| 8072 | #endif |
| 8073 | } |
| 8074 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8075 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8076 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8077 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8078 | Like stat(), but for an open file descriptor.\n\ |
| 8079 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8080 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8081 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8082 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8083 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8084 | int fd; |
| 8085 | STRUCT_STAT st; |
| 8086 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8087 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8088 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 8089 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8090 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 8091 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 8092 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8093 | Py_BEGIN_ALLOW_THREADS |
| 8094 | res = FSTAT(fd, &st); |
| 8095 | Py_END_ALLOW_THREADS |
| 8096 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8097 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8098 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8099 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8100 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8101 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8102 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8103 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8104 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8105 | } |
| 8106 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8107 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8108 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8109 | 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] | 8110 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8111 | |
| 8112 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 8113 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8114 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8115 | int fd; |
| 8116 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 8117 | return NULL; |
| 8118 | if (!_PyVerify_fd(fd)) |
| 8119 | return PyBool_FromLong(0); |
| 8120 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8121 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8122 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8123 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8124 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8125 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8126 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8127 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8128 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8129 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8130 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8131 | #if defined(PYOS_OS2) |
| 8132 | HFILE read, write; |
| 8133 | APIRET rc; |
| 8134 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8135 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8136 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8137 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8138 | |
| 8139 | return Py_BuildValue("(ii)", read, write); |
| 8140 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8141 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8142 | int fds[2]; |
| 8143 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8144 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8145 | if (res != 0) |
| 8146 | return posix_error(); |
| 8147 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8148 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8149 | HANDLE read, write; |
| 8150 | int read_fd, write_fd; |
| 8151 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8152 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8153 | if (!ok) |
| 8154 | return win32_error("CreatePipe", NULL); |
| 8155 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 8156 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 8157 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8158 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8159 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8160 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8161 | #endif /* HAVE_PIPE */ |
| 8162 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8163 | #ifdef HAVE_PIPE2 |
| 8164 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8165 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 8166 | Create a pipe with flags set atomically.\n\ |
| 8167 | flags can be constructed by ORing together one or more of these values:\n\ |
| 8168 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8169 | "); |
| 8170 | |
| 8171 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8172 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8173 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8174 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8175 | int fds[2]; |
| 8176 | int res; |
| 8177 | |
Serhiy Storchaka | 9101e23 | 2013-01-19 12:41:45 +0200 | [diff] [blame] | 8178 | flags = _PyLong_AsInt(arg); |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8179 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8180 | return NULL; |
| 8181 | |
| 8182 | res = pipe2(fds, flags); |
| 8183 | if (res != 0) |
| 8184 | return posix_error(); |
| 8185 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8186 | } |
| 8187 | #endif /* HAVE_PIPE2 */ |
| 8188 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8189 | #ifdef HAVE_WRITEV |
| 8190 | PyDoc_STRVAR(posix_writev__doc__, |
| 8191 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 8192 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 8193 | arbitrary sequence of buffers.\n\ |
| 8194 | Returns the total bytes written."); |
| 8195 | |
| 8196 | static PyObject * |
| 8197 | posix_writev(PyObject *self, PyObject *args) |
| 8198 | { |
| 8199 | int fd, cnt; |
| 8200 | Py_ssize_t res; |
| 8201 | PyObject *seq; |
| 8202 | struct iovec *iov; |
| 8203 | Py_buffer *buf; |
| 8204 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 8205 | return NULL; |
| 8206 | if (!PySequence_Check(seq)) { |
| 8207 | PyErr_SetString(PyExc_TypeError, |
| 8208 | "writev() arg 2 must be a sequence"); |
| 8209 | return NULL; |
| 8210 | } |
| 8211 | cnt = PySequence_Size(seq); |
| 8212 | |
| 8213 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 8214 | return NULL; |
| 8215 | } |
| 8216 | |
| 8217 | Py_BEGIN_ALLOW_THREADS |
| 8218 | res = writev(fd, iov, cnt); |
| 8219 | Py_END_ALLOW_THREADS |
| 8220 | |
| 8221 | iov_cleanup(iov, buf, cnt); |
| 8222 | return PyLong_FromSsize_t(res); |
| 8223 | } |
| 8224 | #endif |
| 8225 | |
| 8226 | #ifdef HAVE_PWRITE |
| 8227 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 8228 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 8229 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 8230 | offset unchanged."); |
| 8231 | |
| 8232 | static PyObject * |
| 8233 | posix_pwrite(PyObject *self, PyObject *args) |
| 8234 | { |
| 8235 | Py_buffer pbuf; |
| 8236 | int fd; |
| 8237 | off_t offset; |
| 8238 | Py_ssize_t size; |
| 8239 | |
| 8240 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 8241 | return NULL; |
| 8242 | |
| 8243 | if (!_PyVerify_fd(fd)) { |
| 8244 | PyBuffer_Release(&pbuf); |
| 8245 | return posix_error(); |
| 8246 | } |
| 8247 | Py_BEGIN_ALLOW_THREADS |
| 8248 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 8249 | Py_END_ALLOW_THREADS |
| 8250 | PyBuffer_Release(&pbuf); |
| 8251 | if (size < 0) |
| 8252 | return posix_error(); |
| 8253 | return PyLong_FromSsize_t(size); |
| 8254 | } |
| 8255 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8256 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8257 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8258 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8259 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 8260 | Create a FIFO (a POSIX named pipe).\n\ |
| 8261 | \n\ |
| 8262 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8263 | and path should be relative; path will then be relative to that directory.\n\ |
| 8264 | dir_fd may not be implemented on your platform.\n\ |
| 8265 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8267 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8268 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8269 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8270 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8271 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8272 | int dir_fd = DEFAULT_DIR_FD; |
| 8273 | int result; |
| 8274 | PyObject *return_value = NULL; |
| 8275 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 8276 | |
| 8277 | memset(&path, 0, sizeof(path)); |
| 8278 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 8279 | path_converter, &path, |
| 8280 | &mode, |
| 8281 | #ifdef HAVE_MKFIFOAT |
| 8282 | dir_fd_converter, &dir_fd |
| 8283 | #else |
| 8284 | dir_fd_unavailable, &dir_fd |
| 8285 | #endif |
| 8286 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8287 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8288 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8289 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8290 | #ifdef HAVE_MKFIFOAT |
| 8291 | if (dir_fd != DEFAULT_DIR_FD) |
| 8292 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 8293 | else |
| 8294 | #endif |
| 8295 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8296 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8297 | |
| 8298 | if (result < 0) { |
| 8299 | return_value = posix_error(); |
| 8300 | goto exit; |
| 8301 | } |
| 8302 | |
| 8303 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8304 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8305 | |
| 8306 | exit: |
| 8307 | path_cleanup(&path); |
| 8308 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8309 | } |
| 8310 | #endif |
| 8311 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8312 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8313 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8314 | "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] | 8315 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 8316 | named filename. mode specifies both the permissions to use and the\n\ |
| 8317 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 8318 | 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] | 8319 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8320 | os.makedev()), otherwise it is ignored.\n\ |
| 8321 | \n\ |
| 8322 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8323 | and path should be relative; path will then be relative to that directory.\n\ |
| 8324 | dir_fd may not be implemented on your platform.\n\ |
| 8325 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8326 | |
| 8327 | |
| 8328 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8329 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8330 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8331 | path_t path; |
| 8332 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8333 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8334 | int dir_fd = DEFAULT_DIR_FD; |
| 8335 | int result; |
| 8336 | PyObject *return_value = NULL; |
| 8337 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 8338 | |
| 8339 | memset(&path, 0, sizeof(path)); |
| 8340 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 8341 | path_converter, &path, |
| 8342 | &mode, &device, |
| 8343 | #ifdef HAVE_MKNODAT |
| 8344 | dir_fd_converter, &dir_fd |
| 8345 | #else |
| 8346 | dir_fd_unavailable, &dir_fd |
| 8347 | #endif |
| 8348 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8349 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8350 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8351 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8352 | #ifdef HAVE_MKNODAT |
| 8353 | if (dir_fd != DEFAULT_DIR_FD) |
| 8354 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 8355 | else |
| 8356 | #endif |
| 8357 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8358 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8359 | |
| 8360 | if (result < 0) { |
| 8361 | return_value = posix_error(); |
| 8362 | goto exit; |
| 8363 | } |
| 8364 | |
| 8365 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8366 | Py_INCREF(Py_None); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 8367 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8368 | exit: |
| 8369 | path_cleanup(&path); |
| 8370 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8371 | } |
| 8372 | #endif |
| 8373 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8374 | #ifdef HAVE_DEVICE_MACROS |
| 8375 | PyDoc_STRVAR(posix_major__doc__, |
| 8376 | "major(device) -> major number\n\ |
| 8377 | Extracts a device major number from a raw device number."); |
| 8378 | |
| 8379 | static PyObject * |
| 8380 | posix_major(PyObject *self, PyObject *args) |
| 8381 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8382 | int device; |
| 8383 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 8384 | return NULL; |
| 8385 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8386 | } |
| 8387 | |
| 8388 | PyDoc_STRVAR(posix_minor__doc__, |
| 8389 | "minor(device) -> minor number\n\ |
| 8390 | Extracts a device minor number from a raw device number."); |
| 8391 | |
| 8392 | static PyObject * |
| 8393 | posix_minor(PyObject *self, PyObject *args) |
| 8394 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8395 | int device; |
| 8396 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 8397 | return NULL; |
| 8398 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8399 | } |
| 8400 | |
| 8401 | PyDoc_STRVAR(posix_makedev__doc__, |
| 8402 | "makedev(major, minor) -> device number\n\ |
| 8403 | Composes a raw device number from the major and minor device numbers."); |
| 8404 | |
| 8405 | static PyObject * |
| 8406 | posix_makedev(PyObject *self, PyObject *args) |
| 8407 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8408 | int major, minor; |
| 8409 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 8410 | return NULL; |
| 8411 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8412 | } |
| 8413 | #endif /* device macros */ |
| 8414 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8415 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8416 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8417 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8418 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8419 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8420 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8421 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8422 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8423 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8424 | int fd; |
| 8425 | off_t length; |
| 8426 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8427 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8428 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8429 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8430 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8431 | Py_BEGIN_ALLOW_THREADS |
| 8432 | res = ftruncate(fd, length); |
| 8433 | Py_END_ALLOW_THREADS |
| 8434 | if (res < 0) |
| 8435 | return posix_error(); |
| 8436 | Py_INCREF(Py_None); |
| 8437 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8438 | } |
| 8439 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8440 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8441 | #ifdef HAVE_TRUNCATE |
| 8442 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8443 | "truncate(path, length)\n\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8444 | Truncate the file given by path to length bytes.\n\ |
| 8445 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8446 | If this functionality is unavailable, using it raises an exception."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8447 | |
| 8448 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8449 | posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8450 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8451 | path_t path; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8452 | off_t length; |
| 8453 | int res; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8454 | PyObject *result = NULL; |
| 8455 | static char *keywords[] = {"path", "length", NULL}; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8456 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8457 | memset(&path, 0, sizeof(path)); |
| 8458 | #ifdef HAVE_FTRUNCATE |
| 8459 | path.allow_fd = 1; |
| 8460 | #endif |
| 8461 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords, |
| 8462 | path_converter, &path, |
| 8463 | _parse_off_t, &length)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8464 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8465 | |
| 8466 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8467 | #ifdef HAVE_FTRUNCATE |
| 8468 | if (path.fd != -1) |
| 8469 | res = ftruncate(path.fd, length); |
| 8470 | else |
| 8471 | #endif |
| 8472 | res = truncate(path.narrow, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8473 | Py_END_ALLOW_THREADS |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8474 | if (res < 0) |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 8475 | result = path_posix_error("truncate", &path); |
| 8476 | else { |
| 8477 | Py_INCREF(Py_None); |
| 8478 | result = Py_None; |
| 8479 | } |
| 8480 | path_cleanup(&path); |
| 8481 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8482 | } |
| 8483 | #endif |
| 8484 | |
| 8485 | #ifdef HAVE_POSIX_FALLOCATE |
| 8486 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8487 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8488 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8489 | starting from offset and continuing for len bytes."); |
| 8490 | |
| 8491 | static PyObject * |
| 8492 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8493 | { |
| 8494 | off_t len, offset; |
| 8495 | int res, fd; |
| 8496 | |
| 8497 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8498 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8499 | return NULL; |
| 8500 | |
| 8501 | Py_BEGIN_ALLOW_THREADS |
| 8502 | res = posix_fallocate(fd, offset, len); |
| 8503 | Py_END_ALLOW_THREADS |
| 8504 | if (res != 0) { |
| 8505 | errno = res; |
| 8506 | return posix_error(); |
| 8507 | } |
| 8508 | Py_RETURN_NONE; |
| 8509 | } |
| 8510 | #endif |
| 8511 | |
| 8512 | #ifdef HAVE_POSIX_FADVISE |
| 8513 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8514 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8515 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8516 | the kernel to make optimizations.\n\ |
| 8517 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8518 | offset and continuing for len bytes.\n\ |
| 8519 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8520 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8521 | POSIX_FADV_DONTNEED."); |
| 8522 | |
| 8523 | static PyObject * |
| 8524 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8525 | { |
| 8526 | off_t len, offset; |
| 8527 | int res, fd, advice; |
| 8528 | |
| 8529 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8530 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8531 | return NULL; |
| 8532 | |
| 8533 | Py_BEGIN_ALLOW_THREADS |
| 8534 | res = posix_fadvise(fd, offset, len, advice); |
| 8535 | Py_END_ALLOW_THREADS |
| 8536 | if (res != 0) { |
| 8537 | errno = res; |
| 8538 | return posix_error(); |
| 8539 | } |
| 8540 | Py_RETURN_NONE; |
| 8541 | } |
| 8542 | #endif |
| 8543 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8544 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8545 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8546 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8547 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8548 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8549 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8550 | * get re-set with another call for the same key. */ |
| 8551 | static PyObject *posix_putenv_garbage; |
| 8552 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8553 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8554 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8555 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8556 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8557 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8558 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8559 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8560 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8561 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8562 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8563 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8564 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8565 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8566 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8567 | if (newstr == NULL) { |
| 8568 | PyErr_NoMemory(); |
| 8569 | goto error; |
| 8570 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8571 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8572 | PyErr_Format(PyExc_ValueError, |
| 8573 | "the environment variable is longer than %u characters", |
| 8574 | _MAX_ENV); |
| 8575 | goto error; |
| 8576 | } |
| 8577 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8578 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8579 | if (newenv == NULL) |
| 8580 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8581 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8582 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8583 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8584 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8585 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8586 | PyObject *os1, *os2; |
| 8587 | char *s1, *s2; |
| 8588 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8589 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8590 | if (!PyArg_ParseTuple(args, |
| 8591 | "O&O&:putenv", |
| 8592 | PyUnicode_FSConverter, &os1, |
| 8593 | PyUnicode_FSConverter, &os2)) |
| 8594 | return NULL; |
| 8595 | s1 = PyBytes_AsString(os1); |
| 8596 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8597 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8598 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8599 | if (newstr == NULL) { |
| 8600 | PyErr_NoMemory(); |
| 8601 | goto error; |
| 8602 | } |
| 8603 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8605 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8606 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8607 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8608 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8609 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8610 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8611 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8612 | * this will cause previous value to be collected. This has to |
| 8613 | * happen after the real putenv() call because the old value |
| 8614 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8615 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8616 | /* really not much we can do; just leak */ |
| 8617 | PyErr_Clear(); |
| 8618 | } |
| 8619 | else { |
| 8620 | Py_DECREF(newstr); |
| 8621 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8622 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8623 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8624 | Py_DECREF(os1); |
| 8625 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8626 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8627 | Py_RETURN_NONE; |
| 8628 | |
| 8629 | error: |
| 8630 | #ifndef MS_WINDOWS |
| 8631 | Py_DECREF(os1); |
| 8632 | Py_DECREF(os2); |
| 8633 | #endif |
| 8634 | Py_XDECREF(newstr); |
| 8635 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8636 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8637 | #endif /* putenv */ |
| 8638 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8639 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8640 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8641 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8642 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8643 | |
| 8644 | static PyObject * |
| 8645 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8646 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8647 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8648 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8649 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8650 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8651 | |
| 8652 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8653 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8654 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8655 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8656 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8657 | #ifdef HAVE_BROKEN_UNSETENV |
| 8658 | unsetenv(PyBytes_AS_STRING(name)); |
| 8659 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8660 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8661 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8662 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8663 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8664 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8665 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8666 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8667 | /* Remove the key from posix_putenv_garbage; |
| 8668 | * this will cause it to be collected. This has to |
| 8669 | * happen after the real unsetenv() call because the |
| 8670 | * old value was still accessible until then. |
| 8671 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8672 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8673 | /* really not much we can do; just leak */ |
| 8674 | PyErr_Clear(); |
| 8675 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8676 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8677 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8678 | } |
| 8679 | #endif /* unsetenv */ |
| 8680 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8681 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8682 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8683 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8684 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8685 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8686 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8687 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8688 | int code; |
| 8689 | char *message; |
| 8690 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8691 | return NULL; |
| 8692 | message = strerror(code); |
| 8693 | if (message == NULL) { |
| 8694 | PyErr_SetString(PyExc_ValueError, |
| 8695 | "strerror() argument out of range"); |
| 8696 | return NULL; |
| 8697 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8698 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8699 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8700 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8701 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8702 | #ifdef HAVE_SYS_WAIT_H |
| 8703 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8704 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8705 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8706 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8707 | 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] | 8708 | |
| 8709 | static PyObject * |
| 8710 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 8711 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | WAIT_TYPE status; |
| 8713 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8714 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 8716 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8717 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8718 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8719 | } |
| 8720 | #endif /* WCOREDUMP */ |
| 8721 | |
| 8722 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8723 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8724 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8725 | 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] | 8726 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8727 | |
| 8728 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8729 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8730 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8731 | WAIT_TYPE status; |
| 8732 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8733 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8734 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 8735 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8736 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8737 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8738 | } |
| 8739 | #endif /* WIFCONTINUED */ |
| 8740 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8741 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8742 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8743 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8744 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8745 | |
| 8746 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8747 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8748 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8749 | WAIT_TYPE status; |
| 8750 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8751 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8752 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 8753 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8754 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8755 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8756 | } |
| 8757 | #endif /* WIFSTOPPED */ |
| 8758 | |
| 8759 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8760 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8761 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8762 | 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] | 8763 | |
| 8764 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8765 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8766 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8767 | WAIT_TYPE status; |
| 8768 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8769 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8770 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 8771 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8772 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8773 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8774 | } |
| 8775 | #endif /* WIFSIGNALED */ |
| 8776 | |
| 8777 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8778 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8779 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8780 | 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] | 8781 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8782 | |
| 8783 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8784 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8785 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8786 | WAIT_TYPE status; |
| 8787 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8788 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8789 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 8790 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8791 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8792 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8793 | } |
| 8794 | #endif /* WIFEXITED */ |
| 8795 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8796 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8797 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8798 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8799 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8800 | |
| 8801 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8802 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8803 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8804 | WAIT_TYPE status; |
| 8805 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8807 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8808 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8809 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8810 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8811 | } |
| 8812 | #endif /* WEXITSTATUS */ |
| 8813 | |
| 8814 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8815 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8816 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8817 | 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] | 8818 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8819 | |
| 8820 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8821 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8822 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8823 | WAIT_TYPE status; |
| 8824 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8825 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8826 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8827 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8828 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8829 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8830 | } |
| 8831 | #endif /* WTERMSIG */ |
| 8832 | |
| 8833 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8834 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8835 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8836 | Return the signal that stopped the process that provided\n\ |
| 8837 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8838 | |
| 8839 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8840 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8841 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8842 | WAIT_TYPE status; |
| 8843 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8844 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8845 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8846 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8847 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8848 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8849 | } |
| 8850 | #endif /* WSTOPSIG */ |
| 8851 | |
| 8852 | #endif /* HAVE_SYS_WAIT_H */ |
| 8853 | |
| 8854 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8855 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8856 | #ifdef _SCO_DS |
| 8857 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8858 | needed definitions in sys/statvfs.h */ |
| 8859 | #define _SVID3 |
| 8860 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8861 | #include <sys/statvfs.h> |
| 8862 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8863 | static PyObject* |
| 8864 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8865 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8866 | if (v == NULL) |
| 8867 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8868 | |
| 8869 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8870 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8871 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8872 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8873 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8874 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8875 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8876 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8877 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8878 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8879 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8880 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8881 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8882 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8883 | PyStructSequence_SET_ITEM(v, 2, |
| 8884 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8885 | PyStructSequence_SET_ITEM(v, 3, |
| 8886 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8887 | PyStructSequence_SET_ITEM(v, 4, |
| 8888 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8889 | PyStructSequence_SET_ITEM(v, 5, |
| 8890 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8891 | PyStructSequence_SET_ITEM(v, 6, |
| 8892 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8893 | PyStructSequence_SET_ITEM(v, 7, |
| 8894 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8895 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8896 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8897 | #endif |
| 8898 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8899 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8900 | } |
| 8901 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8902 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8903 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8904 | Perform an fstatvfs system call on the given fd.\n\ |
| 8905 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8906 | |
| 8907 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8908 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8909 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8910 | int fd, res; |
| 8911 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8913 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8914 | return NULL; |
| 8915 | Py_BEGIN_ALLOW_THREADS |
| 8916 | res = fstatvfs(fd, &st); |
| 8917 | Py_END_ALLOW_THREADS |
| 8918 | if (res != 0) |
| 8919 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8920 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8921 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8922 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8923 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8924 | |
| 8925 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8926 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8927 | #include <sys/statvfs.h> |
| 8928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8929 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8930 | "statvfs(path)\n\n\ |
| 8931 | Perform a statvfs system call on the given path.\n\ |
| 8932 | \n\ |
| 8933 | path may always be specified as a string.\n\ |
| 8934 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8935 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8936 | |
| 8937 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8938 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8939 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8940 | static char *keywords[] = {"path", NULL}; |
| 8941 | path_t path; |
| 8942 | int result; |
| 8943 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8944 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8945 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8946 | memset(&path, 0, sizeof(path)); |
| 8947 | #ifdef HAVE_FSTATVFS |
| 8948 | path.allow_fd = 1; |
| 8949 | #endif |
| 8950 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 8951 | path_converter, &path |
| 8952 | )) |
| 8953 | return NULL; |
| 8954 | |
| 8955 | Py_BEGIN_ALLOW_THREADS |
| 8956 | #ifdef HAVE_FSTATVFS |
| 8957 | if (path.fd != -1) { |
| 8958 | #ifdef __APPLE__ |
| 8959 | /* handle weak-linking on Mac OS X 10.3 */ |
| 8960 | if (fstatvfs == NULL) { |
| 8961 | fd_specified("statvfs", path.fd); |
| 8962 | goto exit; |
| 8963 | } |
| 8964 | #endif |
| 8965 | result = fstatvfs(path.fd, &st); |
| 8966 | } |
| 8967 | else |
| 8968 | #endif |
| 8969 | result = statvfs(path.narrow, &st); |
| 8970 | Py_END_ALLOW_THREADS |
| 8971 | |
| 8972 | if (result) { |
| 8973 | return_value = path_posix_error("statvfs", &path); |
| 8974 | goto exit; |
| 8975 | } |
| 8976 | |
| 8977 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 8978 | |
| 8979 | exit: |
| 8980 | path_cleanup(&path); |
| 8981 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8982 | } |
| 8983 | #endif /* HAVE_STATVFS */ |
| 8984 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8985 | #ifdef MS_WINDOWS |
| 8986 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8987 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8988 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8989 | |
| 8990 | static PyObject * |
| 8991 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8992 | { |
| 8993 | BOOL retval; |
| 8994 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8995 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8996 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8997 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8998 | return NULL; |
| 8999 | |
| 9000 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 9001 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 9002 | Py_END_ALLOW_THREADS |
| 9003 | if (retval == 0) |
| 9004 | return PyErr_SetFromWindowsErr(0); |
| 9005 | |
| 9006 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9007 | } |
| 9008 | #endif |
| 9009 | |
| 9010 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9011 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9012 | * It maps strings representing configuration variable names to |
| 9013 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9014 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9015 | * rarely-used constants. There are three separate tables that use |
| 9016 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9017 | * |
| 9018 | * This code is always included, even if none of the interfaces that |
| 9019 | * need it are included. The #if hackery needed to avoid it would be |
| 9020 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9021 | */ |
| 9022 | struct constdef { |
| 9023 | char *name; |
| 9024 | long value; |
| 9025 | }; |
| 9026 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9027 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9028 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9029 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9030 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9031 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9032 | *valuep = PyLong_AS_LONG(arg); |
| 9033 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9034 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9035 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9036 | /* look up the value in the table using a binary search */ |
| 9037 | size_t lo = 0; |
| 9038 | size_t mid; |
| 9039 | size_t hi = tablesize; |
| 9040 | int cmp; |
| 9041 | const char *confname; |
| 9042 | if (!PyUnicode_Check(arg)) { |
| 9043 | PyErr_SetString(PyExc_TypeError, |
| 9044 | "configuration names must be strings or integers"); |
| 9045 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9046 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9047 | confname = _PyUnicode_AsString(arg); |
| 9048 | if (confname == NULL) |
| 9049 | return 0; |
| 9050 | while (lo < hi) { |
| 9051 | mid = (lo + hi) / 2; |
| 9052 | cmp = strcmp(confname, table[mid].name); |
| 9053 | if (cmp < 0) |
| 9054 | hi = mid; |
| 9055 | else if (cmp > 0) |
| 9056 | lo = mid + 1; |
| 9057 | else { |
| 9058 | *valuep = table[mid].value; |
| 9059 | return 1; |
| 9060 | } |
| 9061 | } |
| 9062 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9063 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9064 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9065 | } |
| 9066 | |
| 9067 | |
| 9068 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9069 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9070 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9071 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9072 | #endif |
| 9073 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9074 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9075 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9076 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9078 | #endif |
| 9079 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9080 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9081 | #endif |
| 9082 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9084 | #endif |
| 9085 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9086 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9087 | #endif |
| 9088 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9089 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9090 | #endif |
| 9091 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9092 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9093 | #endif |
| 9094 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9095 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9096 | #endif |
| 9097 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9098 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9099 | #endif |
| 9100 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9101 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9102 | #endif |
| 9103 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9104 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9105 | #endif |
| 9106 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9107 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9108 | #endif |
| 9109 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9110 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9111 | #endif |
| 9112 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9113 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9114 | #endif |
| 9115 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9116 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9117 | #endif |
| 9118 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9119 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9120 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9121 | #ifdef _PC_ACL_ENABLED |
| 9122 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9123 | #endif |
| 9124 | #ifdef _PC_MIN_HOLE_SIZE |
| 9125 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9126 | #endif |
| 9127 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9128 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9129 | #endif |
| 9130 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9131 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9132 | #endif |
| 9133 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9134 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9135 | #endif |
| 9136 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9137 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9138 | #endif |
| 9139 | #ifdef _PC_REC_XFER_ALIGN |
| 9140 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9141 | #endif |
| 9142 | #ifdef _PC_SYMLINK_MAX |
| 9143 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9144 | #endif |
| 9145 | #ifdef _PC_XATTR_ENABLED |
| 9146 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9147 | #endif |
| 9148 | #ifdef _PC_XATTR_EXISTS |
| 9149 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9150 | #endif |
| 9151 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9152 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9153 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9154 | }; |
| 9155 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9156 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9157 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9158 | { |
| 9159 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9160 | sizeof(posix_constants_pathconf) |
| 9161 | / sizeof(struct constdef)); |
| 9162 | } |
| 9163 | #endif |
| 9164 | |
| 9165 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9166 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9167 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9168 | 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] | 9169 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9170 | |
| 9171 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9172 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9173 | { |
| 9174 | PyObject *result = NULL; |
| 9175 | int name, fd; |
| 9176 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9177 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 9178 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9179 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9180 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9181 | errno = 0; |
| 9182 | limit = fpathconf(fd, name); |
| 9183 | if (limit == -1 && errno != 0) |
| 9184 | posix_error(); |
| 9185 | else |
| 9186 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9187 | } |
| 9188 | return result; |
| 9189 | } |
| 9190 | #endif |
| 9191 | |
| 9192 | |
| 9193 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9194 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9195 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9196 | Return the configuration limit name for the file or directory path.\n\ |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9197 | If there is no limit, return -1.\n\ |
| 9198 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 9199 | If this functionality is unavailable, using it raises an exception."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9200 | |
| 9201 | static PyObject * |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9202 | posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9203 | { |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9204 | path_t path; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9205 | PyObject *result = NULL; |
| 9206 | int name; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9207 | static char *keywords[] = {"path", "name", NULL}; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9208 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9209 | memset(&path, 0, sizeof(path)); |
| 9210 | #ifdef HAVE_FPATHCONF |
| 9211 | path.allow_fd = 1; |
| 9212 | #endif |
| 9213 | if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords, |
| 9214 | path_converter, &path, |
| 9215 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9216 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9217 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9218 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9219 | #ifdef HAVE_FPATHCONF |
| 9220 | if (path.fd != -1) |
| 9221 | limit = fpathconf(path.fd, name); |
| 9222 | else |
| 9223 | #endif |
| 9224 | limit = pathconf(path.narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9225 | if (limit == -1 && errno != 0) { |
| 9226 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9227 | /* could be a path or name problem */ |
| 9228 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9229 | else |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9230 | result = path_posix_error("pathconf", &path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9231 | } |
| 9232 | else |
| 9233 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9234 | } |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9235 | path_cleanup(&path); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9236 | return result; |
| 9237 | } |
| 9238 | #endif |
| 9239 | |
| 9240 | #ifdef HAVE_CONFSTR |
| 9241 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9242 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9243 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9244 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9245 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9246 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9247 | #endif |
| 9248 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9249 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9250 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9251 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9252 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9253 | #endif |
| 9254 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9255 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9256 | #endif |
| 9257 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9258 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9259 | #endif |
| 9260 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9261 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9262 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9263 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9264 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9265 | #endif |
| 9266 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9267 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9268 | #endif |
| 9269 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9270 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9271 | #endif |
| 9272 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9273 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9274 | #endif |
| 9275 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9276 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9277 | #endif |
| 9278 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9279 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9280 | #endif |
| 9281 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9282 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9283 | #endif |
| 9284 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9285 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9286 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9287 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9288 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9289 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9290 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9291 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9292 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9293 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9294 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9295 | #endif |
| 9296 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9297 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9298 | #endif |
| 9299 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9300 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9301 | #endif |
| 9302 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9303 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9304 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9305 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9306 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9307 | #endif |
| 9308 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9309 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9310 | #endif |
| 9311 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9312 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9313 | #endif |
| 9314 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9315 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9316 | #endif |
| 9317 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9318 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9319 | #endif |
| 9320 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9321 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9322 | #endif |
| 9323 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9324 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9325 | #endif |
| 9326 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9327 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9328 | #endif |
| 9329 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9330 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9331 | #endif |
| 9332 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9333 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9334 | #endif |
| 9335 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9336 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9337 | #endif |
| 9338 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9339 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9340 | #endif |
| 9341 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9342 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9343 | #endif |
| 9344 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9345 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9346 | #endif |
| 9347 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9348 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9349 | #endif |
| 9350 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9351 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9352 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9353 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9354 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9355 | #endif |
| 9356 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9357 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9358 | #endif |
| 9359 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9360 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9361 | #endif |
| 9362 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9363 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9364 | #endif |
| 9365 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9366 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9367 | #endif |
| 9368 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9369 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9370 | #endif |
| 9371 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9372 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9373 | #endif |
| 9374 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9375 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9376 | #endif |
| 9377 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9378 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9379 | #endif |
| 9380 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9381 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9382 | #endif |
| 9383 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9384 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9385 | #endif |
| 9386 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9387 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9388 | #endif |
| 9389 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9390 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9391 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9392 | }; |
| 9393 | |
| 9394 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9395 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9396 | { |
| 9397 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9398 | sizeof(posix_constants_confstr) |
| 9399 | / sizeof(struct constdef)); |
| 9400 | } |
| 9401 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9402 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9403 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9404 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9405 | |
| 9406 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9407 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9408 | { |
| 9409 | PyObject *result = NULL; |
| 9410 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9411 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9412 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9413 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9414 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 9415 | return NULL; |
| 9416 | |
| 9417 | errno = 0; |
| 9418 | len = confstr(name, buffer, sizeof(buffer)); |
| 9419 | if (len == 0) { |
| 9420 | if (errno) { |
| 9421 | posix_error(); |
| 9422 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9423 | } |
| 9424 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9425 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9426 | } |
| 9427 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9428 | |
| 9429 | if ((unsigned int)len >= sizeof(buffer)) { |
| 9430 | char *buf = PyMem_Malloc(len); |
| 9431 | if (buf == NULL) |
| 9432 | return PyErr_NoMemory(); |
| 9433 | confstr(name, buf, len); |
| 9434 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9435 | PyMem_Free(buf); |
| 9436 | } |
| 9437 | else |
| 9438 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9439 | return result; |
| 9440 | } |
| 9441 | #endif |
| 9442 | |
| 9443 | |
| 9444 | #ifdef HAVE_SYSCONF |
| 9445 | static struct constdef posix_constants_sysconf[] = { |
| 9446 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9447 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9448 | #endif |
| 9449 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9450 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9451 | #endif |
| 9452 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9453 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9454 | #endif |
| 9455 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9456 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9457 | #endif |
| 9458 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9459 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9460 | #endif |
| 9461 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9462 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9463 | #endif |
| 9464 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9465 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9466 | #endif |
| 9467 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9468 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9469 | #endif |
| 9470 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9471 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9472 | #endif |
| 9473 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9474 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9475 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9476 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9477 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9478 | #endif |
| 9479 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9480 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9481 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9482 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9483 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9484 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9485 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9486 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9487 | #endif |
| 9488 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9489 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9490 | #endif |
| 9491 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9492 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9493 | #endif |
| 9494 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9495 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9496 | #endif |
| 9497 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9498 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9499 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9500 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9501 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9502 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9503 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9504 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9505 | #endif |
| 9506 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9507 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9508 | #endif |
| 9509 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9510 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9511 | #endif |
| 9512 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9513 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9514 | #endif |
| 9515 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9516 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9517 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9518 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9519 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9520 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9521 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9522 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9523 | #endif |
| 9524 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9525 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9526 | #endif |
| 9527 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9528 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9529 | #endif |
| 9530 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9531 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9532 | #endif |
| 9533 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9534 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9535 | #endif |
| 9536 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9537 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9538 | #endif |
| 9539 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9540 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9541 | #endif |
| 9542 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9543 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9544 | #endif |
| 9545 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9546 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9547 | #endif |
| 9548 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9549 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9550 | #endif |
| 9551 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9552 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9553 | #endif |
| 9554 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9555 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9556 | #endif |
| 9557 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9558 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9559 | #endif |
| 9560 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9561 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9562 | #endif |
| 9563 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9564 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9565 | #endif |
| 9566 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9567 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9568 | #endif |
| 9569 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9570 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9571 | #endif |
| 9572 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9573 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9574 | #endif |
| 9575 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9576 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9577 | #endif |
| 9578 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9579 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9580 | #endif |
| 9581 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9582 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9583 | #endif |
| 9584 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9585 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9586 | #endif |
| 9587 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9588 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9589 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9590 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9591 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9592 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9593 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9594 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9595 | #endif |
| 9596 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9597 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9598 | #endif |
| 9599 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9600 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9601 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9602 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9603 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9604 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9605 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9606 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9607 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9608 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9609 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9610 | #endif |
| 9611 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9612 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9613 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9614 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9615 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9616 | #endif |
| 9617 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9618 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9619 | #endif |
| 9620 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9621 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9622 | #endif |
| 9623 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9624 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9625 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9626 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9627 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9628 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9629 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9630 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9631 | #endif |
| 9632 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9633 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9634 | #endif |
| 9635 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9636 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9637 | #endif |
| 9638 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9639 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9640 | #endif |
| 9641 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9642 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9643 | #endif |
| 9644 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9645 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9646 | #endif |
| 9647 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9648 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9649 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9650 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9651 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9652 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9653 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9654 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9655 | #endif |
| 9656 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9657 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9658 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9659 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9660 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9661 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9662 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9663 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9664 | #endif |
| 9665 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9666 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9667 | #endif |
| 9668 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9669 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9670 | #endif |
| 9671 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9672 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9673 | #endif |
| 9674 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9675 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9676 | #endif |
| 9677 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9678 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9679 | #endif |
| 9680 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9681 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9682 | #endif |
| 9683 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9684 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9685 | #endif |
| 9686 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9687 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9688 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9689 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9690 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9691 | #endif |
| 9692 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9693 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9694 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9695 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9696 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9697 | #endif |
| 9698 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9699 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9700 | #endif |
| 9701 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9702 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9703 | #endif |
| 9704 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9705 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9706 | #endif |
| 9707 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9708 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9709 | #endif |
| 9710 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9711 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9712 | #endif |
| 9713 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9714 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9715 | #endif |
| 9716 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9717 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9718 | #endif |
| 9719 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9720 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9721 | #endif |
| 9722 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9723 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9724 | #endif |
| 9725 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9726 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9727 | #endif |
| 9728 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9729 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9730 | #endif |
| 9731 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9732 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9733 | #endif |
| 9734 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9735 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9736 | #endif |
| 9737 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9738 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9739 | #endif |
| 9740 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9741 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9742 | #endif |
| 9743 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9744 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9745 | #endif |
| 9746 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9747 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9748 | #endif |
| 9749 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9750 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9751 | #endif |
| 9752 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9753 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9754 | #endif |
| 9755 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9756 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9757 | #endif |
| 9758 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9759 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9760 | #endif |
| 9761 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9762 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9763 | #endif |
| 9764 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9765 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9766 | #endif |
| 9767 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9768 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9769 | #endif |
| 9770 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9771 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9772 | #endif |
| 9773 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9774 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9775 | #endif |
| 9776 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9777 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9778 | #endif |
| 9779 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9780 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9781 | #endif |
| 9782 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9783 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9784 | #endif |
| 9785 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9786 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9787 | #endif |
| 9788 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9789 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9790 | #endif |
| 9791 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9792 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9793 | #endif |
| 9794 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9795 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9796 | #endif |
| 9797 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9798 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9799 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9800 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9801 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9802 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9803 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9804 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9805 | #endif |
| 9806 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9807 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9808 | #endif |
| 9809 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9810 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9811 | #endif |
| 9812 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9813 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9814 | #endif |
| 9815 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9816 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9817 | #endif |
| 9818 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9819 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9820 | #endif |
| 9821 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9822 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9823 | #endif |
| 9824 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9825 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9826 | #endif |
| 9827 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9828 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9829 | #endif |
| 9830 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9831 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9832 | #endif |
| 9833 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9834 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9835 | #endif |
| 9836 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9837 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9838 | #endif |
| 9839 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9840 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9841 | #endif |
| 9842 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9843 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9844 | #endif |
| 9845 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9846 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9847 | #endif |
| 9848 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9849 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9850 | #endif |
| 9851 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9852 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9853 | #endif |
| 9854 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9855 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9856 | #endif |
| 9857 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9858 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9859 | #endif |
| 9860 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9861 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9862 | #endif |
| 9863 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9864 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9865 | #endif |
| 9866 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9867 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9868 | #endif |
| 9869 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9870 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9871 | #endif |
| 9872 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9873 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9874 | #endif |
| 9875 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9876 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9877 | #endif |
| 9878 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9879 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9880 | #endif |
| 9881 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9882 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9883 | #endif |
| 9884 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9885 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9886 | #endif |
| 9887 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9888 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9889 | #endif |
| 9890 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9891 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9892 | #endif |
| 9893 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9894 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9895 | #endif |
| 9896 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9897 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9898 | #endif |
| 9899 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9900 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9901 | #endif |
| 9902 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9903 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9904 | #endif |
| 9905 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9906 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9907 | #endif |
| 9908 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9909 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9910 | #endif |
| 9911 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9912 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9913 | #endif |
| 9914 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9915 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9916 | #endif |
| 9917 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9918 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9919 | #endif |
| 9920 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9921 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9922 | #endif |
| 9923 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9924 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9925 | #endif |
| 9926 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9927 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9928 | #endif |
| 9929 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9930 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9931 | #endif |
| 9932 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9933 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9934 | #endif |
| 9935 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9936 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9937 | #endif |
| 9938 | }; |
| 9939 | |
| 9940 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9941 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9942 | { |
| 9943 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9944 | sizeof(posix_constants_sysconf) |
| 9945 | / sizeof(struct constdef)); |
| 9946 | } |
| 9947 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9948 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9949 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9950 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9951 | |
| 9952 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9953 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9954 | { |
| 9955 | PyObject *result = NULL; |
| 9956 | int name; |
| 9957 | |
| 9958 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9959 | int value; |
| 9960 | |
| 9961 | errno = 0; |
| 9962 | value = sysconf(name); |
| 9963 | if (value == -1 && errno != 0) |
| 9964 | posix_error(); |
| 9965 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9966 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9967 | } |
| 9968 | return result; |
| 9969 | } |
| 9970 | #endif |
| 9971 | |
| 9972 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9973 | /* This code is used to ensure that the tables of configuration value names |
| 9974 | * are in sorted order as required by conv_confname(), and also to build the |
| 9975 | * the exported dictionaries that are used to publish information about the |
| 9976 | * names available on the host platform. |
| 9977 | * |
| 9978 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9979 | * when used, even for platforms we're not able to test on. It also makes |
| 9980 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9981 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9982 | |
| 9983 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9984 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9985 | { |
| 9986 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9987 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9988 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9989 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9990 | |
| 9991 | return strcmp(c1->name, c2->name); |
| 9992 | } |
| 9993 | |
| 9994 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9995 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9996 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9997 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9998 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9999 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10000 | |
| 10001 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 10002 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10003 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10004 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10005 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 10006 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10007 | PyObject *o = PyLong_FromLong(table[i].value); |
| 10008 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 10009 | Py_XDECREF(o); |
| 10010 | Py_DECREF(d); |
| 10011 | return -1; |
| 10012 | } |
| 10013 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10014 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10015 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10016 | } |
| 10017 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10018 | /* Return -1 on failure, 0 on success. */ |
| 10019 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10020 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10021 | { |
| 10022 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10023 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10024 | sizeof(posix_constants_pathconf) |
| 10025 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10026 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10027 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10028 | #endif |
| 10029 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10030 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10031 | sizeof(posix_constants_confstr) |
| 10032 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10033 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10034 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10035 | #endif |
| 10036 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10037 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10038 | sizeof(posix_constants_sysconf) |
| 10039 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10040 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10041 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10042 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10043 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10044 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10045 | |
| 10046 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10047 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10048 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10049 | 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] | 10050 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10051 | |
| 10052 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10053 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10054 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10055 | abort(); |
| 10056 | /*NOTREACHED*/ |
| 10057 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10058 | return NULL; |
| 10059 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10060 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10061 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10062 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10063 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 10064 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10065 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10066 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10067 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10068 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10069 | application (if any) its extension is associated.\n\ |
| 10070 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10071 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10072 | \n\ |
| 10073 | startfile returns as soon as the associated application is launched.\n\ |
| 10074 | There is no option to wait for the application to close, and no way\n\ |
| 10075 | to retrieve the application's exit status.\n\ |
| 10076 | \n\ |
| 10077 | The filepath is relative to the current directory. If you want to use\n\ |
| 10078 | 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] | 10079 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10080 | |
| 10081 | static PyObject * |
| 10082 | win32_startfile(PyObject *self, PyObject *args) |
| 10083 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10084 | PyObject *ofilepath; |
| 10085 | char *filepath; |
| 10086 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10087 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10088 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10089 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10090 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10091 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10092 | &unipath, &operation)) { |
| 10093 | PyErr_Clear(); |
| 10094 | goto normal; |
| 10095 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10096 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10097 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10098 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10099 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10100 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10101 | PyErr_Clear(); |
| 10102 | operation = NULL; |
| 10103 | goto normal; |
| 10104 | } |
| 10105 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10106 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10107 | wpath = PyUnicode_AsUnicode(unipath); |
| 10108 | if (wpath == NULL) |
| 10109 | goto normal; |
| 10110 | if (uoperation) { |
| 10111 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10112 | if (woperation == NULL) |
| 10113 | goto normal; |
| 10114 | } |
| 10115 | else |
| 10116 | woperation = NULL; |
| 10117 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10118 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10119 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 10120 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10121 | Py_END_ALLOW_THREADS |
| 10122 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10123 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10124 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10125 | win32_error_object("startfile", unipath); |
| 10126 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10127 | } |
| 10128 | Py_INCREF(Py_None); |
| 10129 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10130 | |
| 10131 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10132 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10133 | PyUnicode_FSConverter, &ofilepath, |
| 10134 | &operation)) |
| 10135 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10136 | if (win32_warn_bytes_api()) { |
| 10137 | Py_DECREF(ofilepath); |
| 10138 | return NULL; |
| 10139 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10140 | filepath = PyBytes_AsString(ofilepath); |
| 10141 | Py_BEGIN_ALLOW_THREADS |
| 10142 | rc = ShellExecute((HWND)0, operation, filepath, |
| 10143 | NULL, NULL, SW_SHOWNORMAL); |
| 10144 | Py_END_ALLOW_THREADS |
| 10145 | if (rc <= (HINSTANCE)32) { |
| 10146 | PyObject *errval = win32_error("startfile", filepath); |
| 10147 | Py_DECREF(ofilepath); |
| 10148 | return errval; |
| 10149 | } |
| 10150 | Py_DECREF(ofilepath); |
| 10151 | Py_INCREF(Py_None); |
| 10152 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10153 | } |
| 10154 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10155 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10156 | #ifdef HAVE_GETLOADAVG |
| 10157 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 10158 | "getloadavg() -> (float, float, float)\n\n\ |
| 10159 | Return the number of processes in the system run queue averaged over\n\ |
| 10160 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 10161 | was unobtainable"); |
| 10162 | |
| 10163 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10164 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10165 | { |
| 10166 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10167 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10168 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10169 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10170 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10171 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10172 | } |
| 10173 | #endif |
| 10174 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10175 | PyDoc_STRVAR(device_encoding__doc__, |
| 10176 | "device_encoding(fd) -> str\n\n\ |
| 10177 | Return a string describing the encoding of the device\n\ |
| 10178 | if the output is a terminal; else return None."); |
| 10179 | |
| 10180 | static PyObject * |
| 10181 | device_encoding(PyObject *self, PyObject *args) |
| 10182 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10183 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10184 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10185 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 10186 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10187 | |
| 10188 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10189 | } |
| 10190 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10191 | #ifdef HAVE_SETRESUID |
| 10192 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 10193 | "setresuid(ruid, euid, suid)\n\n\ |
| 10194 | Set the current process's real, effective, and saved user ids."); |
| 10195 | |
| 10196 | static PyObject* |
| 10197 | posix_setresuid (PyObject *self, PyObject *args) |
| 10198 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10199 | /* We assume uid_t is no larger than a long. */ |
| 10200 | long ruid, euid, suid; |
| 10201 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 10202 | return NULL; |
| 10203 | if (setresuid(ruid, euid, suid) < 0) |
| 10204 | return posix_error(); |
| 10205 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10206 | } |
| 10207 | #endif |
| 10208 | |
| 10209 | #ifdef HAVE_SETRESGID |
| 10210 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 10211 | "setresgid(rgid, egid, sgid)\n\n\ |
| 10212 | Set the current process's real, effective, and saved group ids."); |
| 10213 | |
| 10214 | static PyObject* |
| 10215 | posix_setresgid (PyObject *self, PyObject *args) |
| 10216 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10217 | /* We assume uid_t is no larger than a long. */ |
| 10218 | long rgid, egid, sgid; |
| 10219 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 10220 | return NULL; |
| 10221 | if (setresgid(rgid, egid, sgid) < 0) |
| 10222 | return posix_error(); |
| 10223 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10224 | } |
| 10225 | #endif |
| 10226 | |
| 10227 | #ifdef HAVE_GETRESUID |
| 10228 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 10229 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 10230 | Get tuple of the current process's real, effective, and saved user ids."); |
| 10231 | |
| 10232 | static PyObject* |
| 10233 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 10234 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10235 | uid_t ruid, euid, suid; |
| 10236 | long l_ruid, l_euid, l_suid; |
| 10237 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10238 | return posix_error(); |
| 10239 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 10240 | l_ruid = ruid; |
| 10241 | l_euid = euid; |
| 10242 | l_suid = suid; |
| 10243 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10244 | } |
| 10245 | #endif |
| 10246 | |
| 10247 | #ifdef HAVE_GETRESGID |
| 10248 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 10249 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 10250 | 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] | 10251 | |
| 10252 | static PyObject* |
| 10253 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 10254 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10255 | uid_t rgid, egid, sgid; |
| 10256 | long l_rgid, l_egid, l_sgid; |
| 10257 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10258 | return posix_error(); |
| 10259 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 10260 | l_rgid = rgid; |
| 10261 | l_egid = egid; |
| 10262 | l_sgid = sgid; |
| 10263 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10264 | } |
| 10265 | #endif |
| 10266 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10267 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10268 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10269 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10270 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 10271 | Return the value of extended attribute attribute on path.\n\ |
| 10272 | \n\ |
| 10273 | path may be either a string or an open file descriptor.\n\ |
| 10274 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10275 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 10276 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10277 | |
| 10278 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10279 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10280 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10281 | path_t path; |
| 10282 | path_t attribute; |
| 10283 | int follow_symlinks = 1; |
| 10284 | PyObject *buffer = NULL; |
| 10285 | int i; |
| 10286 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10287 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10288 | memset(&path, 0, sizeof(path)); |
| 10289 | memset(&attribute, 0, sizeof(attribute)); |
| 10290 | path.allow_fd = 1; |
| 10291 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 10292 | path_converter, &path, |
| 10293 | path_converter, &attribute, |
| 10294 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10295 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10296 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10297 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 10298 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10299 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10300 | for (i = 0; ; i++) { |
| 10301 | void *ptr; |
| 10302 | ssize_t result; |
| 10303 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 10304 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10305 | if (!buffer_size) { |
| 10306 | path_error("getxattr", &path); |
| 10307 | goto exit; |
| 10308 | } |
| 10309 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10310 | if (!buffer) |
| 10311 | goto exit; |
| 10312 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10313 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10314 | Py_BEGIN_ALLOW_THREADS; |
| 10315 | if (path.fd >= 0) |
| 10316 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 10317 | else if (follow_symlinks) |
| 10318 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10319 | else |
| 10320 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10321 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10322 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10323 | if (result < 0) { |
| 10324 | Py_DECREF(buffer); |
| 10325 | buffer = NULL; |
| 10326 | if (errno == ERANGE) |
| 10327 | continue; |
| 10328 | path_error("getxattr", &path); |
| 10329 | goto exit; |
| 10330 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10331 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10332 | if (result != buffer_size) { |
| 10333 | /* Can only shrink. */ |
| 10334 | _PyBytes_Resize(&buffer, result); |
| 10335 | } |
| 10336 | break; |
| 10337 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10338 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10339 | exit: |
| 10340 | path_cleanup(&path); |
| 10341 | path_cleanup(&attribute); |
| 10342 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10343 | } |
| 10344 | |
| 10345 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10346 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 10347 | Set extended attribute attribute on path to value.\n\ |
| 10348 | path may be either a string or an open file descriptor.\n\ |
| 10349 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10350 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 10351 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10352 | |
| 10353 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10354 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10355 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10356 | path_t path; |
| 10357 | path_t attribute; |
| 10358 | Py_buffer value; |
| 10359 | int flags = 0; |
| 10360 | int follow_symlinks = 1; |
| 10361 | int result; |
| 10362 | PyObject *return_value = NULL; |
| 10363 | static char *keywords[] = {"path", "attribute", "value", |
| 10364 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10365 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10366 | memset(&path, 0, sizeof(path)); |
| 10367 | path.allow_fd = 1; |
| 10368 | memset(&attribute, 0, sizeof(attribute)); |
| 10369 | memset(&value, 0, sizeof(value)); |
| 10370 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 10371 | keywords, |
| 10372 | path_converter, &path, |
| 10373 | path_converter, &attribute, |
| 10374 | &value, &flags, |
| 10375 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10376 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10377 | |
| 10378 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 10379 | goto exit; |
| 10380 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10381 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10382 | if (path.fd > -1) |
| 10383 | result = fsetxattr(path.fd, attribute.narrow, |
| 10384 | value.buf, value.len, flags); |
| 10385 | else if (follow_symlinks) |
| 10386 | result = setxattr(path.narrow, attribute.narrow, |
| 10387 | value.buf, value.len, flags); |
| 10388 | else |
| 10389 | result = lsetxattr(path.narrow, attribute.narrow, |
| 10390 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10391 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10392 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10393 | if (result) { |
| 10394 | return_value = path_error("setxattr", &path); |
| 10395 | goto exit; |
| 10396 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10397 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10398 | return_value = Py_None; |
| 10399 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10400 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10401 | exit: |
| 10402 | path_cleanup(&path); |
| 10403 | path_cleanup(&attribute); |
| 10404 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10405 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10406 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10407 | } |
| 10408 | |
| 10409 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10410 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 10411 | Remove extended attribute attribute on path.\n\ |
| 10412 | path may be either a string or an open file descriptor.\n\ |
| 10413 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10414 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 10415 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10416 | |
| 10417 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10418 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10419 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10420 | path_t path; |
| 10421 | path_t attribute; |
| 10422 | int follow_symlinks = 1; |
| 10423 | int result; |
| 10424 | PyObject *return_value = NULL; |
| 10425 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10426 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10427 | memset(&path, 0, sizeof(path)); |
| 10428 | memset(&attribute, 0, sizeof(attribute)); |
| 10429 | path.allow_fd = 1; |
| 10430 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 10431 | keywords, |
| 10432 | path_converter, &path, |
| 10433 | path_converter, &attribute, |
| 10434 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10435 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10436 | |
| 10437 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 10438 | goto exit; |
| 10439 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10440 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10441 | if (path.fd > -1) |
| 10442 | result = fremovexattr(path.fd, attribute.narrow); |
| 10443 | else if (follow_symlinks) |
| 10444 | result = removexattr(path.narrow, attribute.narrow); |
| 10445 | else |
| 10446 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10447 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10448 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10449 | if (result) { |
| 10450 | return_value = path_error("removexattr", &path); |
| 10451 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10452 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10453 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10454 | return_value = Py_None; |
| 10455 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10456 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10457 | exit: |
| 10458 | path_cleanup(&path); |
| 10459 | path_cleanup(&attribute); |
| 10460 | |
| 10461 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10462 | } |
| 10463 | |
| 10464 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10465 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 10466 | Return a list of extended attributes on path.\n\ |
| 10467 | \n\ |
| 10468 | path may be either None, a string, or an open file descriptor.\n\ |
| 10469 | if path is None, listxattr will examine the current directory.\n\ |
| 10470 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10471 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 10472 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10473 | |
| 10474 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10475 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10476 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10477 | path_t path; |
| 10478 | int follow_symlinks = 1; |
| 10479 | Py_ssize_t i; |
| 10480 | PyObject *result = NULL; |
| 10481 | char *buffer = NULL; |
| 10482 | char *name; |
| 10483 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10484 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10485 | memset(&path, 0, sizeof(path)); |
| 10486 | path.allow_fd = 1; |
| 10487 | path.fd = -1; |
| 10488 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 10489 | path_converter, &path, |
| 10490 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10491 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10492 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10493 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 10494 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10495 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10496 | name = path.narrow ? path.narrow : "."; |
| 10497 | for (i = 0; ; i++) { |
| 10498 | char *start, *trace, *end; |
| 10499 | ssize_t length; |
| 10500 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10501 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10502 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 10503 | /* ERANGE */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10504 | path_error("listxattr", &path); |
| 10505 | break; |
| 10506 | } |
| 10507 | buffer = PyMem_MALLOC(buffer_size); |
| 10508 | if (!buffer) { |
| 10509 | PyErr_NoMemory(); |
| 10510 | break; |
| 10511 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10512 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10513 | Py_BEGIN_ALLOW_THREADS; |
| 10514 | if (path.fd > -1) |
| 10515 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10516 | else if (follow_symlinks) |
| 10517 | length = listxattr(name, buffer, buffer_size); |
| 10518 | else |
| 10519 | length = llistxattr(name, buffer, buffer_size); |
| 10520 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10521 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10522 | if (length < 0) { |
| 10523 | if (errno == ERANGE) |
| 10524 | continue; |
| 10525 | path_error("listxattr", &path); |
| 10526 | break; |
| 10527 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10528 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10529 | result = PyList_New(0); |
| 10530 | if (!result) { |
| 10531 | goto exit; |
| 10532 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10533 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10534 | end = buffer + length; |
| 10535 | for (trace = start = buffer; trace != end; trace++) { |
| 10536 | if (!*trace) { |
| 10537 | int error; |
| 10538 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10539 | trace - start); |
| 10540 | if (!attribute) { |
| 10541 | Py_DECREF(result); |
| 10542 | result = NULL; |
| 10543 | goto exit; |
| 10544 | } |
| 10545 | error = PyList_Append(result, attribute); |
| 10546 | Py_DECREF(attribute); |
| 10547 | if (error) { |
| 10548 | Py_DECREF(result); |
| 10549 | result = NULL; |
| 10550 | goto exit; |
| 10551 | } |
| 10552 | start = trace + 1; |
| 10553 | } |
| 10554 | } |
| 10555 | break; |
| 10556 | } |
| 10557 | exit: |
| 10558 | path_cleanup(&path); |
| 10559 | if (buffer) |
| 10560 | PyMem_FREE(buffer); |
| 10561 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10562 | } |
| 10563 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10564 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10565 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10566 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10567 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10568 | "urandom(n) -> str\n\n\ |
| 10569 | Return n random bytes suitable for cryptographic use."); |
| 10570 | |
| 10571 | static PyObject * |
| 10572 | posix_urandom(PyObject *self, PyObject *args) |
| 10573 | { |
| 10574 | Py_ssize_t size; |
| 10575 | PyObject *result; |
| 10576 | int ret; |
| 10577 | |
| 10578 | /* Read arguments */ |
| 10579 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10580 | return NULL; |
| 10581 | if (size < 0) |
| 10582 | return PyErr_Format(PyExc_ValueError, |
| 10583 | "negative argument not allowed"); |
| 10584 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10585 | if (result == NULL) |
| 10586 | return NULL; |
| 10587 | |
| 10588 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10589 | PyBytes_GET_SIZE(result)); |
| 10590 | if (ret == -1) { |
| 10591 | Py_DECREF(result); |
| 10592 | return NULL; |
| 10593 | } |
| 10594 | return result; |
| 10595 | } |
| 10596 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10597 | /* Terminal size querying */ |
| 10598 | |
| 10599 | static PyTypeObject TerminalSizeType; |
| 10600 | |
| 10601 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10602 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10603 | |
| 10604 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10605 | {"columns", "width of the terminal window in characters"}, |
| 10606 | {"lines", "height of the terminal window in characters"}, |
| 10607 | {NULL, NULL} |
| 10608 | }; |
| 10609 | |
| 10610 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10611 | "os.terminal_size", |
| 10612 | TerminalSize_docstring, |
| 10613 | TerminalSize_fields, |
| 10614 | 2, |
| 10615 | }; |
| 10616 | |
| 10617 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10618 | PyDoc_STRVAR(termsize__doc__, |
| 10619 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10620 | "\n" \ |
| 10621 | "The optional argument fd (default standard output) specifies\n" \ |
| 10622 | "which file descriptor should be queried.\n" \ |
| 10623 | "\n" \ |
| 10624 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10625 | "is thrown.\n" \ |
| 10626 | "\n" \ |
| 10627 | "This function will only be defined if an implementation is\n" \ |
| 10628 | "available for this system.\n" \ |
| 10629 | "\n" \ |
| 10630 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10631 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10632 | |
| 10633 | static PyObject* |
| 10634 | get_terminal_size(PyObject *self, PyObject *args) |
| 10635 | { |
| 10636 | int columns, lines; |
| 10637 | PyObject *termsize; |
| 10638 | |
| 10639 | int fd = fileno(stdout); |
| 10640 | /* Under some conditions stdout may not be connected and |
| 10641 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10642 | * GUI apps don't have valid standard streams by default. |
| 10643 | * |
| 10644 | * If this happens, and the optional fd argument is not present, |
| 10645 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10646 | */ |
| 10647 | |
| 10648 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10649 | return NULL; |
| 10650 | |
| 10651 | #ifdef TERMSIZE_USE_IOCTL |
| 10652 | { |
| 10653 | struct winsize w; |
| 10654 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10655 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10656 | columns = w.ws_col; |
| 10657 | lines = w.ws_row; |
| 10658 | } |
| 10659 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10660 | |
| 10661 | #ifdef TERMSIZE_USE_CONIO |
| 10662 | { |
| 10663 | DWORD nhandle; |
| 10664 | HANDLE handle; |
| 10665 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10666 | switch (fd) { |
| 10667 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10668 | break; |
| 10669 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10670 | break; |
| 10671 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10672 | break; |
| 10673 | default: |
| 10674 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10675 | } |
| 10676 | handle = GetStdHandle(nhandle); |
| 10677 | if (handle == NULL) |
| 10678 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10679 | if (handle == INVALID_HANDLE_VALUE) |
| 10680 | return PyErr_SetFromWindowsErr(0); |
| 10681 | |
| 10682 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10683 | return PyErr_SetFromWindowsErr(0); |
| 10684 | |
| 10685 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10686 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10687 | } |
| 10688 | #endif /* TERMSIZE_USE_CONIO */ |
| 10689 | |
| 10690 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10691 | if (termsize == NULL) |
| 10692 | return NULL; |
| 10693 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10694 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10695 | if (PyErr_Occurred()) { |
| 10696 | Py_DECREF(termsize); |
| 10697 | return NULL; |
| 10698 | } |
| 10699 | return termsize; |
| 10700 | } |
| 10701 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10702 | |
| 10703 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10704 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10705 | {"access", (PyCFunction)posix_access, |
| 10706 | METH_VARARGS | METH_KEYWORDS, |
| 10707 | posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10708 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10709 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10710 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10711 | {"chdir", (PyCFunction)posix_chdir, |
| 10712 | METH_VARARGS | METH_KEYWORDS, |
| 10713 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10714 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10715 | {"chflags", (PyCFunction)posix_chflags, |
| 10716 | METH_VARARGS | METH_KEYWORDS, |
| 10717 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10718 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10719 | {"chmod", (PyCFunction)posix_chmod, |
| 10720 | METH_VARARGS | METH_KEYWORDS, |
| 10721 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10722 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10723 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10724 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10725 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10726 | {"chown", (PyCFunction)posix_chown, |
| 10727 | METH_VARARGS | METH_KEYWORDS, |
| 10728 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10729 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10730 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10731 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10732 | #endif /* HAVE_LCHMOD */ |
| 10733 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10734 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10735 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10736 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10737 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10738 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10739 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10740 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10741 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10742 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10744 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10745 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10746 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10747 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10748 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10749 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10750 | METH_NOARGS, posix_getcwd__doc__}, |
| 10751 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10752 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10753 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10754 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 10755 | {"link", (PyCFunction)posix_link, |
| 10756 | METH_VARARGS | METH_KEYWORDS, |
| 10757 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10758 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10759 | {"listdir", (PyCFunction)posix_listdir, |
| 10760 | METH_VARARGS | METH_KEYWORDS, |
| 10761 | posix_listdir__doc__}, |
| 10762 | {"lstat", (PyCFunction)posix_lstat, |
| 10763 | METH_VARARGS | METH_KEYWORDS, |
| 10764 | posix_lstat__doc__}, |
| 10765 | {"mkdir", (PyCFunction)posix_mkdir, |
| 10766 | METH_VARARGS | METH_KEYWORDS, |
| 10767 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10768 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10769 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10770 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10771 | #ifdef HAVE_GETPRIORITY |
| 10772 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10773 | #endif /* HAVE_GETPRIORITY */ |
| 10774 | #ifdef HAVE_SETPRIORITY |
| 10775 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10776 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10777 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10778 | {"readlink", (PyCFunction)posix_readlink, |
| 10779 | METH_VARARGS | METH_KEYWORDS, |
| 10780 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10781 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10782 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10783 | {"readlink", (PyCFunction)win_readlink, |
| 10784 | METH_VARARGS | METH_KEYWORDS, |
| 10785 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10786 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10787 | {"rename", (PyCFunction)posix_rename, |
| 10788 | METH_VARARGS | METH_KEYWORDS, |
| 10789 | posix_rename__doc__}, |
| 10790 | {"replace", (PyCFunction)posix_replace, |
| 10791 | METH_VARARGS | METH_KEYWORDS, |
| 10792 | posix_replace__doc__}, |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 10793 | {"rmdir", (PyCFunction)posix_rmdir, |
| 10794 | METH_VARARGS | METH_KEYWORDS, |
| 10795 | posix_rmdir__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10796 | {"stat", (PyCFunction)posix_stat, |
| 10797 | METH_VARARGS | METH_KEYWORDS, |
| 10798 | posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10799 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10800 | #if defined(HAVE_SYMLINK) |
| 10801 | {"symlink", (PyCFunction)posix_symlink, |
| 10802 | METH_VARARGS | METH_KEYWORDS, |
| 10803 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10804 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10805 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10806 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10807 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10808 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10809 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10810 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10811 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10812 | {"unlink", (PyCFunction)posix_unlink, |
| 10813 | METH_VARARGS | METH_KEYWORDS, |
| 10814 | posix_unlink__doc__}, |
| 10815 | {"remove", (PyCFunction)posix_unlink, |
| 10816 | METH_VARARGS | METH_KEYWORDS, |
| 10817 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10818 | {"utime", (PyCFunction)posix_utime, |
| 10819 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10820 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10821 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10822 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10823 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10824 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10825 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10826 | {"execve", (PyCFunction)posix_execve, |
| 10827 | METH_VARARGS | METH_KEYWORDS, |
| 10828 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10829 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10830 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10831 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10832 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10833 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10834 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10835 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10836 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10837 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10838 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10839 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10840 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10841 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10842 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10843 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10844 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10845 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10846 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10847 | {"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] | 10848 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10849 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10850 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10851 | #endif |
| 10852 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10853 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10854 | #endif |
| 10855 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10856 | {"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] | 10857 | #endif |
| 10858 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10859 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10860 | #endif |
| 10861 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10862 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10863 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10864 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10865 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10866 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10867 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10868 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10869 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10870 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10871 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10872 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10873 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10874 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10875 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10876 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10877 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 10878 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10879 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10880 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 10881 | #endif /* HAVE_GETEUID */ |
| 10882 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10883 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10884 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10885 | #ifdef HAVE_GETGROUPLIST |
| 10886 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10887 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10888 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10889 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10890 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10891 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10892 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10893 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10894 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10895 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10896 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10897 | #endif /* HAVE_GETPPID */ |
| 10898 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10899 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10900 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10901 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10902 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10903 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10904 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10905 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10906 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10907 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10908 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10909 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10910 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10911 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10912 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10913 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10914 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10915 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10916 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10917 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10918 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10919 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10920 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10921 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10922 | #endif /* HAVE_SETEUID */ |
| 10923 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10924 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10925 | #endif /* HAVE_SETEGID */ |
| 10926 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10927 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10928 | #endif /* HAVE_SETREUID */ |
| 10929 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10930 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10931 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10932 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10933 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10934 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10935 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10936 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10937 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10938 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10939 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10940 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10941 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10942 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10943 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10944 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10945 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10946 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10947 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10948 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10949 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10950 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10951 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10952 | #endif /* HAVE_WAIT3 */ |
| 10953 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10954 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10955 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10956 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10957 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10958 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10959 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10960 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10961 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10962 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10963 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10964 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10965 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10966 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10967 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10968 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10969 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10970 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10971 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10972 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10973 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10974 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10975 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10976 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10977 | {"open", (PyCFunction)posix_open,\ |
| 10978 | METH_VARARGS | METH_KEYWORDS, |
| 10979 | posix_open__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10981 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10982 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10983 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10984 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10985 | #ifdef HAVE_LOCKF |
| 10986 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10987 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10988 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10989 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10990 | #ifdef HAVE_READV |
| 10991 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10992 | #endif |
| 10993 | #ifdef HAVE_PREAD |
| 10994 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10995 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10996 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10997 | #ifdef HAVE_WRITEV |
| 10998 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10999 | #endif |
| 11000 | #ifdef HAVE_PWRITE |
| 11001 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 11002 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11003 | #ifdef HAVE_SENDFILE |
| 11004 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 11005 | posix_sendfile__doc__}, |
| 11006 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11007 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11008 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11009 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11010 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11011 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11012 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 11013 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11014 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11015 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11016 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 11017 | METH_VARARGS | METH_KEYWORDS, |
| 11018 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11019 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 11020 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11021 | {"mknod", (PyCFunction)posix_mknod, |
| 11022 | METH_VARARGS | METH_KEYWORDS, |
| 11023 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 11024 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11025 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11026 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 11027 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 11028 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11029 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11030 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11031 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11032 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11033 | #ifdef HAVE_TRUNCATE |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11034 | {"truncate", (PyCFunction)posix_truncate, |
| 11035 | METH_VARARGS | METH_KEYWORDS, |
| 11036 | posix_truncate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11037 | #endif |
| 11038 | #ifdef HAVE_POSIX_FALLOCATE |
| 11039 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 11040 | #endif |
| 11041 | #ifdef HAVE_POSIX_FADVISE |
| 11042 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 11043 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11044 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11045 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11046 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11047 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11048 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11049 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11050 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11051 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11052 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11053 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11054 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11055 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11056 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11057 | #ifdef HAVE_SYNC |
| 11058 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 11059 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11060 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11061 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11062 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11063 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11064 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11065 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11066 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11067 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11068 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11069 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11070 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11071 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11072 | #endif /* WIFSTOPPED */ |
| 11073 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11074 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11075 | #endif /* WIFSIGNALED */ |
| 11076 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11077 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11078 | #endif /* WIFEXITED */ |
| 11079 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11080 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11081 | #endif /* WEXITSTATUS */ |
| 11082 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11083 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11084 | #endif /* WTERMSIG */ |
| 11085 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11086 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11087 | #endif /* WSTOPSIG */ |
| 11088 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11089 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11090 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11091 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11092 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11093 | {"statvfs", (PyCFunction)posix_statvfs, |
| 11094 | METH_VARARGS | METH_KEYWORDS, |
| 11095 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11096 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11097 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11098 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11099 | #endif |
| 11100 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11101 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11102 | #endif |
| 11103 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11104 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11105 | #endif |
| 11106 | #ifdef HAVE_PATHCONF |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11107 | {"pathconf", (PyCFunction)posix_pathconf, |
| 11108 | METH_VARARGS | METH_KEYWORDS, |
| 11109 | posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11110 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11111 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11112 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11114 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 11115 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 11116 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11117 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 11118 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11119 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11120 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11121 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 11122 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11123 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11124 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11125 | #endif |
| 11126 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11127 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11128 | #endif |
| 11129 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11131 | #endif |
| 11132 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11133 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11134 | #endif |
| 11135 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11136 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11137 | {"setxattr", (PyCFunction)posix_setxattr, |
| 11138 | METH_VARARGS | METH_KEYWORDS, |
| 11139 | posix_setxattr__doc__}, |
| 11140 | {"getxattr", (PyCFunction)posix_getxattr, |
| 11141 | METH_VARARGS | METH_KEYWORDS, |
| 11142 | posix_getxattr__doc__}, |
| 11143 | {"removexattr", (PyCFunction)posix_removexattr, |
| 11144 | METH_VARARGS | METH_KEYWORDS, |
| 11145 | posix_removexattr__doc__}, |
| 11146 | {"listxattr", (PyCFunction)posix_listxattr, |
| 11147 | METH_VARARGS | METH_KEYWORDS, |
| 11148 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11149 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11150 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 11151 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 11152 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11153 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11154 | }; |
| 11155 | |
| 11156 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11157 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11158 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11159 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11160 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11161 | } |
| 11162 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11163 | #if defined(PYOS_OS2) |
| 11164 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11165 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11166 | { |
| 11167 | APIRET rc; |
| 11168 | ULONG values[QSV_MAX+1]; |
| 11169 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 11170 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11171 | |
| 11172 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 11173 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11174 | Py_END_ALLOW_THREADS |
| 11175 | |
| 11176 | if (rc != NO_ERROR) { |
| 11177 | os2_error(rc); |
| 11178 | return -1; |
| 11179 | } |
| 11180 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11181 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 11182 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 11183 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 11184 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 11185 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 11186 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 11187 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11188 | |
| 11189 | switch (values[QSV_VERSION_MINOR]) { |
| 11190 | case 0: ver = "2.00"; break; |
| 11191 | case 10: ver = "2.10"; break; |
| 11192 | case 11: ver = "2.11"; break; |
| 11193 | case 30: ver = "3.00"; break; |
| 11194 | case 40: ver = "4.00"; break; |
| 11195 | case 50: ver = "5.00"; break; |
| 11196 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11197 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11198 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11199 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11200 | ver = &tmp[0]; |
| 11201 | } |
| 11202 | |
| 11203 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11204 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11205 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11206 | |
| 11207 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 11208 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 11209 | tmp[1] = ':'; |
| 11210 | tmp[2] = '\0'; |
| 11211 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11212 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11213 | } |
| 11214 | #endif |
| 11215 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11216 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11217 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11218 | enable_symlink() |
| 11219 | { |
| 11220 | HANDLE tok; |
| 11221 | TOKEN_PRIVILEGES tok_priv; |
| 11222 | LUID luid; |
| 11223 | int meth_idx = 0; |
| 11224 | |
| 11225 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11226 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11227 | |
| 11228 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11229 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11230 | |
| 11231 | tok_priv.PrivilegeCount = 1; |
| 11232 | tok_priv.Privileges[0].Luid = luid; |
| 11233 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11234 | |
| 11235 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11236 | sizeof(TOKEN_PRIVILEGES), |
| 11237 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11238 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11239 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11240 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11241 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11242 | } |
| 11243 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11244 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11245 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11246 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11247 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11248 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11249 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11250 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11251 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11252 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11253 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11254 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11255 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11256 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11257 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11258 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11259 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11260 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11261 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11262 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11263 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11264 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11265 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11266 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11267 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11268 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11269 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11270 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11271 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11272 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11273 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11274 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11275 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11276 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11277 | #endif |
| 11278 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11279 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11280 | #endif |
| 11281 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11282 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11283 | #endif |
| 11284 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11285 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11286 | #endif |
| 11287 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11288 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11289 | #endif |
| 11290 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11291 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11292 | #endif |
| 11293 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11294 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11295 | #endif |
| 11296 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11297 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11298 | #endif |
| 11299 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11300 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11301 | #endif |
| 11302 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11303 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11304 | #endif |
| 11305 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11306 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11307 | #endif |
| 11308 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11309 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11310 | #endif |
| 11311 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11312 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11313 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11314 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11315 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11316 | #endif |
| 11317 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11318 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11319 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11320 | #ifdef O_XATTR |
| 11321 | if (ins(d, "O_XATTR", (long)O_XATTR)) return -1; |
| 11322 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11323 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11324 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11325 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11326 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11327 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11328 | #endif |
| 11329 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11330 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11331 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11332 | #ifdef O_EXEC |
| 11333 | if (ins(d, "O_EXEC", (long)O_EXEC)) return -1; |
| 11334 | #endif |
| 11335 | #ifdef O_SEARCH |
| 11336 | if (ins(d, "O_SEARCH", (long)O_SEARCH)) return -1; |
| 11337 | #endif |
| 11338 | #ifdef O_TTY_INIT |
| 11339 | if (ins(d, "O_TTY_INIT", (long)O_TTY_INIT)) return -1; |
| 11340 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11341 | #ifdef PRIO_PROCESS |
| 11342 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11343 | #endif |
| 11344 | #ifdef PRIO_PGRP |
| 11345 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11346 | #endif |
| 11347 | #ifdef PRIO_USER |
| 11348 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11349 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11350 | #ifdef O_CLOEXEC |
| 11351 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11352 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11353 | #ifdef O_ACCMODE |
| 11354 | if (ins(d, "O_ACCMODE", (long)O_ACCMODE)) return -1; |
| 11355 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11356 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11357 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11358 | #ifdef SEEK_HOLE |
| 11359 | if (ins(d, "SEEK_HOLE", (long)SEEK_HOLE)) return -1; |
| 11360 | #endif |
| 11361 | #ifdef SEEK_DATA |
| 11362 | if (ins(d, "SEEK_DATA", (long)SEEK_DATA)) return -1; |
| 11363 | #endif |
| 11364 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11365 | /* MS Windows */ |
| 11366 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11367 | /* Don't inherit in child processes. */ |
| 11368 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11369 | #endif |
| 11370 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11371 | /* Optimize for short life (keep in memory). */ |
| 11372 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11373 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11374 | #endif |
| 11375 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11376 | /* Automatically delete when last handle is closed. */ |
| 11377 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11378 | #endif |
| 11379 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11380 | /* Optimize for random access. */ |
| 11381 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11382 | #endif |
| 11383 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11384 | /* Optimize for sequential access. */ |
| 11385 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11386 | #endif |
| 11387 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11388 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11389 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11390 | /* Send a SIGIO signal whenever input or output |
| 11391 | becomes available on file descriptor */ |
| 11392 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11393 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11394 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11395 | /* Direct disk access. */ |
| 11396 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11397 | #endif |
| 11398 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11399 | /* Must be a directory. */ |
| 11400 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11401 | #endif |
| 11402 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11403 | /* Do not follow links. */ |
| 11404 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11405 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11406 | #ifdef O_NOLINKS |
| 11407 | /* Fails if link count of the named file is greater than 1 */ |
| 11408 | if (ins(d, "O_NOLINKS", (long)O_NOLINKS)) return -1; |
| 11409 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11410 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11411 | /* Do not update the access time. */ |
| 11412 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11413 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11415 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11416 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11417 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11418 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11419 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11420 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11421 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11422 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11423 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11424 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11425 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11426 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11427 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11428 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11429 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11430 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11431 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11432 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11433 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11434 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11435 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11436 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11437 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11438 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11439 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11440 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11441 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11442 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11443 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11444 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11445 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11446 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11447 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11448 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11449 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11450 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11451 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11452 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11453 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11454 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11455 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11456 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11457 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11458 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11459 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11460 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11461 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11462 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11463 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11464 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11465 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11466 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11467 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11468 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11469 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11470 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11471 | #endif /* ST_RDONLY */ |
| 11472 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11473 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11474 | #endif /* ST_NOSUID */ |
| 11475 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11476 | /* FreeBSD sendfile() constants */ |
| 11477 | #ifdef SF_NODISKIO |
| 11478 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11479 | #endif |
| 11480 | #ifdef SF_MNOWAIT |
| 11481 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11482 | #endif |
| 11483 | #ifdef SF_SYNC |
| 11484 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11485 | #endif |
| 11486 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11487 | /* constants for posix_fadvise */ |
| 11488 | #ifdef POSIX_FADV_NORMAL |
| 11489 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11490 | #endif |
| 11491 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11492 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11493 | #endif |
| 11494 | #ifdef POSIX_FADV_RANDOM |
| 11495 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11496 | #endif |
| 11497 | #ifdef POSIX_FADV_NOREUSE |
| 11498 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11499 | #endif |
| 11500 | #ifdef POSIX_FADV_WILLNEED |
| 11501 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11502 | #endif |
| 11503 | #ifdef POSIX_FADV_DONTNEED |
| 11504 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11505 | #endif |
| 11506 | |
| 11507 | /* constants for waitid */ |
| 11508 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11509 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11510 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11511 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11512 | #endif |
| 11513 | #ifdef WEXITED |
| 11514 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11515 | #endif |
| 11516 | #ifdef WNOWAIT |
| 11517 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11518 | #endif |
| 11519 | #ifdef WSTOPPED |
| 11520 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11521 | #endif |
| 11522 | #ifdef CLD_EXITED |
| 11523 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11524 | #endif |
| 11525 | #ifdef CLD_DUMPED |
| 11526 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11527 | #endif |
| 11528 | #ifdef CLD_TRAPPED |
| 11529 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11530 | #endif |
| 11531 | #ifdef CLD_CONTINUED |
| 11532 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11533 | #endif |
| 11534 | |
| 11535 | /* constants for lockf */ |
| 11536 | #ifdef F_LOCK |
| 11537 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11538 | #endif |
| 11539 | #ifdef F_TLOCK |
| 11540 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11541 | #endif |
| 11542 | #ifdef F_ULOCK |
| 11543 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11544 | #endif |
| 11545 | #ifdef F_TEST |
| 11546 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11547 | #endif |
| 11548 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11549 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11550 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11551 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11552 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11553 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11554 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11555 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11556 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11557 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11558 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11559 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11560 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11561 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11562 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11563 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11564 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11565 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11566 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11567 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11568 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11569 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11570 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11571 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11572 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11573 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11574 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11575 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11576 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11577 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11578 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11579 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11580 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11581 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11582 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11583 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11584 | #ifdef SCHED_SPORADIC |
| 11585 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11586 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11587 | #ifdef SCHED_BATCH |
| 11588 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11589 | #endif |
| 11590 | #ifdef SCHED_IDLE |
| 11591 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11592 | #endif |
| 11593 | #ifdef SCHED_RESET_ON_FORK |
| 11594 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11595 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11596 | #ifdef SCHED_SYS |
| 11597 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11598 | #endif |
| 11599 | #ifdef SCHED_IA |
| 11600 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11601 | #endif |
| 11602 | #ifdef SCHED_FSS |
| 11603 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11604 | #endif |
| 11605 | #ifdef SCHED_FX |
| 11606 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11607 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11608 | #endif |
| 11609 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11610 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11611 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11612 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11613 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11614 | #endif |
| 11615 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11616 | #ifdef RTLD_LAZY |
| 11617 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11618 | #endif |
| 11619 | #ifdef RTLD_NOW |
| 11620 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11621 | #endif |
| 11622 | #ifdef RTLD_GLOBAL |
| 11623 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11624 | #endif |
| 11625 | #ifdef RTLD_LOCAL |
| 11626 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11627 | #endif |
| 11628 | #ifdef RTLD_NODELETE |
| 11629 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11630 | #endif |
| 11631 | #ifdef RTLD_NOLOAD |
| 11632 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11633 | #endif |
| 11634 | #ifdef RTLD_DEEPBIND |
| 11635 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11636 | #endif |
| 11637 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11638 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11639 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11640 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11641 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11642 | } |
| 11643 | |
| 11644 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11645 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11646 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11647 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11648 | |
| 11649 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11650 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11651 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11652 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11653 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11654 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11655 | #define MODNAME "posix" |
| 11656 | #endif |
| 11657 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11658 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11659 | PyModuleDef_HEAD_INIT, |
| 11660 | MODNAME, |
| 11661 | posix__doc__, |
| 11662 | -1, |
| 11663 | posix_methods, |
| 11664 | NULL, |
| 11665 | NULL, |
| 11666 | NULL, |
| 11667 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11668 | }; |
| 11669 | |
| 11670 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11671 | static char *have_functions[] = { |
| 11672 | |
| 11673 | #ifdef HAVE_FACCESSAT |
| 11674 | "HAVE_FACCESSAT", |
| 11675 | #endif |
| 11676 | |
| 11677 | #ifdef HAVE_FCHDIR |
| 11678 | "HAVE_FCHDIR", |
| 11679 | #endif |
| 11680 | |
| 11681 | #ifdef HAVE_FCHMOD |
| 11682 | "HAVE_FCHMOD", |
| 11683 | #endif |
| 11684 | |
| 11685 | #ifdef HAVE_FCHMODAT |
| 11686 | "HAVE_FCHMODAT", |
| 11687 | #endif |
| 11688 | |
| 11689 | #ifdef HAVE_FCHOWN |
| 11690 | "HAVE_FCHOWN", |
| 11691 | #endif |
| 11692 | |
| 11693 | #ifdef HAVE_FEXECVE |
| 11694 | "HAVE_FEXECVE", |
| 11695 | #endif |
| 11696 | |
| 11697 | #ifdef HAVE_FDOPENDIR |
| 11698 | "HAVE_FDOPENDIR", |
| 11699 | #endif |
| 11700 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11701 | #ifdef HAVE_FPATHCONF |
| 11702 | "HAVE_FPATHCONF", |
| 11703 | #endif |
| 11704 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11705 | #ifdef HAVE_FSTATAT |
| 11706 | "HAVE_FSTATAT", |
| 11707 | #endif |
| 11708 | |
| 11709 | #ifdef HAVE_FSTATVFS |
| 11710 | "HAVE_FSTATVFS", |
| 11711 | #endif |
| 11712 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11713 | #ifdef HAVE_FTRUNCATE |
| 11714 | "HAVE_FTRUNCATE", |
| 11715 | #endif |
| 11716 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11717 | #ifdef HAVE_FUTIMENS |
| 11718 | "HAVE_FUTIMENS", |
| 11719 | #endif |
| 11720 | |
| 11721 | #ifdef HAVE_FUTIMES |
| 11722 | "HAVE_FUTIMES", |
| 11723 | #endif |
| 11724 | |
| 11725 | #ifdef HAVE_FUTIMESAT |
| 11726 | "HAVE_FUTIMESAT", |
| 11727 | #endif |
| 11728 | |
| 11729 | #ifdef HAVE_LINKAT |
| 11730 | "HAVE_LINKAT", |
| 11731 | #endif |
| 11732 | |
| 11733 | #ifdef HAVE_LCHFLAGS |
| 11734 | "HAVE_LCHFLAGS", |
| 11735 | #endif |
| 11736 | |
| 11737 | #ifdef HAVE_LCHMOD |
| 11738 | "HAVE_LCHMOD", |
| 11739 | #endif |
| 11740 | |
| 11741 | #ifdef HAVE_LCHOWN |
| 11742 | "HAVE_LCHOWN", |
| 11743 | #endif |
| 11744 | |
| 11745 | #ifdef HAVE_LSTAT |
| 11746 | "HAVE_LSTAT", |
| 11747 | #endif |
| 11748 | |
| 11749 | #ifdef HAVE_LUTIMES |
| 11750 | "HAVE_LUTIMES", |
| 11751 | #endif |
| 11752 | |
| 11753 | #ifdef HAVE_MKDIRAT |
| 11754 | "HAVE_MKDIRAT", |
| 11755 | #endif |
| 11756 | |
| 11757 | #ifdef HAVE_MKFIFOAT |
| 11758 | "HAVE_MKFIFOAT", |
| 11759 | #endif |
| 11760 | |
| 11761 | #ifdef HAVE_MKNODAT |
| 11762 | "HAVE_MKNODAT", |
| 11763 | #endif |
| 11764 | |
| 11765 | #ifdef HAVE_OPENAT |
| 11766 | "HAVE_OPENAT", |
| 11767 | #endif |
| 11768 | |
| 11769 | #ifdef HAVE_READLINKAT |
| 11770 | "HAVE_READLINKAT", |
| 11771 | #endif |
| 11772 | |
| 11773 | #ifdef HAVE_RENAMEAT |
| 11774 | "HAVE_RENAMEAT", |
| 11775 | #endif |
| 11776 | |
| 11777 | #ifdef HAVE_SYMLINKAT |
| 11778 | "HAVE_SYMLINKAT", |
| 11779 | #endif |
| 11780 | |
| 11781 | #ifdef HAVE_UNLINKAT |
| 11782 | "HAVE_UNLINKAT", |
| 11783 | #endif |
| 11784 | |
| 11785 | #ifdef HAVE_UTIMENSAT |
| 11786 | "HAVE_UTIMENSAT", |
| 11787 | #endif |
| 11788 | |
| 11789 | #ifdef MS_WINDOWS |
| 11790 | "MS_WINDOWS", |
| 11791 | #endif |
| 11792 | |
| 11793 | NULL |
| 11794 | }; |
| 11795 | |
| 11796 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11797 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11798 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11799 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11800 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11801 | PyObject *list; |
| 11802 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11803 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11804 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11805 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11806 | #endif |
| 11807 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11808 | m = PyModule_Create(&posixmodule); |
| 11809 | if (m == NULL) |
| 11810 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11811 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11812 | /* Initialize environ dictionary */ |
| 11813 | v = convertenviron(); |
| 11814 | Py_XINCREF(v); |
| 11815 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11816 | return NULL; |
| 11817 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11818 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11819 | if (all_ins(m)) |
| 11820 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11821 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11822 | if (setup_confname_tables(m)) |
| 11823 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11824 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11825 | Py_INCREF(PyExc_OSError); |
| 11826 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11827 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11828 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11829 | if (posix_putenv_garbage == NULL) |
| 11830 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11831 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11832 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11833 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11834 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11835 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11836 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11837 | #endif |
| 11838 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11839 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11840 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11841 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11842 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11843 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11844 | structseq_new = StatResultType.tp_new; |
| 11845 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11846 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11847 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11848 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11849 | #ifdef NEED_TICKS_PER_SECOND |
| 11850 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11851 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11852 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11853 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11854 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11855 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11856 | # endif |
| 11857 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11858 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11859 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11860 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11861 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11862 | SchedParamType.tp_new = sched_param_new; |
| 11863 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11864 | |
| 11865 | /* initialize TerminalSize_info */ |
| 11866 | PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11867 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11868 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11869 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11870 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11871 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11872 | Py_INCREF((PyObject*) &StatResultType); |
| 11873 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11874 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11875 | PyModule_AddObject(m, "statvfs_result", |
| 11876 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11877 | |
| 11878 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11879 | Py_INCREF(&SchedParamType); |
| 11880 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11881 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11882 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 11883 | times_result_desc.name = MODNAME ".times_result"; |
| 11884 | PyStructSequence_InitType(&TimesResultType, ×_result_desc); |
| 11885 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 11886 | |
| 11887 | uname_result_desc.name = MODNAME ".uname_result"; |
| 11888 | PyStructSequence_InitType(&UnameResultType, &uname_result_desc); |
| 11889 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 11890 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11891 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11892 | /* |
| 11893 | * Step 2 of weak-linking support on Mac OS X. |
| 11894 | * |
| 11895 | * The code below removes functions that are not available on the |
| 11896 | * currently active platform. |
| 11897 | * |
| 11898 | * 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] | 11899 | * 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] | 11900 | * OSX 10.4. |
| 11901 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11902 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11903 | if (fstatvfs == NULL) { |
| 11904 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11905 | return NULL; |
| 11906 | } |
| 11907 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11908 | #endif /* HAVE_FSTATVFS */ |
| 11909 | |
| 11910 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11911 | if (statvfs == NULL) { |
| 11912 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11913 | return NULL; |
| 11914 | } |
| 11915 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11916 | #endif /* HAVE_STATVFS */ |
| 11917 | |
| 11918 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11919 | if (lchown == NULL) { |
| 11920 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11921 | return NULL; |
| 11922 | } |
| 11923 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11924 | #endif /* HAVE_LCHOWN */ |
| 11925 | |
| 11926 | |
| 11927 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11928 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 11929 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11930 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 11931 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 11932 | billion = PyLong_FromLong(1000000000); |
| 11933 | if (!billion) |
| 11934 | return NULL; |
| 11935 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11936 | /* suppress "function not used" warnings */ |
| 11937 | { |
| 11938 | int ignored; |
| 11939 | fd_specified("", -1); |
| 11940 | follow_symlinks_specified("", 1); |
| 11941 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 11942 | dir_fd_converter(Py_None, &ignored); |
| 11943 | dir_fd_unavailable(Py_None, &ignored); |
| 11944 | } |
| 11945 | |
| 11946 | /* |
| 11947 | * provide list of locally available functions |
| 11948 | * so os.py can populate support_* lists |
| 11949 | */ |
| 11950 | list = PyList_New(0); |
| 11951 | if (!list) |
| 11952 | return NULL; |
| 11953 | for (trace = have_functions; *trace; trace++) { |
| 11954 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 11955 | if (!unicode) |
| 11956 | return NULL; |
| 11957 | if (PyList_Append(list, unicode)) |
| 11958 | return NULL; |
| 11959 | Py_DECREF(unicode); |
| 11960 | } |
| 11961 | PyModule_AddObject(m, "_have_functions", list); |
| 11962 | |
| 11963 | initialized = 1; |
| 11964 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11965 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11966 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11967 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11968 | |
| 11969 | #ifdef __cplusplus |
| 11970 | } |
| 11971 | #endif |