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 |
| 417 | #define DEFAULT_DIR_FD AT_FDCWD |
| 418 | #else |
| 419 | #define DEFAULT_DIR_FD (-100) |
| 420 | #endif |
| 421 | |
| 422 | static int |
| 423 | _fd_converter(PyObject *o, int *p, int default_value) { |
| 424 | long long_value; |
| 425 | if (o == Py_None) { |
| 426 | *p = default_value; |
| 427 | return 1; |
| 428 | } |
| 429 | if (PyFloat_Check(o)) { |
| 430 | PyErr_SetString(PyExc_TypeError, |
| 431 | "integer argument expected, got float" ); |
| 432 | return 0; |
| 433 | } |
| 434 | long_value = PyLong_AsLong(o); |
| 435 | if (long_value == -1 && PyErr_Occurred()) |
| 436 | return 0; |
| 437 | if (long_value > INT_MAX) { |
| 438 | PyErr_SetString(PyExc_OverflowError, |
| 439 | "signed integer is greater than maximum"); |
| 440 | return 0; |
| 441 | } |
| 442 | if (long_value < INT_MIN) { |
| 443 | PyErr_SetString(PyExc_OverflowError, |
| 444 | "signed integer is less than minimum"); |
| 445 | return 0; |
| 446 | } |
| 447 | *p = (int)long_value; |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | static int |
| 452 | dir_fd_converter(PyObject *o, void *p) { |
| 453 | return _fd_converter(o, (int *)p, DEFAULT_DIR_FD); |
| 454 | } |
| 455 | |
| 456 | |
| 457 | |
| 458 | /* |
| 459 | * A PyArg_ParseTuple "converter" function |
| 460 | * that handles filesystem paths in the manner |
| 461 | * preferred by the os module. |
| 462 | * |
| 463 | * path_converter accepts (Unicode) strings and their |
| 464 | * subclasses, and bytes and their subclasses. What |
| 465 | * it does with the argument depends on the platform: |
| 466 | * |
| 467 | * * On Windows, if we get a (Unicode) string we |
| 468 | * extract the wchar_t * and return it; if we get |
| 469 | * bytes we extract the char * and return that. |
| 470 | * |
| 471 | * * On all other platforms, strings are encoded |
| 472 | * to bytes using PyUnicode_FSConverter, then we |
| 473 | * extract the char * from the bytes object and |
| 474 | * return that. |
| 475 | * |
| 476 | * path_converter also optionally accepts signed |
| 477 | * integers (representing open file descriptors) instead |
| 478 | * of path strings. |
| 479 | * |
| 480 | * Input fields: |
| 481 | * path.nullable |
| 482 | * If nonzero, the path is permitted to be None. |
| 483 | * path.allow_fd |
| 484 | * If nonzero, the path is permitted to be a file handle |
| 485 | * (a signed int) instead of a string. |
| 486 | * path.function_name |
| 487 | * If non-NULL, path_converter will use that as the name |
| 488 | * of the function in error messages. |
| 489 | * (If path.argument_name is NULL it omits the function name.) |
| 490 | * path.argument_name |
| 491 | * If non-NULL, path_converter will use that as the name |
| 492 | * of the parameter in error messages. |
| 493 | * (If path.argument_name is NULL it uses "path".) |
| 494 | * |
| 495 | * Output fields: |
| 496 | * path.wide |
| 497 | * Points to the path if it was expressed as Unicode |
| 498 | * and was not encoded. (Only used on Windows.) |
| 499 | * path.narrow |
| 500 | * Points to the path if it was expressed as bytes, |
| 501 | * or it was Unicode and was encoded to bytes. |
| 502 | * path.fd |
| 503 | * Contains a file descriptor if path.accept_fd was true |
| 504 | * and the caller provided a signed integer instead of any |
| 505 | * sort of string. |
| 506 | * |
| 507 | * WARNING: if your "path" parameter is optional, and is |
| 508 | * unspecified, path_converter will never get called. |
| 509 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 510 | * yourself! |
| 511 | * path.length |
| 512 | * The length of the path in characters, if specified as |
| 513 | * a string. |
| 514 | * path.object |
| 515 | * The original object passed in. |
| 516 | * path.cleanup |
| 517 | * For internal use only. May point to a temporary object. |
| 518 | * (Pay no attention to the man behind the curtain.) |
| 519 | * |
| 520 | * At most one of path.wide or path.narrow will be non-NULL. |
| 521 | * If path was None and path.nullable was set, |
| 522 | * or if path was an integer and path.allow_fd was set, |
| 523 | * both path.wide and path.narrow will be NULL |
| 524 | * and path.length will be 0. |
| 525 | * |
| 526 | * path_converter takes care to not write to the path_t |
| 527 | * unless it's successful. However it must reset the |
| 528 | * "cleanup" field each time it's called. |
| 529 | * |
| 530 | * Use as follows: |
| 531 | * path_t path; |
| 532 | * memset(&path, 0, sizeof(path)); |
| 533 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 534 | * // ... use values from path ... |
| 535 | * path_cleanup(&path); |
| 536 | * |
| 537 | * (Note that if PyArg_Parse fails you don't need to call |
| 538 | * path_cleanup(). However it is safe to do so.) |
| 539 | */ |
| 540 | typedef struct { |
| 541 | char *function_name; |
| 542 | char *argument_name; |
| 543 | int nullable; |
| 544 | int allow_fd; |
| 545 | wchar_t *wide; |
| 546 | char *narrow; |
| 547 | int fd; |
| 548 | Py_ssize_t length; |
| 549 | PyObject *object; |
| 550 | PyObject *cleanup; |
| 551 | } path_t; |
| 552 | |
| 553 | static void |
| 554 | path_cleanup(path_t *path) { |
| 555 | if (path->cleanup) { |
| 556 | Py_DECREF(path->cleanup); |
| 557 | path->cleanup = NULL; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | static int |
| 562 | path_converter(PyObject *o, void *p) { |
| 563 | path_t *path = (path_t *)p; |
| 564 | PyObject *unicode, *bytes; |
| 565 | Py_ssize_t length; |
| 566 | char *narrow; |
| 567 | |
| 568 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 569 | PyErr_Format(exc, "%s%s" fmt, \ |
| 570 | path->function_name ? path->function_name : "", \ |
| 571 | path->function_name ? ": " : "", \ |
| 572 | path->argument_name ? path->argument_name : "path") |
| 573 | |
| 574 | /* Py_CLEANUP_SUPPORTED support */ |
| 575 | if (o == NULL) { |
| 576 | path_cleanup(path); |
| 577 | return 1; |
| 578 | } |
| 579 | |
| 580 | /* ensure it's always safe to call path_cleanup() */ |
| 581 | path->cleanup = NULL; |
| 582 | |
| 583 | if (o == Py_None) { |
| 584 | if (!path->nullable) { |
| 585 | FORMAT_EXCEPTION(PyExc_TypeError, |
| 586 | "can't specify None for %s argument"); |
| 587 | return 0; |
| 588 | } |
| 589 | path->wide = NULL; |
| 590 | path->narrow = NULL; |
| 591 | path->length = 0; |
| 592 | path->object = o; |
| 593 | path->fd = -1; |
| 594 | return 1; |
| 595 | } |
| 596 | |
| 597 | unicode = PyUnicode_FromObject(o); |
| 598 | if (unicode) { |
| 599 | #ifdef MS_WINDOWS |
| 600 | wchar_t *wide; |
| 601 | length = PyUnicode_GET_SIZE(unicode); |
| 602 | if (length > 32767) { |
| 603 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 604 | Py_DECREF(unicode); |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | wide = PyUnicode_AsUnicode(unicode); |
| 609 | if (!wide) { |
| 610 | Py_DECREF(unicode); |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | path->wide = wide; |
| 615 | path->narrow = NULL; |
| 616 | path->length = length; |
| 617 | path->object = o; |
| 618 | path->fd = -1; |
| 619 | path->cleanup = unicode; |
| 620 | return Py_CLEANUP_SUPPORTED; |
| 621 | #else |
| 622 | int converted = PyUnicode_FSConverter(unicode, &bytes); |
| 623 | Py_DECREF(unicode); |
| 624 | if (!converted) |
| 625 | bytes = NULL; |
| 626 | #endif |
| 627 | } |
| 628 | else { |
| 629 | PyErr_Clear(); |
| 630 | bytes = PyBytes_FromObject(o); |
| 631 | if (!bytes) { |
| 632 | PyErr_Clear(); |
| 633 | if (path->allow_fd) { |
| 634 | int fd; |
| 635 | /* |
| 636 | * note: _fd_converter always permits None. |
| 637 | * but we've already done our None check. |
| 638 | * so o cannot be None at this point. |
| 639 | */ |
| 640 | int result = _fd_converter(o, &fd, -1); |
| 641 | if (result) { |
| 642 | path->wide = NULL; |
| 643 | path->narrow = NULL; |
| 644 | path->length = 0; |
| 645 | path->object = o; |
| 646 | path->fd = fd; |
| 647 | return result; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | if (!bytes) { |
| 654 | if (!PyErr_Occurred()) |
| 655 | FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter"); |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | #ifdef MS_WINDOWS |
| 660 | if (win32_warn_bytes_api()) { |
| 661 | Py_DECREF(bytes); |
| 662 | return 0; |
| 663 | } |
| 664 | #endif |
| 665 | |
| 666 | length = PyBytes_GET_SIZE(bytes); |
| 667 | #ifdef MS_WINDOWS |
| 668 | if (length > MAX_PATH) { |
| 669 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
| 670 | Py_DECREF(bytes); |
| 671 | return 0; |
| 672 | } |
| 673 | #endif |
| 674 | |
| 675 | narrow = PyBytes_AS_STRING(bytes); |
| 676 | if (length != strlen(narrow)) { |
| 677 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); |
| 678 | Py_DECREF(bytes); |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | path->wide = NULL; |
| 683 | path->narrow = narrow; |
| 684 | path->length = length; |
| 685 | path->object = o; |
| 686 | path->fd = -1; |
| 687 | path->cleanup = bytes; |
| 688 | return Py_CLEANUP_SUPPORTED; |
| 689 | } |
| 690 | |
| 691 | static void |
| 692 | argument_unavailable_error(char *function_name, char *argument_name) { |
| 693 | PyErr_Format(PyExc_NotImplementedError, |
| 694 | "%s%s%s unavailable on this platform", |
| 695 | (function_name != NULL) ? function_name : "", |
| 696 | (function_name != NULL) ? ": ": "", |
| 697 | argument_name); |
| 698 | } |
| 699 | |
| 700 | static int |
| 701 | dir_fd_unavailable(PyObject *o, void *p) { |
| 702 | int *dir_fd = (int *)p; |
| 703 | int return_value = _fd_converter(o, dir_fd, DEFAULT_DIR_FD); |
| 704 | if (!return_value) |
| 705 | return 0; |
| 706 | if (*dir_fd == DEFAULT_DIR_FD) |
| 707 | return 1; |
| 708 | argument_unavailable_error(NULL, "dir_fd"); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static int |
| 713 | fd_specified(char *function_name, int fd) { |
| 714 | if (fd == -1) |
| 715 | return 0; |
| 716 | |
| 717 | argument_unavailable_error(function_name, "fd"); |
| 718 | return 1; |
| 719 | } |
| 720 | |
| 721 | static int |
| 722 | follow_symlinks_specified(char *function_name, int follow_symlinks) { |
| 723 | if (follow_symlinks) |
| 724 | return 0; |
| 725 | |
| 726 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 727 | return 1; |
| 728 | } |
| 729 | |
| 730 | static int |
| 731 | path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) { |
| 732 | if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) { |
| 733 | PyErr_Format(PyExc_ValueError, |
| 734 | "%s: can't specify dir_fd without matching path", |
| 735 | function_name); |
| 736 | return 1; |
| 737 | } |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static int |
| 742 | dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) { |
| 743 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 744 | PyErr_Format(PyExc_ValueError, |
| 745 | "%s: can't specify both dir_fd and fd", |
| 746 | function_name); |
| 747 | return 1; |
| 748 | } |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int |
| 753 | fd_and_follow_symlinks_invalid(char *function_name, int fd, |
| 754 | int follow_symlinks) { |
| 755 | if ((fd > 0) && (!follow_symlinks)) { |
| 756 | PyErr_Format(PyExc_ValueError, |
| 757 | "%s: cannot use fd and follow_symlinks together", |
| 758 | function_name); |
| 759 | return 1; |
| 760 | } |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | static int |
| 765 | dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd, |
| 766 | int follow_symlinks) { |
| 767 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 768 | PyErr_Format(PyExc_ValueError, |
| 769 | "%s: cannot use dir_fd and follow_symlinks together", |
| 770 | function_name); |
| 771 | return 1; |
| 772 | } |
| 773 | return 0; |
| 774 | } |
| 775 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 776 | /* A helper used by a number of POSIX-only functions */ |
| 777 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 778 | static int |
| 779 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 780 | { |
| 781 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 782 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 783 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 784 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 785 | #endif |
| 786 | if (PyErr_Occurred()) |
| 787 | return 0; |
| 788 | return 1; |
| 789 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 790 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 791 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 792 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 793 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 794 | * valid and throw an assertion if it isn't. |
| 795 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 796 | * an assertion can be useful, but it does contradict the POSIX standard |
| 797 | * which for write(2) states: |
| 798 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 799 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 800 | * writing." |
| 801 | * Furthermore, python allows the user to enter any old integer |
| 802 | * as a fd and should merely raise a python exception on error. |
| 803 | * The Microsoft CRT doesn't provide an official way to check for the |
| 804 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 805 | * 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] | 806 | * internal structures involved. |
| 807 | * The structures below must be updated for each version of visual studio |
| 808 | * according to the file internal.h in the CRT source, until MS comes |
| 809 | * up with a less hacky way to do this. |
| 810 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 811 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 812 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 813 | /* The actual size of the structure is determined at runtime. |
| 814 | * Only the first items must be present. |
| 815 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 816 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 817 | intptr_t osfhnd; |
| 818 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 819 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 820 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 821 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 822 | #define IOINFO_L2E 5 |
| 823 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 824 | #define IOINFO_ARRAYS 64 |
| 825 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 826 | #define FOPEN 0x01 |
| 827 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 828 | |
| 829 | /* This function emulates what the windows CRT does to validate file handles */ |
| 830 | int |
| 831 | _PyVerify_fd(int fd) |
| 832 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 833 | const int i1 = fd >> IOINFO_L2E; |
| 834 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 835 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 836 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 838 | /* Determine the actual size of the ioinfo structure, |
| 839 | * as used by the CRT loaded in memory |
| 840 | */ |
| 841 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 842 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 843 | } |
| 844 | if (sizeof_ioinfo == 0) { |
| 845 | /* This should not happen... */ |
| 846 | goto fail; |
| 847 | } |
| 848 | |
| 849 | /* See that it isn't a special CLEAR fileno */ |
| 850 | if (fd != _NO_CONSOLE_FILENO) { |
| 851 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 852 | * we check pointer validity and other info |
| 853 | */ |
| 854 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 855 | /* finally, check that the file is open */ |
| 856 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 857 | if (info->osfile & FOPEN) { |
| 858 | return 1; |
| 859 | } |
| 860 | } |
| 861 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 862 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 863 | errno = EBADF; |
| 864 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 868 | static int |
| 869 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 870 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 871 | if (!_PyVerify_fd(fd1)) |
| 872 | return 0; |
| 873 | if (fd2 == _NO_CONSOLE_FILENO) |
| 874 | return 0; |
| 875 | if ((unsigned)fd2 < _NHANDLE_) |
| 876 | return 1; |
| 877 | else |
| 878 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 879 | } |
| 880 | #else |
| 881 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 882 | #define _PyVerify_fd_dup2(A, B) (1) |
| 883 | #endif |
| 884 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 885 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 886 | /* The following structure was copied from |
| 887 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 888 | include doesn't seem to be present in the Windows SDK (at least as included |
| 889 | with Visual Studio Express). */ |
| 890 | typedef struct _REPARSE_DATA_BUFFER { |
| 891 | ULONG ReparseTag; |
| 892 | USHORT ReparseDataLength; |
| 893 | USHORT Reserved; |
| 894 | union { |
| 895 | struct { |
| 896 | USHORT SubstituteNameOffset; |
| 897 | USHORT SubstituteNameLength; |
| 898 | USHORT PrintNameOffset; |
| 899 | USHORT PrintNameLength; |
| 900 | ULONG Flags; |
| 901 | WCHAR PathBuffer[1]; |
| 902 | } SymbolicLinkReparseBuffer; |
| 903 | |
| 904 | struct { |
| 905 | USHORT SubstituteNameOffset; |
| 906 | USHORT SubstituteNameLength; |
| 907 | USHORT PrintNameOffset; |
| 908 | USHORT PrintNameLength; |
| 909 | WCHAR PathBuffer[1]; |
| 910 | } MountPointReparseBuffer; |
| 911 | |
| 912 | struct { |
| 913 | UCHAR DataBuffer[1]; |
| 914 | } GenericReparseBuffer; |
| 915 | }; |
| 916 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 917 | |
| 918 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 919 | GenericReparseBuffer) |
| 920 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 921 | |
| 922 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 923 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 924 | { |
| 925 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 926 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 927 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 928 | |
| 929 | if (0 == DeviceIoControl( |
| 930 | reparse_point_handle, |
| 931 | FSCTL_GET_REPARSE_POINT, |
| 932 | NULL, 0, /* in buffer */ |
| 933 | target_buffer, sizeof(target_buffer), |
| 934 | &n_bytes_returned, |
| 935 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 936 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 937 | |
| 938 | if (reparse_tag) |
| 939 | *reparse_tag = rdb->ReparseTag; |
| 940 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 941 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 942 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 943 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 944 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 945 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 946 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 947 | #ifdef WITH_NEXT_FRAMEWORK |
| 948 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 949 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 950 | */ |
| 951 | #include <crt_externs.h> |
| 952 | static char **environ; |
| 953 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 954 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 955 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 956 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 957 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 958 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 959 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 960 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 961 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 962 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 963 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 964 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 965 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 966 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 967 | APIRET rc; |
| 968 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 969 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 970 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 971 | d = PyDict_New(); |
| 972 | if (d == NULL) |
| 973 | return NULL; |
| 974 | #ifdef WITH_NEXT_FRAMEWORK |
| 975 | if (environ == NULL) |
| 976 | environ = *_NSGetEnviron(); |
| 977 | #endif |
| 978 | #ifdef MS_WINDOWS |
| 979 | /* _wenviron must be initialized in this way if the program is started |
| 980 | through main() instead of wmain(). */ |
| 981 | _wgetenv(L""); |
| 982 | if (_wenviron == NULL) |
| 983 | return d; |
| 984 | /* This part ignores errors */ |
| 985 | for (e = _wenviron; *e != NULL; e++) { |
| 986 | PyObject *k; |
| 987 | PyObject *v; |
| 988 | wchar_t *p = wcschr(*e, L'='); |
| 989 | if (p == NULL) |
| 990 | continue; |
| 991 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 992 | if (k == NULL) { |
| 993 | PyErr_Clear(); |
| 994 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 995 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 996 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 997 | if (v == NULL) { |
| 998 | PyErr_Clear(); |
| 999 | Py_DECREF(k); |
| 1000 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1001 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1002 | if (PyDict_GetItem(d, k) == NULL) { |
| 1003 | if (PyDict_SetItem(d, k, v) != 0) |
| 1004 | PyErr_Clear(); |
| 1005 | } |
| 1006 | Py_DECREF(k); |
| 1007 | Py_DECREF(v); |
| 1008 | } |
| 1009 | #else |
| 1010 | if (environ == NULL) |
| 1011 | return d; |
| 1012 | /* This part ignores errors */ |
| 1013 | for (e = environ; *e != NULL; e++) { |
| 1014 | PyObject *k; |
| 1015 | PyObject *v; |
| 1016 | char *p = strchr(*e, '='); |
| 1017 | if (p == NULL) |
| 1018 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1019 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1020 | if (k == NULL) { |
| 1021 | PyErr_Clear(); |
| 1022 | continue; |
| 1023 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1024 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1025 | if (v == NULL) { |
| 1026 | PyErr_Clear(); |
| 1027 | Py_DECREF(k); |
| 1028 | continue; |
| 1029 | } |
| 1030 | if (PyDict_GetItem(d, k) == NULL) { |
| 1031 | if (PyDict_SetItem(d, k, v) != 0) |
| 1032 | PyErr_Clear(); |
| 1033 | } |
| 1034 | Py_DECREF(k); |
| 1035 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1036 | } |
| 1037 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1038 | #if defined(PYOS_OS2) |
| 1039 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 1040 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 1041 | PyObject *v = PyBytes_FromString(buffer); |
| 1042 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 1043 | Py_DECREF(v); |
| 1044 | } |
| 1045 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 1046 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 1047 | PyObject *v = PyBytes_FromString(buffer); |
| 1048 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 1049 | Py_DECREF(v); |
| 1050 | } |
| 1051 | #endif |
| 1052 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1055 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1056 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1057 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1058 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1059 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1060 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1061 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1062 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1063 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1064 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1065 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1068 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1069 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1070 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1071 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 1072 | PyObject *name_str, *rc; |
| 1073 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 1074 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1075 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 1076 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 1077 | name_str); |
| 1078 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1079 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1082 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1083 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1084 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1085 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1086 | /* XXX We should pass the function name along in the future. |
| 1087 | (winreg.c also wants to pass the function name.) |
| 1088 | This would however require an additional param to the |
| 1089 | Windows error object, which is non-trivial. |
| 1090 | */ |
| 1091 | errno = GetLastError(); |
| 1092 | if (filename) |
| 1093 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1094 | else |
| 1095 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1096 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1097 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1098 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1099 | win32_error_unicode(char* function, wchar_t* filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1100 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1101 | /* XXX - see win32_error for comments on 'function' */ |
| 1102 | errno = GetLastError(); |
| 1103 | if (filename) |
| 1104 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 1105 | else |
| 1106 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1109 | static PyObject * |
| 1110 | win32_error_object(char* function, PyObject* filename) |
| 1111 | { |
| 1112 | /* XXX - see win32_error for comments on 'function' */ |
| 1113 | errno = GetLastError(); |
| 1114 | if (filename) |
| 1115 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1116 | PyExc_WindowsError, |
| 1117 | errno, |
| 1118 | filename); |
| 1119 | else |
| 1120 | return PyErr_SetFromWindowsErr(errno); |
| 1121 | } |
| 1122 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1123 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1124 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1125 | /* |
| 1126 | * Some functions return Win32 errors, others only ever use posix_error |
| 1127 | * (this is for backwards compatibility with exceptions) |
| 1128 | */ |
| 1129 | static PyObject * |
| 1130 | path_posix_error(char *function_name, path_t *path) |
| 1131 | { |
| 1132 | if (path->narrow) |
| 1133 | return posix_error_with_filename(path->narrow); |
| 1134 | return posix_error(); |
| 1135 | } |
| 1136 | |
| 1137 | static PyObject * |
| 1138 | path_error(char *function_name, path_t *path) |
| 1139 | { |
| 1140 | #ifdef MS_WINDOWS |
| 1141 | if (path->narrow) |
| 1142 | return win32_error(function_name, path->narrow); |
| 1143 | if (path->wide) |
| 1144 | return win32_error_unicode(function_name, path->wide); |
| 1145 | return win32_error(function_name, NULL); |
| 1146 | #else |
| 1147 | return path_posix_error(function_name, path); |
| 1148 | #endif |
| 1149 | } |
| 1150 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1151 | #if defined(PYOS_OS2) |
| 1152 | /********************************************************************** |
| 1153 | * Helper Function to Trim and Format OS/2 Messages |
| 1154 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1155 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1156 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 1157 | { |
| 1158 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 1159 | |
| 1160 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 1161 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 1162 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 1163 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1164 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 1165 | } |
| 1166 | |
| 1167 | /* Add Optional Reason Text */ |
| 1168 | if (reason) { |
| 1169 | strcat(msgbuf, " : "); |
| 1170 | strcat(msgbuf, reason); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | /********************************************************************** |
| 1175 | * Decode an OS/2 Operating System Error Code |
| 1176 | * |
| 1177 | * A convenience function to lookup an OS/2 error code and return a |
| 1178 | * text message we can use to raise a Python exception. |
| 1179 | * |
| 1180 | * Notes: |
| 1181 | * The messages for errors returned from the OS/2 kernel reside in |
| 1182 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 1183 | * |
| 1184 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1185 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1186 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 1187 | { |
| 1188 | APIRET rc; |
| 1189 | ULONG msglen; |
| 1190 | |
| 1191 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 1192 | Py_BEGIN_ALLOW_THREADS |
| 1193 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 1194 | errorcode, "oso001.msg", &msglen); |
| 1195 | Py_END_ALLOW_THREADS |
| 1196 | |
| 1197 | if (rc == NO_ERROR) |
| 1198 | os2_formatmsg(msgbuf, msglen, reason); |
| 1199 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 1200 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1201 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1202 | |
| 1203 | return msgbuf; |
| 1204 | } |
| 1205 | |
| 1206 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 1207 | errors are not in a global variable e.g. 'errno' nor are |
| 1208 | they congruent with posix error numbers. */ |
| 1209 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1210 | static PyObject * |
| 1211 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1212 | { |
| 1213 | char text[1024]; |
| 1214 | PyObject *v; |
| 1215 | |
| 1216 | os2_strerror(text, sizeof(text), code, ""); |
| 1217 | |
| 1218 | v = Py_BuildValue("(is)", code, text); |
| 1219 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 1220 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1221 | Py_DECREF(v); |
| 1222 | } |
| 1223 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 1224 | } |
| 1225 | |
| 1226 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1227 | |
| 1228 | /* POSIX generic methods */ |
| 1229 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1230 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1231 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1232 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1233 | int fd; |
| 1234 | int res; |
| 1235 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1236 | if (fd < 0) |
| 1237 | return NULL; |
| 1238 | if (!_PyVerify_fd(fd)) |
| 1239 | return posix_error(); |
| 1240 | Py_BEGIN_ALLOW_THREADS |
| 1241 | res = (*func)(fd); |
| 1242 | Py_END_ALLOW_THREADS |
| 1243 | if (res < 0) |
| 1244 | return posix_error(); |
| 1245 | Py_INCREF(Py_None); |
| 1246 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1247 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1248 | |
| 1249 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1250 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1251 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1252 | PyObject *opath1 = NULL; |
| 1253 | char *path1; |
| 1254 | int res; |
| 1255 | if (!PyArg_ParseTuple(args, format, |
| 1256 | PyUnicode_FSConverter, &opath1)) |
| 1257 | return NULL; |
| 1258 | path1 = PyBytes_AsString(opath1); |
| 1259 | Py_BEGIN_ALLOW_THREADS |
| 1260 | res = (*func)(path1); |
| 1261 | Py_END_ALLOW_THREADS |
| 1262 | if (res < 0) |
| 1263 | return posix_error_with_allocated_filename(opath1); |
| 1264 | Py_DECREF(opath1); |
| 1265 | Py_INCREF(Py_None); |
| 1266 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1269 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1270 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1271 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1272 | win32_1str(PyObject* args, char* func, |
| 1273 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 1274 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1275 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1276 | PyObject *uni; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1277 | const char *ansi; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1278 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1279 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1280 | if (PyArg_ParseTuple(args, wformat, &uni)) |
| 1281 | { |
| 1282 | wchar_t *wstr = PyUnicode_AsUnicode(uni); |
| 1283 | if (wstr == NULL) |
| 1284 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1285 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1286 | result = funcW(wstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1287 | Py_END_ALLOW_THREADS |
| 1288 | if (!result) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1289 | return win32_error_object(func, uni); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1290 | Py_INCREF(Py_None); |
| 1291 | return Py_None; |
| 1292 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1293 | PyErr_Clear(); |
| 1294 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1295 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 1296 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1297 | if (win32_warn_bytes_api()) |
| 1298 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1299 | Py_BEGIN_ALLOW_THREADS |
| 1300 | result = funcA(ansi); |
| 1301 | Py_END_ALLOW_THREADS |
| 1302 | if (!result) |
| 1303 | return win32_error(func, ansi); |
| 1304 | Py_INCREF(Py_None); |
| 1305 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1306 | |
| 1307 | } |
| 1308 | |
| 1309 | /* This is a reimplementation of the C library's chdir function, |
| 1310 | but one that produces Win32 errors instead of DOS error codes. |
| 1311 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1312 | it also needs to set "magic" environment variables indicating |
| 1313 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1314 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1315 | win32_chdir(LPCSTR path) |
| 1316 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1317 | char new_path[MAX_PATH+1]; |
| 1318 | int result; |
| 1319 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1320 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1321 | if(!SetCurrentDirectoryA(path)) |
| 1322 | return FALSE; |
| 1323 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 1324 | if (!result) |
| 1325 | return FALSE; |
| 1326 | /* In the ANSI API, there should not be any paths longer |
| 1327 | than MAX_PATH. */ |
| 1328 | assert(result <= MAX_PATH+1); |
| 1329 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1330 | strncmp(new_path, "//", 2) == 0) |
| 1331 | /* UNC path, nothing to do. */ |
| 1332 | return TRUE; |
| 1333 | env[1] = new_path[0]; |
| 1334 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* The Unicode version differs from the ANSI version |
| 1338 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1339 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1340 | win32_wchdir(LPCWSTR path) |
| 1341 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1342 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 1343 | int result; |
| 1344 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1345 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1346 | if(!SetCurrentDirectoryW(path)) |
| 1347 | return FALSE; |
| 1348 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 1349 | if (!result) |
| 1350 | return FALSE; |
| 1351 | if (result > MAX_PATH+1) { |
| 1352 | new_path = malloc(result * sizeof(wchar_t)); |
| 1353 | if (!new_path) { |
| 1354 | SetLastError(ERROR_OUTOFMEMORY); |
| 1355 | return FALSE; |
| 1356 | } |
| 1357 | result = GetCurrentDirectoryW(result, new_path); |
| 1358 | if (!result) { |
| 1359 | free(new_path); |
| 1360 | return FALSE; |
| 1361 | } |
| 1362 | } |
| 1363 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1364 | wcsncmp(new_path, L"//", 2) == 0) |
| 1365 | /* UNC path, nothing to do. */ |
| 1366 | return TRUE; |
| 1367 | env[1] = new_path[0]; |
| 1368 | result = SetEnvironmentVariableW(env, new_path); |
| 1369 | if (new_path != _new_path) |
| 1370 | free(new_path); |
| 1371 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1372 | } |
| 1373 | #endif |
| 1374 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1375 | #ifdef MS_WINDOWS |
| 1376 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1377 | - time stamps are restricted to second resolution |
| 1378 | - file modification times suffer from forth-and-back conversions between |
| 1379 | UTC and local time |
| 1380 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1381 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1382 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1383 | |
| 1384 | struct win32_stat{ |
| 1385 | int st_dev; |
| 1386 | __int64 st_ino; |
| 1387 | unsigned short st_mode; |
| 1388 | int st_nlink; |
| 1389 | int st_uid; |
| 1390 | int st_gid; |
| 1391 | int st_rdev; |
| 1392 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1393 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1394 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1395 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1396 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1397 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1398 | int st_ctime_nsec; |
| 1399 | }; |
| 1400 | |
| 1401 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1402 | |
| 1403 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1404 | 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] | 1405 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1406 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1407 | /* Cannot simply cast and dereference in_ptr, |
| 1408 | since it might not be aligned properly */ |
| 1409 | __int64 in; |
| 1410 | memcpy(&in, in_ptr, sizeof(in)); |
| 1411 | *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] | 1412 | *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] | 1413 | } |
| 1414 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1415 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1416 | 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] | 1417 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1418 | /* XXX endianness */ |
| 1419 | __int64 out; |
| 1420 | out = time_in + secs_between_epochs; |
| 1421 | out = out * 10000000 + nsec_in / 100; |
| 1422 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1425 | /* Below, we *know* that ugo+r is 0444 */ |
| 1426 | #if _S_IREAD != 0400 |
| 1427 | #error Unsupported C library |
| 1428 | #endif |
| 1429 | static int |
| 1430 | attributes_to_mode(DWORD attr) |
| 1431 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1432 | int m = 0; |
| 1433 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1434 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1435 | else |
| 1436 | m |= _S_IFREG; |
| 1437 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1438 | m |= 0444; |
| 1439 | else |
| 1440 | m |= 0666; |
| 1441 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1445 | 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] | 1446 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1447 | memset(result, 0, sizeof(*result)); |
| 1448 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1449 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1450 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1451 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1452 | 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] | 1453 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1454 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1455 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1456 | /* first clear the S_IFMT bits */ |
| 1457 | result->st_mode ^= (result->st_mode & 0170000); |
| 1458 | /* now set the bits that make this a symlink */ |
| 1459 | result->st_mode |= 0120000; |
| 1460 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1461 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1462 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1465 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1466 | 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] | 1467 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1468 | HANDLE hFindFile; |
| 1469 | WIN32_FIND_DATAA FileData; |
| 1470 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1471 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1472 | return FALSE; |
| 1473 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1474 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1475 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1476 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1477 | info->ftCreationTime = FileData.ftCreationTime; |
| 1478 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1479 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1480 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1481 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1482 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1483 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1484 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1485 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1489 | 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] | 1490 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1491 | HANDLE hFindFile; |
| 1492 | WIN32_FIND_DATAW FileData; |
| 1493 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1494 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1495 | return FALSE; |
| 1496 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1497 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1498 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1499 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1500 | info->ftCreationTime = FileData.ftCreationTime; |
| 1501 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1502 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1503 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1504 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1505 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1506 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1507 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1508 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1511 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1512 | static int has_GetFinalPathNameByHandle = 0; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1513 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1514 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1515 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1516 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1517 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1518 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1519 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1520 | DWORD); |
| 1521 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1522 | /* only recheck */ |
| 1523 | if (!has_GetFinalPathNameByHandle) |
| 1524 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1525 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1526 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1527 | "GetFinalPathNameByHandleA"); |
| 1528 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1529 | "GetFinalPathNameByHandleW"); |
| 1530 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1531 | Py_GetFinalPathNameByHandleW; |
| 1532 | } |
| 1533 | return has_GetFinalPathNameByHandle; |
| 1534 | } |
| 1535 | |
| 1536 | static BOOL |
| 1537 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1538 | { |
| 1539 | int buf_size, result_length; |
| 1540 | wchar_t *buf; |
| 1541 | |
| 1542 | /* We have a good handle to the target, use it to determine |
| 1543 | the target path name (then we'll call lstat on it). */ |
| 1544 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1545 | VOLUME_NAME_DOS); |
| 1546 | if(!buf_size) |
| 1547 | return FALSE; |
| 1548 | |
| 1549 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1550 | if (!buf) { |
| 1551 | SetLastError(ERROR_OUTOFMEMORY); |
| 1552 | return FALSE; |
| 1553 | } |
| 1554 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1555 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1556 | buf, buf_size, VOLUME_NAME_DOS); |
| 1557 | |
| 1558 | if(!result_length) { |
| 1559 | free(buf); |
| 1560 | return FALSE; |
| 1561 | } |
| 1562 | |
| 1563 | if(!CloseHandle(hdl)) { |
| 1564 | free(buf); |
| 1565 | return FALSE; |
| 1566 | } |
| 1567 | |
| 1568 | buf[result_length] = 0; |
| 1569 | |
| 1570 | *target_path = buf; |
| 1571 | return TRUE; |
| 1572 | } |
| 1573 | |
| 1574 | static int |
| 1575 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1576 | BOOL traverse); |
| 1577 | static int |
| 1578 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1579 | BOOL traverse) |
| 1580 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1581 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1582 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1583 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1584 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1585 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1586 | const char *dot; |
| 1587 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1588 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1589 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1590 | traverse reparse point. */ |
| 1591 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1594 | hFile = CreateFileA( |
| 1595 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1596 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1597 | 0, /* share mode */ |
| 1598 | NULL, /* security attributes */ |
| 1599 | OPEN_EXISTING, |
| 1600 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1601 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1602 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1603 | the symlink path agin and not the actual final path. */ |
| 1604 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1605 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1606 | NULL); |
| 1607 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1608 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1609 | /* Either the target doesn't exist, or we don't have access to |
| 1610 | get a handle to it. If the former, we need to return an error. |
| 1611 | If the latter, we can use attributes_from_dir. */ |
| 1612 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1613 | return -1; |
| 1614 | /* Could not get attributes on open file. Fall back to |
| 1615 | reading the directory. */ |
| 1616 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1617 | /* Very strange. This should not fail now */ |
| 1618 | return -1; |
| 1619 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1620 | if (traverse) { |
| 1621 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1622 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1623 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1624 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1625 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1626 | } else { |
| 1627 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1628 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1629 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1630 | } |
| 1631 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1632 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1633 | return -1; |
| 1634 | |
| 1635 | /* Close the outer open file handle now that we're about to |
| 1636 | reopen it with different flags. */ |
| 1637 | if (!CloseHandle(hFile)) |
| 1638 | return -1; |
| 1639 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1640 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1641 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1642 | the file without the reparse handling flag set. */ |
| 1643 | hFile2 = CreateFileA( |
| 1644 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1645 | NULL, OPEN_EXISTING, |
| 1646 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1647 | NULL); |
| 1648 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1649 | return -1; |
| 1650 | |
| 1651 | if (!get_target_path(hFile2, &target_path)) |
| 1652 | return -1; |
| 1653 | |
| 1654 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1655 | free(target_path); |
| 1656 | return code; |
| 1657 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1658 | } else |
| 1659 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1660 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1661 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1662 | |
| 1663 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1664 | dot = strrchr(path, '.'); |
| 1665 | if (dot) { |
| 1666 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1667 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1668 | result->st_mode |= 0111; |
| 1669 | } |
| 1670 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1674 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1675 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1676 | { |
| 1677 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1678 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1679 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1680 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1681 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1682 | const wchar_t *dot; |
| 1683 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1684 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1685 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1686 | traverse reparse point. */ |
| 1687 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1690 | hFile = CreateFileW( |
| 1691 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1692 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1693 | 0, /* share mode */ |
| 1694 | NULL, /* security attributes */ |
| 1695 | OPEN_EXISTING, |
| 1696 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1697 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1698 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1699 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1700 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1701 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1702 | NULL); |
| 1703 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1704 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1705 | /* Either the target doesn't exist, or we don't have access to |
| 1706 | get a handle to it. If the former, we need to return an error. |
| 1707 | If the latter, we can use attributes_from_dir. */ |
| 1708 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1709 | return -1; |
| 1710 | /* Could not get attributes on open file. Fall back to |
| 1711 | reading the directory. */ |
| 1712 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1713 | /* Very strange. This should not fail now */ |
| 1714 | return -1; |
| 1715 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1716 | if (traverse) { |
| 1717 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1718 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1719 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1720 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1721 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1722 | } else { |
| 1723 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1724 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1725 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1726 | } |
| 1727 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1728 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1729 | return -1; |
| 1730 | |
| 1731 | /* Close the outer open file handle now that we're about to |
| 1732 | reopen it with different flags. */ |
| 1733 | if (!CloseHandle(hFile)) |
| 1734 | return -1; |
| 1735 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1736 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1737 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1738 | the file without the reparse handling flag set. */ |
| 1739 | hFile2 = CreateFileW( |
| 1740 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1741 | NULL, OPEN_EXISTING, |
| 1742 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1743 | NULL); |
| 1744 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1745 | return -1; |
| 1746 | |
| 1747 | if (!get_target_path(hFile2, &target_path)) |
| 1748 | return -1; |
| 1749 | |
| 1750 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1751 | free(target_path); |
| 1752 | return code; |
| 1753 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1754 | } else |
| 1755 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1756 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1757 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1758 | |
| 1759 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1760 | dot = wcsrchr(path, '.'); |
| 1761 | if (dot) { |
| 1762 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1763 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1764 | result->st_mode |= 0111; |
| 1765 | } |
| 1766 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1770 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1771 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1772 | /* Protocol violation: we explicitly clear errno, instead of |
| 1773 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1774 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1775 | errno = 0; |
| 1776 | return code; |
| 1777 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1778 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1779 | static int |
| 1780 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1781 | { |
| 1782 | /* Protocol violation: we explicitly clear errno, instead of |
| 1783 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1784 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1785 | errno = 0; |
| 1786 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1787 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1788 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1789 | |
| 1790 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1791 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1792 | default does not traverse symlinks and instead returns attributes for |
| 1793 | the symlink. |
| 1794 | |
| 1795 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1796 | win32_stat will first explicitly resolve the symlink target and then will |
| 1797 | call win32_lstat on that result. |
| 1798 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1799 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1800 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1801 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1802 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1803 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1804 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1807 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1808 | 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] | 1809 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1810 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | static int |
| 1814 | win32_stat(const char* path, struct win32_stat *result) |
| 1815 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1816 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1819 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1820 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1821 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1822 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | static int |
| 1826 | win32_fstat(int file_number, struct win32_stat *result) |
| 1827 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1828 | BY_HANDLE_FILE_INFORMATION info; |
| 1829 | HANDLE h; |
| 1830 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1831 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1832 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1833 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1834 | /* Protocol violation: we explicitly clear errno, instead of |
| 1835 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1836 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1838 | if (h == INVALID_HANDLE_VALUE) { |
| 1839 | /* This is really a C library error (invalid file handle). |
| 1840 | We set the Win32 error to the closes one matching. */ |
| 1841 | SetLastError(ERROR_INVALID_HANDLE); |
| 1842 | return -1; |
| 1843 | } |
| 1844 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1845 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1846 | type = GetFileType(h); |
| 1847 | if (type == FILE_TYPE_UNKNOWN) { |
| 1848 | DWORD error = GetLastError(); |
| 1849 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1850 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1851 | } |
| 1852 | /* else: valid but unknown file */ |
| 1853 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1854 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1855 | if (type != FILE_TYPE_DISK) { |
| 1856 | if (type == FILE_TYPE_CHAR) |
| 1857 | result->st_mode = _S_IFCHR; |
| 1858 | else if (type == FILE_TYPE_PIPE) |
| 1859 | result->st_mode = _S_IFIFO; |
| 1860 | return 0; |
| 1861 | } |
| 1862 | |
| 1863 | if (!GetFileInformationByHandle(h, &info)) { |
| 1864 | return -1; |
| 1865 | } |
| 1866 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1867 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1868 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1869 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1870 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | #endif /* MS_WINDOWS */ |
| 1874 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1875 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1876 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1877 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1878 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1879 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1880 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1881 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1882 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1883 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1884 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1885 | |
| 1886 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1887 | {"st_mode", "protection bits"}, |
| 1888 | {"st_ino", "inode"}, |
| 1889 | {"st_dev", "device"}, |
| 1890 | {"st_nlink", "number of hard links"}, |
| 1891 | {"st_uid", "user ID of owner"}, |
| 1892 | {"st_gid", "group ID of owner"}, |
| 1893 | {"st_size", "total size, in bytes"}, |
| 1894 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1895 | {NULL, "integer time of last access"}, |
| 1896 | {NULL, "integer time of last modification"}, |
| 1897 | {NULL, "integer time of last change"}, |
| 1898 | {"st_atime", "time of last access"}, |
| 1899 | {"st_mtime", "time of last modification"}, |
| 1900 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1901 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1902 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1903 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1904 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1905 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1906 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1907 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1908 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1909 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1910 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1911 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1912 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1913 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1914 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1915 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1916 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1917 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1918 | #endif |
| 1919 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1920 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1921 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1922 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1923 | }; |
| 1924 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1925 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1926 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1927 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1928 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1929 | #endif |
| 1930 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1931 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1932 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1933 | #else |
| 1934 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1935 | #endif |
| 1936 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1937 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1938 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1939 | #else |
| 1940 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1941 | #endif |
| 1942 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1943 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1944 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1945 | #else |
| 1946 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1947 | #endif |
| 1948 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1949 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1950 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1951 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1952 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1953 | #endif |
| 1954 | |
| 1955 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1956 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1957 | #else |
| 1958 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1959 | #endif |
| 1960 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1961 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1962 | "stat_result", /* name */ |
| 1963 | stat_result__doc__, /* doc */ |
| 1964 | stat_result_fields, |
| 1965 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1966 | }; |
| 1967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1968 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1969 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1970 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1971 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1972 | 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] | 1973 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1974 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1975 | |
| 1976 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1977 | {"f_bsize", }, |
| 1978 | {"f_frsize", }, |
| 1979 | {"f_blocks", }, |
| 1980 | {"f_bfree", }, |
| 1981 | {"f_bavail", }, |
| 1982 | {"f_files", }, |
| 1983 | {"f_ffree", }, |
| 1984 | {"f_favail", }, |
| 1985 | {"f_flag", }, |
| 1986 | {"f_namemax",}, |
| 1987 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1988 | }; |
| 1989 | |
| 1990 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1991 | "statvfs_result", /* name */ |
| 1992 | statvfs_result__doc__, /* doc */ |
| 1993 | statvfs_result_fields, |
| 1994 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1995 | }; |
| 1996 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1997 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1998 | PyDoc_STRVAR(waitid_result__doc__, |
| 1999 | "waitid_result: Result from waitid.\n\n\ |
| 2000 | This object may be accessed either as a tuple of\n\ |
| 2001 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2002 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2003 | \n\ |
| 2004 | See os.waitid for more information."); |
| 2005 | |
| 2006 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2007 | {"si_pid", }, |
| 2008 | {"si_uid", }, |
| 2009 | {"si_signo", }, |
| 2010 | {"si_status", }, |
| 2011 | {"si_code", }, |
| 2012 | {0} |
| 2013 | }; |
| 2014 | |
| 2015 | static PyStructSequence_Desc waitid_result_desc = { |
| 2016 | "waitid_result", /* name */ |
| 2017 | waitid_result__doc__, /* doc */ |
| 2018 | waitid_result_fields, |
| 2019 | 5 |
| 2020 | }; |
| 2021 | static PyTypeObject WaitidResultType; |
| 2022 | #endif |
| 2023 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2024 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2025 | static PyTypeObject StatResultType; |
| 2026 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2027 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 2028 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2029 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2030 | static newfunc structseq_new; |
| 2031 | |
| 2032 | static PyObject * |
| 2033 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2034 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2035 | PyStructSequence *result; |
| 2036 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2037 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2038 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2039 | if (!result) |
| 2040 | return NULL; |
| 2041 | /* If we have been initialized from a tuple, |
| 2042 | st_?time might be set to None. Initialize it |
| 2043 | from the int slots. */ |
| 2044 | for (i = 7; i <= 9; i++) { |
| 2045 | if (result->ob_item[i+3] == Py_None) { |
| 2046 | Py_DECREF(Py_None); |
| 2047 | Py_INCREF(result->ob_item[i]); |
| 2048 | result->ob_item[i+3] = result->ob_item[i]; |
| 2049 | } |
| 2050 | } |
| 2051 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | |
| 2055 | |
| 2056 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 2057 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2058 | |
| 2059 | PyDoc_STRVAR(stat_float_times__doc__, |
| 2060 | "stat_float_times([newval]) -> oldval\n\n\ |
| 2061 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 2062 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 2063 | future calls return ints. \n\ |
| 2064 | If newval is omitted, return the current setting.\n"); |
| 2065 | |
| 2066 | static PyObject* |
| 2067 | stat_float_times(PyObject* self, PyObject *args) |
| 2068 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2069 | int newval = -1; |
| 2070 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 2071 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 2072 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2073 | "stat_float_times() is deprecated", |
| 2074 | 1)) |
| 2075 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2076 | if (newval == -1) |
| 2077 | /* Return old value */ |
| 2078 | return PyBool_FromLong(_stat_float_times); |
| 2079 | _stat_float_times = newval; |
| 2080 | Py_INCREF(Py_None); |
| 2081 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2082 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2083 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2084 | static PyObject *billion = NULL; |
| 2085 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2086 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2087 | 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] | 2088 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2089 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2090 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2091 | PyObject *s_in_ns = NULL; |
| 2092 | PyObject *ns_total = NULL; |
| 2093 | PyObject *float_s = NULL; |
| 2094 | |
| 2095 | if (!(s && ns_fractional)) |
| 2096 | goto exit; |
| 2097 | |
| 2098 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2099 | if (!s_in_ns) |
| 2100 | goto exit; |
| 2101 | |
| 2102 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2103 | if (!ns_total) |
| 2104 | goto exit; |
| 2105 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2106 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2107 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2108 | if (!float_s) |
| 2109 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2110 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2111 | else { |
| 2112 | float_s = s; |
| 2113 | Py_INCREF(float_s); |
| 2114 | } |
| 2115 | |
| 2116 | PyStructSequence_SET_ITEM(v, index, s); |
| 2117 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2118 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2119 | s = NULL; |
| 2120 | float_s = NULL; |
| 2121 | ns_total = NULL; |
| 2122 | exit: |
| 2123 | Py_XDECREF(s); |
| 2124 | Py_XDECREF(ns_fractional); |
| 2125 | Py_XDECREF(s_in_ns); |
| 2126 | Py_XDECREF(ns_total); |
| 2127 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2130 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2131 | (used by posix_stat() and posix_fstat()) */ |
| 2132 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2133 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2134 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2135 | unsigned long ansec, mnsec, cnsec; |
| 2136 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2137 | if (v == NULL) |
| 2138 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2139 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2140 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2141 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2142 | PyStructSequence_SET_ITEM(v, 1, |
| 2143 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2144 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2145 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2146 | #endif |
| 2147 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2148 | PyStructSequence_SET_ITEM(v, 2, |
| 2149 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2150 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2151 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2152 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2153 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 2154 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 2155 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
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, 6, |
| 2158 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
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, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2161 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2162 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2163 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2164 | ansec = st->st_atim.tv_nsec; |
| 2165 | mnsec = st->st_mtim.tv_nsec; |
| 2166 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2167 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2168 | ansec = st->st_atimespec.tv_nsec; |
| 2169 | mnsec = st->st_mtimespec.tv_nsec; |
| 2170 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2171 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2172 | ansec = st->st_atime_nsec; |
| 2173 | mnsec = st->st_mtime_nsec; |
| 2174 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2175 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2176 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2177 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2178 | fill_time(v, 7, st->st_atime, ansec); |
| 2179 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2180 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2181 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2182 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2183 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2184 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2185 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2186 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2187 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2188 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2189 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2190 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2191 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2192 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2193 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2194 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2195 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2196 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2197 | #endif |
| 2198 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2199 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2200 | PyObject *val; |
| 2201 | unsigned long bsec,bnsec; |
| 2202 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2203 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2204 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2205 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2206 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2207 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2208 | if (_stat_float_times) { |
| 2209 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2210 | } else { |
| 2211 | val = PyLong_FromLong((long)bsec); |
| 2212 | } |
| 2213 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2214 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2215 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2216 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2217 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2218 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2219 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2220 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2221 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2222 | if (PyErr_Occurred()) { |
| 2223 | Py_DECREF(v); |
| 2224 | return NULL; |
| 2225 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2226 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2227 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2230 | /* POSIX methods */ |
| 2231 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2232 | |
| 2233 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2234 | posix_do_stat(char *function_name, path_t *path, |
| 2235 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2236 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2237 | STRUCT_STAT st; |
| 2238 | int result; |
| 2239 | |
| 2240 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2241 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2242 | return NULL; |
| 2243 | #endif |
| 2244 | |
| 2245 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2246 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2247 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2248 | return NULL; |
| 2249 | |
| 2250 | Py_BEGIN_ALLOW_THREADS |
| 2251 | if (path->fd != -1) |
| 2252 | result = FSTAT(path->fd, &st); |
| 2253 | else |
| 2254 | #ifdef MS_WINDOWS |
| 2255 | if (path->wide) { |
| 2256 | if (follow_symlinks) |
| 2257 | result = win32_stat_w(path->wide, &st); |
| 2258 | else |
| 2259 | result = win32_lstat_w(path->wide, &st); |
| 2260 | } |
| 2261 | else |
| 2262 | #endif |
| 2263 | #if defined(HAVE_LSTAT) || defined(MS_WINDOWS) |
| 2264 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2265 | result = LSTAT(path->narrow, &st); |
| 2266 | else |
| 2267 | #endif |
| 2268 | #ifdef HAVE_FSTATAT |
| 2269 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2270 | result = fstatat(dir_fd, path->narrow, &st, |
| 2271 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2272 | else |
| 2273 | #endif |
| 2274 | result = STAT(path->narrow, &st); |
| 2275 | Py_END_ALLOW_THREADS |
| 2276 | |
| 2277 | if (result != 0) |
| 2278 | return path_error("stat", path); |
| 2279 | |
| 2280 | return _pystat_fromstructstat(&st); |
| 2281 | } |
| 2282 | |
| 2283 | PyDoc_STRVAR(posix_stat__doc__, |
| 2284 | "stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result\n\n\ |
| 2285 | Perform a stat system call on the given path.\n\ |
| 2286 | \n\ |
| 2287 | path may be specified as either a string or as an open file descriptor.\n\ |
| 2288 | \n\ |
| 2289 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2290 | and path should be relative; path will then be relative to that directory.\n\ |
| 2291 | dir_fd may not be supported on your platform; if it is unavailable, using\n\ |
| 2292 | it will raise a NotImplementedError.\n\ |
| 2293 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2294 | link, stat will examine the symbolic link itself instead of the file the\n\ |
| 2295 | link points to.\n\ |
| 2296 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2297 | an open file descriptor."); |
| 2298 | |
| 2299 | static PyObject * |
| 2300 | posix_stat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2301 | { |
| 2302 | static char *keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
| 2303 | path_t path; |
| 2304 | int dir_fd = DEFAULT_DIR_FD; |
| 2305 | int follow_symlinks = 1; |
| 2306 | PyObject *return_value; |
| 2307 | |
| 2308 | memset(&path, 0, sizeof(path)); |
| 2309 | path.allow_fd = 1; |
| 2310 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords, |
| 2311 | path_converter, &path, |
| 2312 | #ifdef HAVE_FSTATAT |
| 2313 | dir_fd_converter, &dir_fd, |
| 2314 | #else |
| 2315 | dir_fd_unavailable, &dir_fd, |
| 2316 | #endif |
| 2317 | &follow_symlinks)) |
| 2318 | return NULL; |
| 2319 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2320 | path_cleanup(&path); |
| 2321 | return return_value; |
| 2322 | } |
| 2323 | |
| 2324 | PyDoc_STRVAR(posix_lstat__doc__, |
| 2325 | "lstat(path, *, dir_fd=None) -> stat result\n\n\ |
| 2326 | Like stat(), but do not follow symbolic links.\n\ |
| 2327 | Equivalent to stat(path, follow_symlinks=False)."); |
| 2328 | |
| 2329 | static PyObject * |
| 2330 | posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2331 | { |
| 2332 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 2333 | path_t path; |
| 2334 | int dir_fd = DEFAULT_DIR_FD; |
| 2335 | int follow_symlinks = 0; |
| 2336 | PyObject *return_value; |
| 2337 | |
| 2338 | memset(&path, 0, sizeof(path)); |
| 2339 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords, |
| 2340 | path_converter, &path, |
| 2341 | #ifdef HAVE_FSTATAT |
| 2342 | dir_fd_converter, &dir_fd |
| 2343 | #else |
| 2344 | dir_fd_unavailable, &dir_fd |
| 2345 | #endif |
| 2346 | )) |
| 2347 | return NULL; |
| 2348 | return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks); |
| 2349 | path_cleanup(&path); |
| 2350 | return return_value; |
| 2351 | } |
| 2352 | |
| 2353 | PyDoc_STRVAR(posix_access__doc__, |
| 2354 | "access(path, mode, *, dir_fd=None, effective_ids=False,\ |
| 2355 | follow_symlinks=True)\n\n\ |
| 2356 | Use the real uid/gid to test for access to a path. Returns True if granted,\n\ |
| 2357 | False otherwise.\n\ |
| 2358 | \n\ |
| 2359 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2360 | and path should be relative; path will then be relative to that directory.\n\ |
| 2361 | If effective_ids is True, access will use the effective uid/gid instead of\n\ |
| 2362 | the real uid/gid.\n\ |
| 2363 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2364 | link, access will examine the symbolic link itself instead of the file the\n\ |
| 2365 | link points to.\n\ |
| 2366 | dir_fd, effective_ids, and follow_symlinks may not be implemented\n\ |
| 2367 | on your platform. If they are unavailable, using them will raise a\n\ |
| 2368 | NotImplementedError.\n\ |
| 2369 | \n\ |
| 2370 | Note that most operations will use the effective uid/gid, therefore this\n\ |
| 2371 | routine can be used in a suid/sgid environment to test if the invoking user\n\ |
| 2372 | has the specified access to the path.\n\ |
| 2373 | The mode argument can be F_OK to test existence, or the inclusive-OR\n\ |
| 2374 | of R_OK, W_OK, and X_OK."); |
| 2375 | |
| 2376 | static PyObject * |
| 2377 | posix_access(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2378 | { |
| 2379 | static char *keywords[] = {"path", "mode", "dir_fd", "effective_ids", |
| 2380 | "follow_symlinks", NULL}; |
| 2381 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2382 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2383 | int dir_fd = DEFAULT_DIR_FD; |
| 2384 | int effective_ids = 0; |
| 2385 | int follow_symlinks = 1; |
| 2386 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2387 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2388 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2389 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2390 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2391 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2392 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2393 | |
| 2394 | memset(&path, 0, sizeof(path)); |
| 2395 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", keywords, |
| 2396 | path_converter, &path, &mode, |
| 2397 | #ifdef HAVE_FACCESSAT |
| 2398 | dir_fd_converter, &dir_fd, |
| 2399 | #else |
| 2400 | dir_fd_unavailable, &dir_fd, |
| 2401 | #endif |
| 2402 | &effective_ids, &follow_symlinks)) |
| 2403 | return NULL; |
| 2404 | |
| 2405 | #ifndef HAVE_FACCESSAT |
| 2406 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2407 | goto exit; |
| 2408 | |
| 2409 | if (effective_ids) { |
| 2410 | argument_unavailable_error("access", "effective_ids"); |
| 2411 | goto exit; |
| 2412 | } |
| 2413 | #endif |
| 2414 | |
| 2415 | #ifdef MS_WINDOWS |
| 2416 | Py_BEGIN_ALLOW_THREADS |
| 2417 | if (path.wide != NULL) |
| 2418 | attr = GetFileAttributesW(path.wide); |
| 2419 | else |
| 2420 | attr = GetFileAttributesA(path.narrow); |
| 2421 | Py_END_ALLOW_THREADS |
| 2422 | |
| 2423 | /* |
| 2424 | * Access is possible if |
| 2425 | * * we didn't get a -1, and |
| 2426 | * * write access wasn't requested, |
| 2427 | * * or the file isn't read-only, |
| 2428 | * * or it's a directory. |
| 2429 | * (Directories cannot be read-only on Windows.) |
| 2430 | */ |
| 2431 | return_value = PyBool_FromLong( |
| 2432 | (attr != 0xFFFFFFFF) && |
| 2433 | ((mode & 2) || |
| 2434 | !(attr & FILE_ATTRIBUTE_READONLY) || |
| 2435 | (attr & FILE_ATTRIBUTE_DIRECTORY))); |
| 2436 | #else |
| 2437 | |
| 2438 | Py_BEGIN_ALLOW_THREADS |
| 2439 | #ifdef HAVE_FACCESSAT |
| 2440 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2441 | effective_ids || |
| 2442 | !follow_symlinks) { |
| 2443 | int flags = 0; |
| 2444 | if (!follow_symlinks) |
| 2445 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2446 | if (effective_ids) |
| 2447 | flags |= AT_EACCESS; |
| 2448 | result = faccessat(dir_fd, path.narrow, mode, flags); |
| 2449 | } |
| 2450 | else |
| 2451 | #endif |
| 2452 | result = access(path.narrow, mode); |
| 2453 | Py_END_ALLOW_THREADS |
| 2454 | return_value = PyBool_FromLong(!result); |
| 2455 | #endif |
| 2456 | |
| 2457 | #ifndef HAVE_FACCESSAT |
| 2458 | exit: |
| 2459 | #endif |
| 2460 | path_cleanup(&path); |
| 2461 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2464 | #ifndef F_OK |
| 2465 | #define F_OK 0 |
| 2466 | #endif |
| 2467 | #ifndef R_OK |
| 2468 | #define R_OK 4 |
| 2469 | #endif |
| 2470 | #ifndef W_OK |
| 2471 | #define W_OK 2 |
| 2472 | #endif |
| 2473 | #ifndef X_OK |
| 2474 | #define X_OK 1 |
| 2475 | #endif |
| 2476 | |
| 2477 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2478 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2479 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2480 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2481 | |
| 2482 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2483 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2484 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2485 | int id; |
| 2486 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2487 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2488 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 2489 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2490 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2491 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2492 | /* file descriptor 0 only, the default input device (stdin) */ |
| 2493 | if (id == 0) { |
| 2494 | ret = ttyname(); |
| 2495 | } |
| 2496 | else { |
| 2497 | ret = NULL; |
| 2498 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2499 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2500 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2501 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2502 | if (ret == NULL) |
| 2503 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2504 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2505 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2506 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2507 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2508 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2509 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2510 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2511 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2512 | |
| 2513 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2514 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2515 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2516 | char *ret; |
| 2517 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2518 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2519 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2520 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2521 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2522 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2523 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2524 | if (ret == NULL) |
| 2525 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2526 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2527 | } |
| 2528 | #endif |
| 2529 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2530 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2531 | "chdir(path)\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2532 | Change the current working directory to the specified path.\n\ |
| 2533 | \n\ |
| 2534 | path may always be specified as a string.\n\ |
| 2535 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2536 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2537 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2538 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2539 | posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2540 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2541 | path_t path; |
| 2542 | int result; |
| 2543 | PyObject *return_value = NULL; |
| 2544 | static char *keywords[] = {"path", NULL}; |
| 2545 | |
| 2546 | memset(&path, 0, sizeof(path)); |
| 2547 | #ifdef HAVE_FCHDIR |
| 2548 | path.allow_fd = 1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2549 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2550 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords, |
| 2551 | path_converter, &path |
| 2552 | )) |
| 2553 | return NULL; |
| 2554 | |
| 2555 | Py_BEGIN_ALLOW_THREADS |
| 2556 | #ifdef MS_WINDOWS |
| 2557 | if (path.wide) |
| 2558 | result = win32_wchdir(path.wide); |
| 2559 | else |
| 2560 | result = win32_chdir(path.narrow); |
| 2561 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
| 2562 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2563 | result = _chdir2(path.narrow); |
| 2564 | #else |
| 2565 | #ifdef HAVE_FCHDIR |
| 2566 | if (path.fd != -1) |
| 2567 | result = fchdir(path.fd); |
| 2568 | else |
| 2569 | #endif |
| 2570 | result = chdir(path.narrow); |
| 2571 | #endif |
| 2572 | Py_END_ALLOW_THREADS |
| 2573 | |
| 2574 | if (result) { |
| 2575 | return_value = path_error("chdir", &path); |
| 2576 | goto exit; |
| 2577 | } |
| 2578 | |
| 2579 | return_value = Py_None; |
| 2580 | Py_INCREF(Py_None); |
| 2581 | |
| 2582 | exit: |
| 2583 | path_cleanup(&path); |
| 2584 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2587 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2588 | PyDoc_STRVAR(posix_fchdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2589 | "fchdir(fd)\n\n\ |
| 2590 | Change to the directory of the given file descriptor. fd must be\n\ |
| 2591 | opened on a directory, not a file. Equivalent to os.chdir(fd)."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2592 | |
| 2593 | static PyObject * |
| 2594 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2595 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2596 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2597 | } |
| 2598 | #endif /* HAVE_FCHDIR */ |
| 2599 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2600 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2601 | PyDoc_STRVAR(posix_chmod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2602 | "chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2603 | Change the access permissions of a file.\n\ |
| 2604 | \n\ |
| 2605 | path may always be specified as a string.\n\ |
| 2606 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2607 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2608 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2609 | and path should be relative; path will then be relative to that directory.\n\ |
| 2610 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2611 | link, chmod will modify the symbolic link itself instead of the file the\n\ |
| 2612 | link points to.\n\ |
| 2613 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2614 | an open file descriptor.\n\ |
| 2615 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2616 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2617 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2618 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2619 | posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2620 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2621 | path_t path; |
| 2622 | int mode; |
| 2623 | int dir_fd = DEFAULT_DIR_FD; |
| 2624 | int follow_symlinks = 1; |
| 2625 | int result; |
| 2626 | PyObject *return_value = NULL; |
| 2627 | static char *keywords[] = {"path", "mode", "dir_fd", |
| 2628 | "follow_symlinks", NULL}; |
| 2629 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2630 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2631 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2632 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2633 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2634 | #ifdef HAVE_FCHMODAT |
| 2635 | int fchmodat_nofollow_unsupported = 0; |
| 2636 | #endif |
| 2637 | |
| 2638 | memset(&path, 0, sizeof(path)); |
| 2639 | #ifdef HAVE_FCHMOD |
| 2640 | path.allow_fd = 1; |
| 2641 | #endif |
| 2642 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords, |
| 2643 | path_converter, &path, |
| 2644 | &mode, |
| 2645 | #ifdef HAVE_FCHMODAT |
| 2646 | dir_fd_converter, &dir_fd, |
| 2647 | #else |
| 2648 | dir_fd_unavailable, &dir_fd, |
| 2649 | #endif |
| 2650 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2651 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2652 | |
| 2653 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 2654 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
| 2655 | goto exit; |
| 2656 | #endif |
| 2657 | |
| 2658 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2659 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2660 | if (path.wide) |
| 2661 | attr = GetFileAttributesW(path.wide); |
| 2662 | else |
| 2663 | attr = GetFileAttributesA(path.narrow); |
| 2664 | if (attr == 0xFFFFFFFF) |
| 2665 | result = 0; |
| 2666 | else { |
| 2667 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2668 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2669 | else |
| 2670 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2671 | if (path.wide) |
| 2672 | result = SetFileAttributesW(path.wide, attr); |
| 2673 | else |
| 2674 | result = SetFileAttributesA(path.narrow, attr); |
| 2675 | } |
| 2676 | Py_END_ALLOW_THREADS |
| 2677 | |
| 2678 | if (!result) { |
| 2679 | return_value = win32_error_object("chmod", path.object); |
| 2680 | goto exit; |
| 2681 | } |
| 2682 | #else /* MS_WINDOWS */ |
| 2683 | Py_BEGIN_ALLOW_THREADS |
| 2684 | #ifdef HAVE_FCHMOD |
| 2685 | if (path.fd != -1) |
| 2686 | result = fchmod(path.fd, mode); |
| 2687 | else |
| 2688 | #endif |
| 2689 | #ifdef HAVE_LCHMOD |
| 2690 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2691 | result = lchmod(path.narrow, mode); |
| 2692 | else |
| 2693 | #endif |
| 2694 | #ifdef HAVE_FCHMODAT |
| 2695 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2696 | /* |
| 2697 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 2698 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2699 | * and then says it isn't implemented yet. |
| 2700 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2701 | * |
| 2702 | * Once it is supported, os.chmod will automatically |
| 2703 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 2704 | * Until then, we need to be careful what exception we raise. |
| 2705 | */ |
| 2706 | result = fchmodat(dir_fd, path.narrow, mode, |
| 2707 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2708 | /* |
| 2709 | * But wait! We can't throw the exception without allowing threads, |
| 2710 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 2711 | */ |
| 2712 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2713 | result && |
| 2714 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 2715 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2716 | } |
| 2717 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2718 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2719 | result = chmod(path.narrow, mode); |
| 2720 | Py_END_ALLOW_THREADS |
| 2721 | |
| 2722 | if (result) { |
| 2723 | #ifdef HAVE_FCHMODAT |
| 2724 | if (fchmodat_nofollow_unsupported) { |
| 2725 | if (dir_fd != DEFAULT_DIR_FD) |
| 2726 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 2727 | dir_fd, follow_symlinks); |
| 2728 | else |
| 2729 | follow_symlinks_specified("chmod", follow_symlinks); |
| 2730 | } |
| 2731 | else |
| 2732 | #endif |
| 2733 | return_value = path_error("chmod", &path); |
| 2734 | goto exit; |
| 2735 | } |
| 2736 | #endif |
| 2737 | |
| 2738 | Py_INCREF(Py_None); |
| 2739 | return_value = Py_None; |
| 2740 | exit: |
| 2741 | path_cleanup(&path); |
| 2742 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2745 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2746 | #ifdef HAVE_FCHMOD |
| 2747 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2748 | "fchmod(fd, mode)\n\n\ |
| 2749 | Change the access permissions of the file given by file\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2750 | descriptor fd. Equivalent to os.chmod(fd, mode)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2751 | |
| 2752 | static PyObject * |
| 2753 | posix_fchmod(PyObject *self, PyObject *args) |
| 2754 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2755 | int fd, mode, res; |
| 2756 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2757 | return NULL; |
| 2758 | Py_BEGIN_ALLOW_THREADS |
| 2759 | res = fchmod(fd, mode); |
| 2760 | Py_END_ALLOW_THREADS |
| 2761 | if (res < 0) |
| 2762 | return posix_error(); |
| 2763 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2764 | } |
| 2765 | #endif /* HAVE_FCHMOD */ |
| 2766 | |
| 2767 | #ifdef HAVE_LCHMOD |
| 2768 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2769 | "lchmod(path, mode)\n\n\ |
| 2770 | 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] | 2771 | affects the link itself rather than the target.\n\ |
| 2772 | Equivalent to chmod(path, mode, follow_symlinks=False)."); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2773 | |
| 2774 | static PyObject * |
| 2775 | posix_lchmod(PyObject *self, PyObject *args) |
| 2776 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2777 | PyObject *opath; |
| 2778 | char *path; |
| 2779 | int i; |
| 2780 | int res; |
| 2781 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2782 | &opath, &i)) |
| 2783 | return NULL; |
| 2784 | path = PyBytes_AsString(opath); |
| 2785 | Py_BEGIN_ALLOW_THREADS |
| 2786 | res = lchmod(path, i); |
| 2787 | Py_END_ALLOW_THREADS |
| 2788 | if (res < 0) |
| 2789 | return posix_error_with_allocated_filename(opath); |
| 2790 | Py_DECREF(opath); |
| 2791 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2792 | } |
| 2793 | #endif /* HAVE_LCHMOD */ |
| 2794 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2795 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2796 | #ifdef HAVE_CHFLAGS |
| 2797 | PyDoc_STRVAR(posix_chflags__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2798 | "chflags(path, flags, *, follow_symlinks=True)\n\n\ |
| 2799 | Set file flags.\n\ |
| 2800 | \n\ |
| 2801 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2802 | link, chflags will change flags on the symbolic link itself instead of the\n\ |
| 2803 | file the link points to.\n\ |
| 2804 | follow_symlinks may not be implemented on your platform. If it is\n\ |
| 2805 | unavailable, using it will raise a NotImplementedError."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2806 | |
| 2807 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2808 | posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2809 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2810 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2811 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2812 | int follow_symlinks = 1; |
| 2813 | int result; |
| 2814 | PyObject *return_value; |
| 2815 | static char *keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 2816 | |
| 2817 | memset(&path, 0, sizeof(path)); |
| 2818 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords, |
| 2819 | path_converter, &path, |
| 2820 | &flags, &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2821 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2822 | |
| 2823 | #ifndef HAVE_LCHFLAGS |
| 2824 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
| 2825 | goto exit; |
| 2826 | #endif |
| 2827 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2828 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2829 | #ifdef HAVE_LCHFLAGS |
| 2830 | if (!follow_symlinks) |
| 2831 | result = lchflags(path.narrow, flags); |
| 2832 | else |
| 2833 | #endif |
| 2834 | result = chflags(path.narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2835 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2836 | |
| 2837 | if (result) { |
| 2838 | return_value = path_posix_error("chflags", &path); |
| 2839 | goto exit; |
| 2840 | } |
| 2841 | |
| 2842 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2843 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2844 | |
| 2845 | exit: |
| 2846 | path_cleanup(&path); |
| 2847 | return return_value; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2848 | } |
| 2849 | #endif /* HAVE_CHFLAGS */ |
| 2850 | |
| 2851 | #ifdef HAVE_LCHFLAGS |
| 2852 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2853 | "lchflags(path, flags)\n\n\ |
| 2854 | Set file flags.\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2855 | This function will not follow symbolic links.\n\ |
| 2856 | Equivalent to chflags(path, flags, follow_symlinks=False)."); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2857 | |
| 2858 | static PyObject * |
| 2859 | posix_lchflags(PyObject *self, PyObject *args) |
| 2860 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2861 | PyObject *opath; |
| 2862 | char *path; |
| 2863 | unsigned long flags; |
| 2864 | int res; |
| 2865 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2866 | PyUnicode_FSConverter, &opath, &flags)) |
| 2867 | return NULL; |
| 2868 | path = PyBytes_AsString(opath); |
| 2869 | Py_BEGIN_ALLOW_THREADS |
| 2870 | res = lchflags(path, flags); |
| 2871 | Py_END_ALLOW_THREADS |
| 2872 | if (res < 0) |
| 2873 | return posix_error_with_allocated_filename(opath); |
| 2874 | Py_DECREF(opath); |
| 2875 | Py_INCREF(Py_None); |
| 2876 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2877 | } |
| 2878 | #endif /* HAVE_LCHFLAGS */ |
| 2879 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2880 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2881 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2882 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2883 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2884 | |
| 2885 | static PyObject * |
| 2886 | posix_chroot(PyObject *self, PyObject *args) |
| 2887 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2888 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2889 | } |
| 2890 | #endif |
| 2891 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2892 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2893 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2894 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2895 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2896 | |
| 2897 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2898 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2899 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2900 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2901 | } |
| 2902 | #endif /* HAVE_FSYNC */ |
| 2903 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2904 | #ifdef HAVE_SYNC |
| 2905 | PyDoc_STRVAR(posix_sync__doc__, |
| 2906 | "sync()\n\n\ |
| 2907 | Force write of everything to disk."); |
| 2908 | |
| 2909 | static PyObject * |
| 2910 | posix_sync(PyObject *self, PyObject *noargs) |
| 2911 | { |
| 2912 | Py_BEGIN_ALLOW_THREADS |
| 2913 | sync(); |
| 2914 | Py_END_ALLOW_THREADS |
| 2915 | Py_RETURN_NONE; |
| 2916 | } |
| 2917 | #endif |
| 2918 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2919 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2920 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2921 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2922 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2923 | #endif |
| 2924 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2925 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2926 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2927 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2928 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2929 | |
| 2930 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2931 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2932 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2933 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2934 | } |
| 2935 | #endif /* HAVE_FDATASYNC */ |
| 2936 | |
| 2937 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2938 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2939 | PyDoc_STRVAR(posix_chown__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2940 | "chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\ |
| 2941 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2942 | \n\ |
| 2943 | path may always be specified as a string.\n\ |
| 2944 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 2945 | If this functionality is unavailable, using it raises an exception.\n\ |
| 2946 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 2947 | and path should be relative; path will then be relative to that directory.\n\ |
| 2948 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 2949 | link, chown will modify the symbolic link itself instead of the file the\n\ |
| 2950 | link points to.\n\ |
| 2951 | It is an error to use dir_fd or follow_symlinks when specifying path as\n\ |
| 2952 | an open file descriptor.\n\ |
| 2953 | dir_fd and follow_symlinks may not be implemented on your platform.\n\ |
| 2954 | If they are unavailable, using them will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2955 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2956 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2957 | posix_chown(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2958 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2959 | path_t path; |
| 2960 | long uid_l, gid_l; |
| 2961 | uid_t uid; |
| 2962 | gid_t gid; |
| 2963 | int dir_fd = DEFAULT_DIR_FD; |
| 2964 | int follow_symlinks = 1; |
| 2965 | int result; |
| 2966 | PyObject *return_value = NULL; |
| 2967 | static char *keywords[] = {"path", "uid", "gid", "dir_fd", |
| 2968 | "follow_symlinks", NULL}; |
| 2969 | |
| 2970 | memset(&path, 0, sizeof(path)); |
| 2971 | #ifdef HAVE_FCHOWN |
| 2972 | path.allow_fd = 1; |
| 2973 | #endif |
| 2974 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&ll|$O&p:chown", keywords, |
| 2975 | path_converter, &path, |
| 2976 | &uid_l, &gid_l, |
| 2977 | #ifdef HAVE_FCHOWNAT |
| 2978 | dir_fd_converter, &dir_fd, |
| 2979 | #else |
| 2980 | dir_fd_unavailable, &dir_fd, |
| 2981 | #endif |
| 2982 | &follow_symlinks)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2983 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2984 | |
| 2985 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 2986 | if (follow_symlinks_specified("chown", follow_symlinks)) |
| 2987 | goto exit; |
| 2988 | #endif |
| 2989 | if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) || |
| 2990 | fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks)) |
| 2991 | goto exit; |
| 2992 | |
| 2993 | #ifdef __APPLE__ |
| 2994 | /* |
| 2995 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 2996 | * (But we still have an lchown symbol because of weak-linking.) |
| 2997 | * It doesn't have fchownat either. So there's no possibility |
| 2998 | * of a graceful failover. |
| 2999 | */ |
| 3000 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3001 | follow_symlinks_specified("chown", follow_symlinks); |
| 3002 | goto exit; |
| 3003 | } |
| 3004 | #endif |
| 3005 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3006 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3007 | uid = (uid_t)uid_l; |
| 3008 | gid = (uid_t)gid_l; |
| 3009 | #ifdef HAVE_FCHOWN |
| 3010 | if (path.fd != -1) |
| 3011 | result = fchown(path.fd, uid, gid); |
| 3012 | else |
| 3013 | #endif |
| 3014 | #ifdef HAVE_LCHOWN |
| 3015 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 3016 | result = lchown(path.narrow, uid, gid); |
| 3017 | else |
| 3018 | #endif |
| 3019 | #ifdef HAVE_FCHOWNAT |
| 3020 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 3021 | result = fchownat(dir_fd, path.narrow, uid, gid, |
| 3022 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3023 | else |
| 3024 | #endif |
| 3025 | result = chown(path.narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3026 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3027 | |
| 3028 | if (result) { |
| 3029 | return_value = path_posix_error("chown", &path); |
| 3030 | goto exit; |
| 3031 | } |
| 3032 | |
| 3033 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3034 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3035 | |
| 3036 | exit: |
| 3037 | path_cleanup(&path); |
| 3038 | return return_value; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3039 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3040 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3041 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3042 | #ifdef HAVE_FCHOWN |
| 3043 | PyDoc_STRVAR(posix_fchown__doc__, |
| 3044 | "fchown(fd, uid, gid)\n\n\ |
| 3045 | 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] | 3046 | 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] | 3047 | |
| 3048 | static PyObject * |
| 3049 | posix_fchown(PyObject *self, PyObject *args) |
| 3050 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3051 | int fd; |
| 3052 | long uid, gid; |
| 3053 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 3054 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3055 | return NULL; |
| 3056 | Py_BEGIN_ALLOW_THREADS |
| 3057 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 3058 | Py_END_ALLOW_THREADS |
| 3059 | if (res < 0) |
| 3060 | return posix_error(); |
| 3061 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3062 | } |
| 3063 | #endif /* HAVE_FCHOWN */ |
| 3064 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3065 | #ifdef HAVE_LCHOWN |
| 3066 | PyDoc_STRVAR(posix_lchown__doc__, |
| 3067 | "lchown(path, uid, gid)\n\n\ |
| 3068 | 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] | 3069 | This function will not follow symbolic links.\n\ |
| 3070 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3071 | |
| 3072 | static PyObject * |
| 3073 | posix_lchown(PyObject *self, PyObject *args) |
| 3074 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3075 | PyObject *opath; |
| 3076 | char *path; |
| 3077 | long uid, gid; |
| 3078 | int res; |
| 3079 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 3080 | PyUnicode_FSConverter, &opath, |
| 3081 | &uid, &gid)) |
| 3082 | return NULL; |
| 3083 | path = PyBytes_AsString(opath); |
| 3084 | Py_BEGIN_ALLOW_THREADS |
| 3085 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 3086 | Py_END_ALLOW_THREADS |
| 3087 | if (res < 0) |
| 3088 | return posix_error_with_allocated_filename(opath); |
| 3089 | Py_DECREF(opath); |
| 3090 | Py_INCREF(Py_None); |
| 3091 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3092 | } |
| 3093 | #endif /* HAVE_LCHOWN */ |
| 3094 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3095 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3096 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3097 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3098 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3099 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3100 | char buf[1026]; |
| 3101 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3102 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3103 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3104 | if (!use_bytes) { |
| 3105 | wchar_t wbuf[1026]; |
| 3106 | wchar_t *wbuf2 = wbuf; |
| 3107 | PyObject *resobj; |
| 3108 | DWORD len; |
| 3109 | Py_BEGIN_ALLOW_THREADS |
| 3110 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 3111 | /* If the buffer is large enough, len does not include the |
| 3112 | terminating \0. If the buffer is too small, len includes |
| 3113 | the space needed for the terminator. */ |
| 3114 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 3115 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 3116 | if (wbuf2) |
| 3117 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3118 | } |
| 3119 | Py_END_ALLOW_THREADS |
| 3120 | if (!wbuf2) { |
| 3121 | PyErr_NoMemory(); |
| 3122 | return NULL; |
| 3123 | } |
| 3124 | if (!len) { |
| 3125 | if (wbuf2 != wbuf) free(wbuf2); |
| 3126 | return win32_error("getcwdu", NULL); |
| 3127 | } |
| 3128 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3129 | if (wbuf2 != wbuf) free(wbuf2); |
| 3130 | return resobj; |
| 3131 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3132 | |
| 3133 | if (win32_warn_bytes_api()) |
| 3134 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3135 | #endif |
| 3136 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3137 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3138 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3139 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3140 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3141 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3142 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3143 | Py_END_ALLOW_THREADS |
| 3144 | if (res == NULL) |
| 3145 | return posix_error(); |
| 3146 | if (use_bytes) |
| 3147 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 3148 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3149 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3150 | |
| 3151 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 3152 | "getcwd() -> path\n\n\ |
| 3153 | Return a unicode string representing the current working directory."); |
| 3154 | |
| 3155 | static PyObject * |
| 3156 | posix_getcwd_unicode(PyObject *self) |
| 3157 | { |
| 3158 | return posix_getcwd(0); |
| 3159 | } |
| 3160 | |
| 3161 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 3162 | "getcwdb() -> path\n\n\ |
| 3163 | Return a bytes string representing the current working directory."); |
| 3164 | |
| 3165 | static PyObject * |
| 3166 | posix_getcwd_bytes(PyObject *self) |
| 3167 | { |
| 3168 | return posix_getcwd(1); |
| 3169 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 3170 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3171 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3172 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3173 | #define HAVE_LINK 1 |
| 3174 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3175 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3176 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3177 | PyDoc_STRVAR(posix_link__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3178 | "link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\ |
| 3179 | Create a hard link to a file.\n\ |
| 3180 | \n\ |
| 3181 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 3182 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 3183 | should be relative; the path will then be relative to that directory.\n\ |
| 3184 | If follow_symlinks is False, and the last element of src is a symbolic\n\ |
| 3185 | link, link will create a link to the symbolic link itself instead of the\n\ |
| 3186 | file the link points to.\n\ |
| 3187 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\ |
| 3188 | platform. If they are unavailable, using them will raise a\n\ |
| 3189 | NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3190 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3191 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3192 | posix_link(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3193 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3194 | path_t src, dst; |
| 3195 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3196 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3197 | int follow_symlinks = 1; |
| 3198 | PyObject *return_value = NULL; |
| 3199 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", |
| 3200 | "follow_symlinks", NULL}; |
| 3201 | #ifdef MS_WINDOWS |
| 3202 | BOOL result; |
| 3203 | #else |
| 3204 | int result; |
| 3205 | #endif |
| 3206 | |
| 3207 | memset(&src, 0, sizeof(src)); |
| 3208 | memset(&dst, 0, sizeof(dst)); |
| 3209 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords, |
| 3210 | path_converter, &src, |
| 3211 | path_converter, &dst, |
| 3212 | dir_fd_converter, &src_dir_fd, |
| 3213 | dir_fd_converter, &dst_dir_fd, |
| 3214 | &follow_symlinks)) |
| 3215 | return NULL; |
| 3216 | |
| 3217 | #ifndef HAVE_LINKAT |
| 3218 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3219 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
| 3220 | goto exit; |
| 3221 | } |
| 3222 | #endif |
| 3223 | |
| 3224 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 3225 | PyErr_SetString(PyExc_NotImplementedError, |
| 3226 | "link: src and dst must be the same type"); |
| 3227 | goto exit; |
| 3228 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3229 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3230 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3231 | Py_BEGIN_ALLOW_THREADS |
| 3232 | if (src.wide) |
| 3233 | result = CreateHardLinkW(dst.wide, src.wide, NULL); |
| 3234 | else |
| 3235 | result = CreateHardLinkA(dst.narrow, src.narrow, NULL); |
| 3236 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3237 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3238 | if (!result) { |
| 3239 | return_value = win32_error_object("link", dst.object); |
| 3240 | goto exit; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3241 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3242 | #else |
| 3243 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3244 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3245 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3246 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3247 | (!follow_symlinks)) |
| 3248 | result = linkat(src_dir_fd, src.narrow, |
| 3249 | dst_dir_fd, dst.narrow, |
| 3250 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3251 | else |
| 3252 | #endif |
| 3253 | result = link(src.narrow, dst.narrow); |
| 3254 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3255 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3256 | if (result) { |
| 3257 | return_value = path_error("link", &dst); |
| 3258 | goto exit; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3259 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3260 | #endif |
| 3261 | |
| 3262 | return_value = Py_None; |
| 3263 | Py_INCREF(Py_None); |
| 3264 | |
| 3265 | exit: |
| 3266 | path_cleanup(&src); |
| 3267 | path_cleanup(&dst); |
| 3268 | return return_value; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3269 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3270 | #endif |
| 3271 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3272 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3273 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3274 | PyDoc_STRVAR(posix_listdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3275 | "listdir(path='.') -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3276 | Return a list containing the names of the entries in the directory.\n\ |
| 3277 | \n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3278 | 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] | 3279 | entries '.' and '..' even if they are present in the directory.\n\ |
| 3280 | \n\ |
| 3281 | path can always be specified as a string.\n\ |
| 3282 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 3283 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3284 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3285 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3286 | posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3287 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3288 | path_t path; |
| 3289 | PyObject *list = NULL; |
| 3290 | static char *keywords[] = {"path", NULL}; |
| 3291 | int fd = -1; |
| 3292 | |
| 3293 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3294 | PyObject *v; |
| 3295 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3296 | BOOL result; |
| 3297 | WIN32_FIND_DATA FileData; |
| 3298 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 3299 | char *bufptr = namebuf; |
| 3300 | /* only claim to have space for MAX_PATH */ |
| 3301 | Py_ssize_t len = sizeof(namebuf)-5; |
| 3302 | PyObject *po = NULL; |
| 3303 | wchar_t *wnamebuf = NULL; |
| 3304 | #elif defined(PYOS_OS2) |
| 3305 | #ifndef MAX_PATH |
| 3306 | #define MAX_PATH CCHMAXPATH |
| 3307 | #endif |
| 3308 | char *pt; |
| 3309 | PyObject *v; |
| 3310 | char namebuf[MAX_PATH+5]; |
| 3311 | HDIR hdir = 1; |
| 3312 | ULONG srchcnt = 1; |
| 3313 | FILEFINDBUF3 ep; |
| 3314 | APIRET rc; |
| 3315 | #else |
| 3316 | PyObject *v; |
| 3317 | DIR *dirp = NULL; |
| 3318 | struct dirent *ep; |
| 3319 | int arg_is_unicode = 1; |
| 3320 | #endif |
| 3321 | |
| 3322 | memset(&path, 0, sizeof(path)); |
| 3323 | path.nullable = 1; |
| 3324 | #ifdef HAVE_FDOPENDIR |
| 3325 | path.allow_fd = 1; |
| 3326 | path.fd = -1; |
| 3327 | #endif |
| 3328 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords, |
| 3329 | path_converter, &path |
| 3330 | )) |
| 3331 | return NULL; |
| 3332 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3333 | /* XXX Should redo this putting the (now four) versions of opendir |
| 3334 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3335 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3336 | if (!path.narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3337 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3338 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3339 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3340 | if (!path.wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3341 | po_wchars = L"."; |
| 3342 | len = 1; |
| 3343 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3344 | po_wchars = path.wide; |
| 3345 | len = wcslen(path.wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3346 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3347 | /* The +5 is so we can append "\\*.*\0" */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3348 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 3349 | if (!wnamebuf) { |
| 3350 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3351 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3352 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3353 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3354 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3355 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3356 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 3357 | wnamebuf[len++] = L'\\'; |
| 3358 | wcscpy(wnamebuf + len, L"*.*"); |
| 3359 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3360 | if ((list = PyList_New(0)) == NULL) { |
| 3361 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3362 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3363 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3364 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3365 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3366 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3367 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3368 | if (error == ERROR_FILE_NOT_FOUND) |
| 3369 | goto exit; |
| 3370 | Py_DECREF(list); |
| 3371 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3372 | win32_error_unicode("FindFirstFileW", wnamebuf); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3373 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3374 | } |
| 3375 | do { |
| 3376 | /* Skip over . and .. */ |
| 3377 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3378 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3379 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3380 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3381 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3382 | Py_DECREF(list); |
| 3383 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3384 | break; |
| 3385 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3386 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3387 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3388 | Py_DECREF(list); |
| 3389 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3390 | break; |
| 3391 | } |
| 3392 | Py_DECREF(v); |
| 3393 | } |
| 3394 | Py_BEGIN_ALLOW_THREADS |
| 3395 | result = FindNextFileW(hFindFile, &wFileData); |
| 3396 | Py_END_ALLOW_THREADS |
| 3397 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3398 | it got to the end of the directory. */ |
| 3399 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3400 | Py_DECREF(list); |
| 3401 | list = win32_error_unicode("FindNextFileW", wnamebuf); |
| 3402 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3403 | } |
| 3404 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3405 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3406 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3407 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3408 | strcpy(namebuf, path.narrow); |
| 3409 | len = path.length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3410 | if (len > 0) { |
| 3411 | char ch = namebuf[len-1]; |
| 3412 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 3413 | namebuf[len++] = '/'; |
| 3414 | strcpy(namebuf + len, "*.*"); |
| 3415 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3416 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3417 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3418 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3419 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3420 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3421 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3422 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3423 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3424 | int error = GetLastError(); |
| 3425 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3426 | goto exit; |
| 3427 | Py_DECREF(list); |
| 3428 | list = win32_error("FindFirstFile", namebuf); |
| 3429 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3430 | } |
| 3431 | do { |
| 3432 | /* Skip over . and .. */ |
| 3433 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3434 | strcmp(FileData.cFileName, "..") != 0) { |
| 3435 | v = PyBytes_FromString(FileData.cFileName); |
| 3436 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3437 | Py_DECREF(list); |
| 3438 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3439 | break; |
| 3440 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3441 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3442 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3443 | Py_DECREF(list); |
| 3444 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3445 | break; |
| 3446 | } |
| 3447 | Py_DECREF(v); |
| 3448 | } |
| 3449 | Py_BEGIN_ALLOW_THREADS |
| 3450 | result = FindNextFile(hFindFile, &FileData); |
| 3451 | Py_END_ALLOW_THREADS |
| 3452 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3453 | it got to the end of the directory. */ |
| 3454 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3455 | Py_DECREF(list); |
| 3456 | list = win32_error("FindNextFile", namebuf); |
| 3457 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3458 | } |
| 3459 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3460 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3461 | exit: |
| 3462 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3463 | if (FindClose(hFindFile) == FALSE) { |
| 3464 | if (list != NULL) { |
| 3465 | Py_DECREF(list); |
| 3466 | list = win32_error_object("FindClose", path.object); |
| 3467 | } |
| 3468 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3469 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3470 | if (wnamebuf) |
| 3471 | free(wnamebuf); |
| 3472 | path_cleanup(&path); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3473 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3474 | return list; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3475 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 3476 | #elif defined(PYOS_OS2) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3477 | if (path.length >= MAX_PATH) { |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 3478 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3479 | goto exit; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3480 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3481 | strcpy(namebuf, path.narrow); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3482 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3483 | if (*pt == ALTSEP) |
| 3484 | *pt = SEP; |
| 3485 | if (namebuf[len-1] != SEP) |
| 3486 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3487 | strcpy(namebuf + len, "*.*"); |
| 3488 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3489 | if ((list = PyList_New(0)) == NULL) { |
| 3490 | goto exit; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 3491 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3492 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3493 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 3494 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3495 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3496 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 3497 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 3498 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3499 | |
| 3500 | if (rc != NO_ERROR) { |
| 3501 | errno = ENOENT; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3502 | Py_DECREF(list); |
| 3503 | list = posix_error_with_filename(path.narrow); |
| 3504 | goto exit; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3507 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3508 | do { |
| 3509 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3510 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3511 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3512 | |
| 3513 | strcpy(namebuf, ep.achName); |
| 3514 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3515 | /* Leave Case of Name Alone -- In Native Form */ |
| 3516 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3517 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3518 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3519 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3520 | Py_DECREF(list); |
| 3521 | list = NULL; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3522 | break; |
| 3523 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3524 | if (PyList_Append(list, v) != 0) { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3525 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3526 | Py_DECREF(list); |
| 3527 | list = NULL; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3528 | break; |
| 3529 | } |
| 3530 | Py_DECREF(v); |
| 3531 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 3532 | } |
| 3533 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3534 | exit: |
| 3535 | path_cleanup(&path); |
| 3536 | |
| 3537 | return list; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3538 | #else |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 3539 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3540 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3541 | /* v is never read, so it does not need to be initialized yet. */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3542 | if (path.narrow && !PyArg_ParseTuple(args, "U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3543 | arg_is_unicode = 0; |
| 3544 | PyErr_Clear(); |
| 3545 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3546 | #ifdef HAVE_FDOPENDIR |
| 3547 | if (path.fd != -1) { |
| 3548 | /* closedir() closes the FD, so we duplicate it */ |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3549 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3550 | fd = dup(path.fd); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3551 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3552 | |
| 3553 | if (fd == -1) { |
| 3554 | list = posix_error(); |
| 3555 | goto exit; |
| 3556 | } |
| 3557 | |
| 3558 | Py_BEGIN_ALLOW_THREADS |
| 3559 | dirp = fdopendir(fd); |
| 3560 | Py_END_ALLOW_THREADS |
| 3561 | } |
| 3562 | else |
| 3563 | #endif |
| 3564 | { |
| 3565 | char *name = path.narrow ? path.narrow : "."; |
| 3566 | Py_BEGIN_ALLOW_THREADS |
| 3567 | dirp = opendir(name); |
| 3568 | Py_END_ALLOW_THREADS |
| 3569 | } |
| 3570 | |
| 3571 | if (dirp == NULL) { |
| 3572 | list = path_error("listdir", &path); |
| 3573 | goto exit; |
| 3574 | } |
| 3575 | if ((list = PyList_New(0)) == NULL) { |
| 3576 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3577 | } |
| 3578 | for (;;) { |
| 3579 | errno = 0; |
| 3580 | Py_BEGIN_ALLOW_THREADS |
| 3581 | ep = readdir(dirp); |
| 3582 | Py_END_ALLOW_THREADS |
| 3583 | if (ep == NULL) { |
| 3584 | if (errno == 0) { |
| 3585 | break; |
| 3586 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3587 | Py_DECREF(list); |
| 3588 | list = path_error("listdir", &path); |
| 3589 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3590 | } |
| 3591 | } |
| 3592 | if (ep->d_name[0] == '.' && |
| 3593 | (NAMLEN(ep) == 1 || |
| 3594 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3595 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3596 | if (arg_is_unicode) |
| 3597 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3598 | else |
| 3599 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3600 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3601 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3602 | break; |
| 3603 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3604 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3605 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3606 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3607 | break; |
| 3608 | } |
| 3609 | Py_DECREF(v); |
| 3610 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3611 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3612 | exit: |
| 3613 | if (dirp != NULL) { |
| 3614 | Py_BEGIN_ALLOW_THREADS |
| 3615 | if (fd > -1) |
| 3616 | rewinddir(dirp); |
| 3617 | closedir(dirp); |
| 3618 | Py_END_ALLOW_THREADS |
| 3619 | } |
| 3620 | |
| 3621 | path_cleanup(&path); |
| 3622 | |
| 3623 | return list; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3624 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 3625 | #endif /* which OS */ |
| 3626 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3627 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3628 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3629 | /* A helper function for abspath on win32 */ |
| 3630 | static PyObject * |
| 3631 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3632 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3633 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3634 | char outbuf[MAX_PATH*2]; |
| 3635 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3636 | PyObject *po; |
| 3637 | |
| 3638 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3639 | { |
| 3640 | wchar_t *wpath; |
| 3641 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 3642 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3643 | DWORD result; |
| 3644 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3645 | |
| 3646 | wpath = PyUnicode_AsUnicode(po); |
| 3647 | if (wpath == NULL) |
| 3648 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3649 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3650 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3651 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3652 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3653 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3654 | if (!woutbufp) |
| 3655 | return PyErr_NoMemory(); |
| 3656 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3657 | } |
| 3658 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3659 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3660 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3661 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3662 | if (woutbufp != woutbuf) |
| 3663 | free(woutbufp); |
| 3664 | return v; |
| 3665 | } |
| 3666 | /* Drop the argument parsing error as narrow strings |
| 3667 | are also valid. */ |
| 3668 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3669 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3670 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3671 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3672 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3673 | if (win32_warn_bytes_api()) |
| 3674 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3675 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3676 | outbuf, &temp)) { |
| 3677 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3678 | return NULL; |
| 3679 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3680 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3681 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3682 | Py_FileSystemDefaultEncoding, NULL); |
| 3683 | } |
| 3684 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3685 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3686 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3687 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3688 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3689 | /* A helper function for samepath on windows */ |
| 3690 | static PyObject * |
| 3691 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3692 | { |
| 3693 | HANDLE hFile; |
| 3694 | int buf_size; |
| 3695 | wchar_t *target_path; |
| 3696 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3697 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3698 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3699 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3700 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3701 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3702 | path = PyUnicode_AsUnicode(po); |
| 3703 | if (path == NULL) |
| 3704 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3705 | |
| 3706 | if(!check_GetFinalPathNameByHandle()) { |
| 3707 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3708 | NotImplementedError. */ |
| 3709 | return PyErr_Format(PyExc_NotImplementedError, |
| 3710 | "GetFinalPathNameByHandle not available on this platform"); |
| 3711 | } |
| 3712 | |
| 3713 | hFile = CreateFileW( |
| 3714 | path, |
| 3715 | 0, /* desired access */ |
| 3716 | 0, /* share mode */ |
| 3717 | NULL, /* security attributes */ |
| 3718 | OPEN_EXISTING, |
| 3719 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3720 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3721 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3722 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3723 | if(hFile == INVALID_HANDLE_VALUE) |
| 3724 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3725 | |
| 3726 | /* We have a good handle to the target, use it to determine the |
| 3727 | target path name. */ |
| 3728 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3729 | |
| 3730 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3731 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3732 | |
| 3733 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3734 | if(!target_path) |
| 3735 | return PyErr_NoMemory(); |
| 3736 | |
| 3737 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3738 | buf_size, VOLUME_NAME_DOS); |
| 3739 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3740 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3741 | |
| 3742 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3743 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3744 | |
| 3745 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3746 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3747 | free(target_path); |
| 3748 | return result; |
| 3749 | |
| 3750 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3751 | |
| 3752 | static PyObject * |
| 3753 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3754 | { |
| 3755 | HANDLE hFile; |
| 3756 | BY_HANDLE_FILE_INFORMATION info; |
| 3757 | int fd; |
| 3758 | |
| 3759 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3760 | return NULL; |
| 3761 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3762 | if (!_PyVerify_fd(fd)) |
| 3763 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3764 | |
| 3765 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3766 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3767 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3768 | |
| 3769 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3770 | return win32_error("_getfileinformation", NULL); |
| 3771 | |
| 3772 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3773 | info.nFileIndexHigh, |
| 3774 | info.nFileIndexLow); |
| 3775 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3776 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3777 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3778 | "Return true if the pathname refers to an existing directory."); |
| 3779 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3780 | static PyObject * |
| 3781 | posix__isdir(PyObject *self, PyObject *args) |
| 3782 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3783 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3784 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3785 | DWORD attributes; |
| 3786 | |
| 3787 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3788 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3789 | if (wpath == NULL) |
| 3790 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3791 | |
| 3792 | attributes = GetFileAttributesW(wpath); |
| 3793 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3794 | Py_RETURN_FALSE; |
| 3795 | goto check; |
| 3796 | } |
| 3797 | /* Drop the argument parsing error as narrow strings |
| 3798 | are also valid. */ |
| 3799 | PyErr_Clear(); |
| 3800 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3801 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3802 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3803 | if (win32_warn_bytes_api()) |
| 3804 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3805 | attributes = GetFileAttributesA(path); |
| 3806 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3807 | Py_RETURN_FALSE; |
| 3808 | |
| 3809 | check: |
| 3810 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3811 | Py_RETURN_TRUE; |
| 3812 | else |
| 3813 | Py_RETURN_FALSE; |
| 3814 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3815 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3816 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3817 | PyDoc_STRVAR(posix_mkdir__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3818 | "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 3819 | Create a directory.\n\ |
| 3820 | \n\ |
| 3821 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 3822 | and path should be relative; path will then be relative to that directory.\n\ |
| 3823 | dir_fd may not be implemented on your platform.\n\ |
| 3824 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 3825 | \n\ |
| 3826 | The mode argument is ignored on Windows."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3827 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3828 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3829 | posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3830 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3831 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3832 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3833 | int dir_fd = DEFAULT_DIR_FD; |
| 3834 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 3835 | PyObject *return_value = NULL; |
| 3836 | int result; |
| 3837 | |
| 3838 | memset(&path, 0, sizeof(path)); |
| 3839 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords, |
| 3840 | path_converter, &path, &mode, |
| 3841 | #ifdef HAVE_MKDIRAT |
| 3842 | dir_fd_converter, &dir_fd |
| 3843 | #else |
| 3844 | dir_fd_unavailable, &dir_fd |
| 3845 | #endif |
| 3846 | )) |
| 3847 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3848 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3849 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3850 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3851 | if (path.wide) |
| 3852 | result = CreateDirectoryW(path.wide, NULL); |
| 3853 | else |
| 3854 | result = CreateDirectoryA(path.narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3855 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3856 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3857 | if (!result) { |
| 3858 | return_value = win32_error_object("mkdir", path.object); |
| 3859 | goto exit; |
| 3860 | } |
| 3861 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3862 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3863 | #if HAVE_MKDIRAT |
| 3864 | if (dir_fd != DEFAULT_DIR_FD) |
| 3865 | result = mkdirat(dir_fd, path.narrow, mode); |
| 3866 | else |
| 3867 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3868 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3869 | result = mkdir(path.narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3870 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3871 | result = mkdir(path.narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3872 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3873 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3874 | if (result < 0) { |
| 3875 | return_value = path_error("mkdir", &path); |
| 3876 | goto exit; |
| 3877 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3878 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3879 | return_value = Py_None; |
| 3880 | Py_INCREF(Py_None); |
| 3881 | exit: |
| 3882 | path_cleanup(&path); |
| 3883 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3886 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3887 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3888 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3889 | #include <sys/resource.h> |
| 3890 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3891 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3892 | |
| 3893 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3894 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3895 | "nice(inc) -> new_priority\n\n\ |
| 3896 | 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] | 3897 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3898 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3899 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3900 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3901 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3902 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3903 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3904 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3905 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3906 | /* There are two flavours of 'nice': one that returns the new |
| 3907 | priority (as required by almost all standards out there) and the |
| 3908 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3909 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3910 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3911 | If we are of the nice family that returns the new priority, we |
| 3912 | need to clear errno before the call, and check if errno is filled |
| 3913 | before calling posix_error() on a returnvalue of -1, because the |
| 3914 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3915 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3916 | errno = 0; |
| 3917 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3918 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3919 | if (value == 0) |
| 3920 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3921 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3922 | if (value == -1 && errno != 0) |
| 3923 | /* either nice() or getpriority() returned an error */ |
| 3924 | return posix_error(); |
| 3925 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3926 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3927 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3928 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3929 | |
| 3930 | #ifdef HAVE_GETPRIORITY |
| 3931 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3932 | "getpriority(which, who) -> current_priority\n\n\ |
| 3933 | Get program scheduling priority."); |
| 3934 | |
| 3935 | static PyObject * |
| 3936 | posix_getpriority(PyObject *self, PyObject *args) |
| 3937 | { |
| 3938 | int which, who, retval; |
| 3939 | |
| 3940 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3941 | return NULL; |
| 3942 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3943 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3944 | if (errno != 0) |
| 3945 | return posix_error(); |
| 3946 | return PyLong_FromLong((long)retval); |
| 3947 | } |
| 3948 | #endif /* HAVE_GETPRIORITY */ |
| 3949 | |
| 3950 | |
| 3951 | #ifdef HAVE_SETPRIORITY |
| 3952 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3953 | "setpriority(which, who, prio) -> None\n\n\ |
| 3954 | Set program scheduling priority."); |
| 3955 | |
| 3956 | static PyObject * |
| 3957 | posix_setpriority(PyObject *self, PyObject *args) |
| 3958 | { |
| 3959 | int which, who, prio, retval; |
| 3960 | |
| 3961 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3962 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3963 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3964 | if (retval == -1) |
| 3965 | return posix_error(); |
| 3966 | Py_RETURN_NONE; |
| 3967 | } |
| 3968 | #endif /* HAVE_SETPRIORITY */ |
| 3969 | |
| 3970 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3971 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3972 | internal_rename(PyObject *args, PyObject *kwargs, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3973 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3974 | char *function_name = is_replace ? "replace" : "rename"; |
| 3975 | path_t src; |
| 3976 | path_t dst; |
| 3977 | int src_dir_fd = DEFAULT_DIR_FD; |
| 3978 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 3979 | int dir_fd_specified; |
| 3980 | PyObject *return_value = NULL; |
| 3981 | char format[24]; |
| 3982 | static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 3983 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3984 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3985 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3986 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3987 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3988 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3989 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3990 | |
| 3991 | memset(&src, 0, sizeof(src)); |
| 3992 | memset(&dst, 0, sizeof(dst)); |
| 3993 | strcpy(format, "O&O&|$O&O&:"); |
| 3994 | strcat(format, function_name); |
| 3995 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords, |
| 3996 | path_converter, &src, |
| 3997 | path_converter, &dst, |
| 3998 | dir_fd_converter, &src_dir_fd, |
| 3999 | dir_fd_converter, &dst_dir_fd)) |
| 4000 | return NULL; |
| 4001 | |
| 4002 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4003 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4004 | #ifndef HAVE_RENAMEAT |
| 4005 | if (dir_fd_specified) { |
| 4006 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 4007 | goto exit; |
| 4008 | } |
| 4009 | #endif |
| 4010 | |
| 4011 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 4012 | PyErr_Format(PyExc_ValueError, |
| 4013 | "%s: src and dst must be the same type", function_name); |
| 4014 | goto exit; |
| 4015 | } |
| 4016 | |
| 4017 | #ifdef MS_WINDOWS |
| 4018 | Py_BEGIN_ALLOW_THREADS |
| 4019 | if (src.wide) |
| 4020 | result = MoveFileExW(src.wide, dst.wide, flags); |
| 4021 | else |
| 4022 | result = MoveFileExA(src.narrow, dst.narrow, flags); |
| 4023 | Py_END_ALLOW_THREADS |
| 4024 | |
| 4025 | if (!result) { |
| 4026 | return_value = win32_error_object(function_name, dst.object); |
| 4027 | goto exit; |
| 4028 | } |
| 4029 | |
| 4030 | #else |
| 4031 | Py_BEGIN_ALLOW_THREADS |
| 4032 | #ifdef HAVE_RENAMEAT |
| 4033 | if (dir_fd_specified) |
| 4034 | result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow); |
| 4035 | else |
| 4036 | #endif |
| 4037 | result = rename(src.narrow, dst.narrow); |
| 4038 | Py_END_ALLOW_THREADS |
| 4039 | |
| 4040 | if (result) { |
| 4041 | return_value = path_error(function_name, &dst); |
| 4042 | goto exit; |
| 4043 | } |
| 4044 | #endif |
| 4045 | |
| 4046 | Py_INCREF(Py_None); |
| 4047 | return_value = Py_None; |
| 4048 | exit: |
| 4049 | path_cleanup(&src); |
| 4050 | path_cleanup(&dst); |
| 4051 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4054 | PyDoc_STRVAR(posix_rename__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4055 | "rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4056 | Rename a file or directory.\n\ |
| 4057 | \n\ |
| 4058 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4059 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4060 | should be relative; the path will then be relative to that directory.\n\ |
| 4061 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4062 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4063 | |
| 4064 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4065 | posix_rename(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4066 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4067 | return internal_rename(args, kwargs, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | PyDoc_STRVAR(posix_replace__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4071 | "replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\ |
| 4072 | Rename a file or directory, overwriting the destination.\n\ |
| 4073 | \n\ |
| 4074 | If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\ |
| 4075 | descriptor open to a directory, and the respective path string (src or dst)\n\ |
| 4076 | should be relative; the path will then be relative to that directory.\n\ |
| 4077 | src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\ |
| 4078 | If they are unavailable, using them will raise a NotImplementedError."); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4079 | |
| 4080 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4081 | posix_replace(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4082 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4083 | return internal_rename(args, kwargs, 1); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4084 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4085 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4086 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4087 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4088 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4089 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4090 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4091 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4092 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4093 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4094 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4095 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4096 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4097 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4100 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4101 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4102 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4103 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4104 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4105 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4106 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4107 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4108 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4109 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 4110 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4111 | wchar_t *command; |
| 4112 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 4113 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4114 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4115 | Py_BEGIN_ALLOW_THREADS |
| 4116 | sts = _wsystem(command); |
| 4117 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4118 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4119 | PyObject *command_obj; |
| 4120 | char *command; |
| 4121 | if (!PyArg_ParseTuple(args, "O&:system", |
| 4122 | PyUnicode_FSConverter, &command_obj)) |
| 4123 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4124 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4125 | command = PyBytes_AsString(command_obj); |
| 4126 | Py_BEGIN_ALLOW_THREADS |
| 4127 | sts = system(command); |
| 4128 | Py_END_ALLOW_THREADS |
| 4129 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 4130 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4131 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4132 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4133 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4134 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4136 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4137 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4138 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4139 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4140 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4141 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4142 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4143 | int i; |
| 4144 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 4145 | return NULL; |
| 4146 | i = (int)umask(i); |
| 4147 | if (i < 0) |
| 4148 | return posix_error(); |
| 4149 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4152 | #ifdef MS_WINDOWS |
| 4153 | |
| 4154 | /* override the default DeleteFileW behavior so that directory |
| 4155 | symlinks can be removed with this function, the same as with |
| 4156 | Unix symlinks */ |
| 4157 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4158 | { |
| 4159 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4160 | WIN32_FIND_DATAW find_data; |
| 4161 | HANDLE find_data_handle; |
| 4162 | int is_directory = 0; |
| 4163 | int is_link = 0; |
| 4164 | |
| 4165 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4166 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4167 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4168 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4169 | it is a symlink */ |
| 4170 | if(is_directory && |
| 4171 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4172 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4173 | |
| 4174 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 4175 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 4176 | FindClose(find_data_handle); |
| 4177 | } |
| 4178 | } |
| 4179 | } |
| 4180 | |
| 4181 | if (is_directory && is_link) |
| 4182 | return RemoveDirectoryW(lpFileName); |
| 4183 | |
| 4184 | return DeleteFileW(lpFileName); |
| 4185 | } |
| 4186 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4187 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4188 | PyDoc_STRVAR(posix_unlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4189 | "unlink(path, *, dir_fd=None, rmdir=False)\n\n\ |
| 4190 | Remove a file (same as remove()).\n\ |
| 4191 | \n\ |
| 4192 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4193 | and path should be relative; path will then be relative to that directory.\n\ |
| 4194 | dir_fd may not be implemented on your platform.\n\ |
| 4195 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 4196 | If rmdir is True, unlink will behave like os.rmdir()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4198 | PyDoc_STRVAR(posix_remove__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4199 | "remove(path, *, dir_fd=None, rmdir=False)\n\n\ |
| 4200 | Remove a file (same as unlink()).\n\ |
| 4201 | \n\ |
| 4202 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4203 | and path should be relative; path will then be relative to that directory.\n\ |
| 4204 | dir_fd may not be implemented on your platform.\n\ |
| 4205 | If it is unavailable, using it will raise a NotImplementedError.\n\ |
| 4206 | If rmdir is True, remove will behave like os.rmdir()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4207 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4208 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4209 | posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4210 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4211 | path_t path; |
| 4212 | int dir_fd = DEFAULT_DIR_FD; |
| 4213 | int remove_dir = 0; |
| 4214 | static char *keywords[] = {"path", "dir_fd", "rmdir", NULL}; |
| 4215 | int result; |
| 4216 | PyObject *return_value = NULL; |
| 4217 | |
| 4218 | memset(&path, 0, sizeof(path)); |
| 4219 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:unlink", keywords, |
| 4220 | path_converter, &path, |
| 4221 | #ifdef HAVE_UNLINKAT |
| 4222 | dir_fd_converter, &dir_fd, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4223 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4224 | dir_fd_unavailable, &dir_fd, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4225 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4226 | &remove_dir)) |
| 4227 | return NULL; |
| 4228 | |
| 4229 | Py_BEGIN_ALLOW_THREADS |
| 4230 | #ifdef MS_WINDOWS |
| 4231 | if (remove_dir) { |
| 4232 | if (path.wide) |
| 4233 | result = RemoveDirectoryW(path.wide); |
| 4234 | else |
| 4235 | result = RemoveDirectoryA(path.narrow); |
| 4236 | } |
| 4237 | else { |
| 4238 | if (path.wide) |
| 4239 | result = Py_DeleteFileW(path.wide); |
| 4240 | else |
| 4241 | result = DeleteFileA(path.narrow); |
| 4242 | } |
| 4243 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 4244 | #else |
| 4245 | if (remove_dir && (dir_fd == DEFAULT_DIR_FD)) |
| 4246 | result = rmdir(path.narrow); |
| 4247 | else |
| 4248 | #ifdef HAVE_UNLINKAT |
| 4249 | if (dir_fd != DEFAULT_DIR_FD) |
| 4250 | result = unlinkat(dir_fd, path.narrow, remove_dir ? AT_REMOVEDIR : 0); |
| 4251 | else |
| 4252 | #endif /* HAVE_UNLINKAT */ |
| 4253 | result = unlink(path.narrow); |
| 4254 | #endif |
| 4255 | Py_END_ALLOW_THREADS |
| 4256 | |
| 4257 | if (result) { |
| 4258 | return_value = path_error("unlink", &path); |
| 4259 | goto exit; |
| 4260 | } |
| 4261 | |
| 4262 | return_value = Py_None; |
| 4263 | Py_INCREF(Py_None); |
| 4264 | |
| 4265 | exit: |
| 4266 | path_cleanup(&path); |
| 4267 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4270 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4271 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4272 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4273 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4274 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4275 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4276 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4277 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4278 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4279 | struct utsname u; |
| 4280 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4281 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4282 | Py_BEGIN_ALLOW_THREADS |
| 4283 | res = uname(&u); |
| 4284 | Py_END_ALLOW_THREADS |
| 4285 | if (res < 0) |
| 4286 | return posix_error(); |
| 4287 | return Py_BuildValue("(sssss)", |
| 4288 | u.sysname, |
| 4289 | u.nodename, |
| 4290 | u.release, |
| 4291 | u.version, |
| 4292 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4293 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4294 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4295 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4296 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4297 | PyDoc_STRVAR(posix_utime__doc__, |
| 4298 | "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 4299 | Set the access and modified time of path.\n\ |
| 4300 | \n\ |
| 4301 | path may always be specified as a string.\n\ |
| 4302 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 4303 | If this functionality is unavailable, using it raises an exception.\n\ |
| 4304 | \n\ |
| 4305 | If times is not None, it must be a tuple (atime, mtime);\n\ |
| 4306 | atime and mtime should be expressed as float seconds since the epoch.\n\ |
| 4307 | If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\ |
| 4308 | atime_ns and mtime_ns should be expressed as integer nanoseconds\n\ |
| 4309 | since the epoch.\n\ |
| 4310 | If both times and ns are None, utime uses the current time.\n\ |
| 4311 | Specifying tuples for both times and ns is an error.\n\ |
| 4312 | \n\ |
| 4313 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 4314 | and path should be relative; path will then be relative to that directory.\n\ |
| 4315 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 4316 | link, utime will modify the symbolic link itself instead of the file the\n\ |
| 4317 | link points to.\n\ |
| 4318 | It is an error to use dir_fd or follow_symlinks when specifying path\n\ |
| 4319 | as an open file descriptor.\n\ |
| 4320 | dir_fd and follow_symlinks may not be available on your platform.\n\ |
| 4321 | If they are unavailable, using them will raise a NotImplementedError."); |
| 4322 | |
| 4323 | typedef struct { |
| 4324 | int now; |
| 4325 | time_t atime_s; |
| 4326 | long atime_ns; |
| 4327 | time_t mtime_s; |
| 4328 | long mtime_ns; |
| 4329 | } utime_t; |
| 4330 | |
| 4331 | /* |
| 4332 | * these macros assume that "utime" is a pointer to a utime_t |
| 4333 | * they also intentionally leak the declaration of a pointer named "time" |
| 4334 | */ |
| 4335 | #define UTIME_TO_TIMESPEC \ |
| 4336 | struct timespec ts[2]; \ |
| 4337 | struct timespec *time; \ |
| 4338 | if (utime->now) \ |
| 4339 | time = NULL; \ |
| 4340 | else { \ |
| 4341 | ts[0].tv_sec = utime->atime_s; \ |
| 4342 | ts[0].tv_nsec = utime->atime_ns; \ |
| 4343 | ts[1].tv_sec = utime->mtime_s; \ |
| 4344 | ts[1].tv_nsec = utime->mtime_ns; \ |
| 4345 | time = ts; \ |
| 4346 | } \ |
| 4347 | |
| 4348 | #define UTIME_TO_TIMEVAL \ |
| 4349 | struct timeval tv[2]; \ |
| 4350 | struct timeval *time; \ |
| 4351 | if (utime->now) \ |
| 4352 | time = NULL; \ |
| 4353 | else { \ |
| 4354 | tv[0].tv_sec = utime->atime_s; \ |
| 4355 | tv[0].tv_usec = utime->atime_ns / 1000; \ |
| 4356 | tv[1].tv_sec = utime->mtime_s; \ |
| 4357 | tv[1].tv_usec = utime->mtime_ns / 1000; \ |
| 4358 | time = tv; \ |
| 4359 | } \ |
| 4360 | |
| 4361 | #define UTIME_TO_UTIMBUF \ |
| 4362 | struct utimbuf u[2]; \ |
| 4363 | struct utimbuf *time; \ |
| 4364 | if (utime->now) \ |
| 4365 | time = NULL; \ |
| 4366 | else { \ |
| 4367 | u.actime = utime->atime_s; \ |
| 4368 | u.modtime = utime->mtime_s; \ |
| 4369 | time = u; \ |
| 4370 | } |
| 4371 | |
| 4372 | #define UTIME_TO_TIME_T \ |
| 4373 | time_t timet[2]; \ |
| 4374 | struct timet time; \ |
| 4375 | if (utime->now) \ |
| 4376 | time = NULL; \ |
| 4377 | else { \ |
| 4378 | timet[0] = utime->atime_s; \ |
| 4379 | timet[1] = utime->mtime_s; \ |
| 4380 | time = &timet; \ |
| 4381 | } \ |
| 4382 | |
| 4383 | |
| 4384 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 4385 | |
| 4386 | #if UTIME_HAVE_DIR_FD |
| 4387 | |
| 4388 | static int |
| 4389 | utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks) |
| 4390 | { |
| 4391 | #ifdef HAVE_UTIMENSAT |
| 4392 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4393 | UTIME_TO_TIMESPEC; |
| 4394 | return utimensat(dir_fd, path, time, flags); |
| 4395 | #elif defined(HAVE_FUTIMESAT) |
| 4396 | UTIME_TO_TIMEVAL; |
| 4397 | /* |
| 4398 | * follow_symlinks will never be false here; |
| 4399 | * we only allow !follow_symlinks and dir_fd together |
| 4400 | * if we have utimensat() |
| 4401 | */ |
| 4402 | assert(follow_symlinks); |
| 4403 | return futimesat(dir_fd, path, time); |
| 4404 | #endif |
| 4405 | } |
| 4406 | |
| 4407 | #endif |
| 4408 | |
| 4409 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 4410 | |
| 4411 | #if UTIME_HAVE_FD |
| 4412 | |
| 4413 | static int |
| 4414 | utime_fd(utime_t *utime, int fd) |
| 4415 | { |
| 4416 | #ifdef HAVE_FUTIMENS |
| 4417 | UTIME_TO_TIMESPEC; |
| 4418 | return futimens(fd, time); |
| 4419 | #else |
| 4420 | UTIME_TO_TIMEVAL; |
| 4421 | return futimes(fd, time); |
| 4422 | #endif |
| 4423 | } |
| 4424 | |
| 4425 | #endif |
| 4426 | |
| 4427 | |
| 4428 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 4429 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 4430 | |
| 4431 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4432 | |
| 4433 | static int |
| 4434 | utime_nofollow_symlinks(utime_t *utime, char *path) |
| 4435 | { |
| 4436 | #ifdef HAVE_UTIMENSAT |
| 4437 | UTIME_TO_TIMESPEC; |
| 4438 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4439 | #else |
| 4440 | UTIME_TO_TIMEVAL; |
| 4441 | return lutimes(path, time); |
| 4442 | #endif |
| 4443 | } |
| 4444 | |
| 4445 | #endif |
| 4446 | |
| 4447 | #ifndef MS_WINDOWS |
| 4448 | |
| 4449 | static int |
| 4450 | utime_default(utime_t *utime, char *path) |
| 4451 | { |
| 4452 | #ifdef HAVE_UTIMENSAT |
| 4453 | UTIME_TO_TIMESPEC; |
| 4454 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4455 | #elif defined(HAVE_UTIMES) |
| 4456 | UTIME_TO_TIMEVAL; |
| 4457 | return utimes(path, time); |
| 4458 | #elif defined(HAVE_UTIME_H) |
| 4459 | UTIME_TO_UTIMBUF; |
| 4460 | return utime(path, time); |
| 4461 | #else |
| 4462 | UTIME_TO_TIME_T; |
| 4463 | return utime(path, time); |
| 4464 | #endif |
| 4465 | } |
| 4466 | |
| 4467 | #endif |
| 4468 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4469 | static int |
| 4470 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4471 | { |
| 4472 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4473 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4474 | divmod = PyNumber_Divmod(py_long, billion); |
| 4475 | if (!divmod) |
| 4476 | goto exit; |
| 4477 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4478 | if ((*s == -1) && PyErr_Occurred()) |
| 4479 | goto exit; |
| 4480 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4481 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4482 | goto exit; |
| 4483 | |
| 4484 | result = 1; |
| 4485 | exit: |
| 4486 | Py_XDECREF(divmod); |
| 4487 | return result; |
| 4488 | } |
| 4489 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4490 | static PyObject * |
| 4491 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4492 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4493 | path_t path; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4494 | PyObject *times = NULL; |
| 4495 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4496 | int dir_fd = DEFAULT_DIR_FD; |
| 4497 | int follow_symlinks = 1; |
| 4498 | char *keywords[] = {"path", "times", "ns", "dir_fd", |
| 4499 | "follow_symlinks", NULL}; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4500 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4501 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4502 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4503 | #ifdef MS_WINDOWS |
| 4504 | HANDLE hFile; |
| 4505 | FILETIME atime, mtime; |
| 4506 | #else |
| 4507 | int result; |
| 4508 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4509 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4510 | PyObject *return_value = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4511 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4512 | memset(&path, 0, sizeof(path)); |
| 4513 | #if UTIME_HAVE_FD |
| 4514 | path.allow_fd = 1; |
| 4515 | #endif |
| 4516 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4517 | "O&|O$OO&p:utime", keywords, |
| 4518 | path_converter, &path, |
| 4519 | ×, &ns, |
| 4520 | #if UTIME_HAVE_DIR_FD |
| 4521 | dir_fd_converter, &dir_fd, |
| 4522 | #else |
| 4523 | dir_fd_unavailable, &dir_fd, |
| 4524 | #endif |
| 4525 | &follow_symlinks |
| 4526 | )) |
| 4527 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4528 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4529 | if (times && (times != Py_None) && ns) { |
| 4530 | PyErr_SetString(PyExc_ValueError, |
| 4531 | "utime: you may specify either 'times'" |
| 4532 | " or 'ns' but not both"); |
| 4533 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | if (times && (times != Py_None)) { |
| 4537 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4538 | PyErr_SetString(PyExc_TypeError, |
| 4539 | "utime: 'times' must be either" |
| 4540 | " a tuple of two ints or None"); |
| 4541 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4542 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4543 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4544 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4545 | &utime.atime_s, &utime.atime_ns) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4546 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4547 | &utime.mtime_s, &utime.mtime_ns) == -1) { |
| 4548 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4549 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4550 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4551 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4552 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4553 | PyErr_SetString(PyExc_TypeError, |
| 4554 | "utime: 'ns' must be a tuple of two ints"); |
| 4555 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4556 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4557 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4558 | 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] | 4559 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4560 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4561 | &utime.mtime_s, &utime.mtime_ns)) { |
| 4562 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4563 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4564 | } |
| 4565 | else { |
| 4566 | /* times and ns are both None/unspecified. use "now". */ |
| 4567 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4568 | } |
| 4569 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4570 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4571 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 4572 | goto exit; |
| 4573 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4574 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4575 | if (path_and_dir_fd_invalid("utime", &path, dir_fd) || |
| 4576 | dir_fd_and_fd_invalid("utime", dir_fd, path.fd) || |
| 4577 | fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks)) |
| 4578 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4579 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4580 | #if !defined(HAVE_UTIMENSAT) |
| 4581 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 4582 | PyErr_SetString(PyExc_RuntimeError, |
| 4583 | "utime: cannot use dir_fd and follow_symlinks " |
| 4584 | "together on this platform"); |
| 4585 | goto exit; |
| 4586 | } |
| 4587 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4588 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4589 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4590 | Py_BEGIN_ALLOW_THREADS |
| 4591 | if (path.wide) |
| 4592 | hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4593 | NULL, OPEN_EXISTING, |
| 4594 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4595 | else |
| 4596 | hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4597 | NULL, OPEN_EXISTING, |
| 4598 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4599 | Py_END_ALLOW_THREADS |
| 4600 | if (hFile == INVALID_HANDLE_VALUE) { |
| 4601 | win32_error_object("utime", path.object); |
| 4602 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4603 | } |
| 4604 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4605 | if (utime.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4606 | SYSTEMTIME now; |
| 4607 | GetSystemTime(&now); |
| 4608 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 4609 | !SystemTimeToFileTime(&now, &atime)) { |
| 4610 | win32_error("utime", NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4611 | goto exit; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4612 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4613 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4614 | else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4615 | time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4616 | time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4617 | } |
| 4618 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4619 | /* Avoid putting the file name into the error here, |
| 4620 | as that may confuse the user into believing that |
| 4621 | something is wrong with the file, when it also |
| 4622 | could be the time stamp that gives a problem. */ |
| 4623 | win32_error("utime", NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4624 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4625 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4626 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4627 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4628 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4629 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4630 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 4631 | result = utime_nofollow_symlinks(&utime, path.narrow); |
| 4632 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4633 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4634 | |
| 4635 | #if UTIME_HAVE_DIR_FD |
| 4636 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
| 4637 | result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks); |
| 4638 | else |
| 4639 | #endif |
| 4640 | |
| 4641 | #if UTIME_HAVE_FD |
| 4642 | if (path.fd != -1) |
| 4643 | result = utime_fd(&utime, path.fd); |
| 4644 | else |
| 4645 | #endif |
| 4646 | |
| 4647 | result = utime_default(&utime, path.narrow); |
| 4648 | |
| 4649 | Py_END_ALLOW_THREADS |
| 4650 | |
| 4651 | if (result < 0) { |
| 4652 | /* see previous comment about not putting filename in error here */ |
| 4653 | return_value = posix_error(); |
| 4654 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4655 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4656 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4657 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4658 | |
| 4659 | Py_INCREF(Py_None); |
| 4660 | return_value = Py_None; |
| 4661 | |
| 4662 | exit: |
| 4663 | path_cleanup(&path); |
| 4664 | #ifdef MS_WINDOWS |
| 4665 | if (hFile != INVALID_HANDLE_VALUE) |
| 4666 | CloseHandle(hFile); |
| 4667 | #endif |
| 4668 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4671 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4672 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4673 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4674 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4675 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4676 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4677 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4678 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4679 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4680 | int sts; |
| 4681 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 4682 | return NULL; |
| 4683 | _exit(sts); |
| 4684 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4687 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4688 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4689 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4690 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4691 | Py_ssize_t i; |
| 4692 | for (i = 0; i < count; i++) |
| 4693 | PyMem_Free(array[i]); |
| 4694 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4695 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4696 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4697 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4698 | int fsconvert_strdup(PyObject *o, char**out) |
| 4699 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4700 | PyObject *bytes; |
| 4701 | Py_ssize_t size; |
| 4702 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4703 | return 0; |
| 4704 | size = PyBytes_GET_SIZE(bytes); |
| 4705 | *out = PyMem_Malloc(size+1); |
| 4706 | if (!*out) |
| 4707 | return 0; |
| 4708 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4709 | Py_DECREF(bytes); |
| 4710 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4711 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4712 | #endif |
| 4713 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4714 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4715 | static char** |
| 4716 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4717 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4718 | char **envlist; |
| 4719 | Py_ssize_t i, pos, envc; |
| 4720 | PyObject *keys=NULL, *vals=NULL; |
| 4721 | PyObject *key, *val, *key2, *val2; |
| 4722 | char *p, *k, *v; |
| 4723 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4724 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4725 | i = PyMapping_Size(env); |
| 4726 | if (i < 0) |
| 4727 | return NULL; |
| 4728 | envlist = PyMem_NEW(char *, i + 1); |
| 4729 | if (envlist == NULL) { |
| 4730 | PyErr_NoMemory(); |
| 4731 | return NULL; |
| 4732 | } |
| 4733 | envc = 0; |
| 4734 | keys = PyMapping_Keys(env); |
| 4735 | vals = PyMapping_Values(env); |
| 4736 | if (!keys || !vals) |
| 4737 | goto error; |
| 4738 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4739 | PyErr_Format(PyExc_TypeError, |
| 4740 | "env.keys() or env.values() is not a list"); |
| 4741 | goto error; |
| 4742 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4743 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4744 | for (pos = 0; pos < i; pos++) { |
| 4745 | key = PyList_GetItem(keys, pos); |
| 4746 | val = PyList_GetItem(vals, pos); |
| 4747 | if (!key || !val) |
| 4748 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4749 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4750 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4751 | goto error; |
| 4752 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4753 | Py_DECREF(key2); |
| 4754 | goto error; |
| 4755 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4756 | |
| 4757 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4758 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 4759 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4760 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4761 | k = PyBytes_AsString(key2); |
| 4762 | v = PyBytes_AsString(val2); |
| 4763 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4764 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4765 | p = PyMem_NEW(char, len); |
| 4766 | if (p == NULL) { |
| 4767 | PyErr_NoMemory(); |
| 4768 | Py_DECREF(key2); |
| 4769 | Py_DECREF(val2); |
| 4770 | goto error; |
| 4771 | } |
| 4772 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4773 | envlist[envc++] = p; |
| 4774 | Py_DECREF(key2); |
| 4775 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4776 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4777 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4778 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4779 | } |
| 4780 | Py_DECREF(vals); |
| 4781 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4782 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4783 | envlist[envc] = 0; |
| 4784 | *envc_ptr = envc; |
| 4785 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4786 | |
| 4787 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4788 | Py_XDECREF(keys); |
| 4789 | Py_XDECREF(vals); |
| 4790 | while (--envc >= 0) |
| 4791 | PyMem_DEL(envlist[envc]); |
| 4792 | PyMem_DEL(envlist); |
| 4793 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4794 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4795 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4796 | static char** |
| 4797 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4798 | { |
| 4799 | int i; |
| 4800 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4801 | if (argvlist == NULL) { |
| 4802 | PyErr_NoMemory(); |
| 4803 | return NULL; |
| 4804 | } |
| 4805 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4806 | PyObject* item = PySequence_ITEM(argv, i); |
| 4807 | if (item == NULL) |
| 4808 | goto fail; |
| 4809 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4810 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4811 | goto fail; |
| 4812 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4813 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4814 | } |
| 4815 | argvlist[*argc] = NULL; |
| 4816 | return argvlist; |
| 4817 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4818 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4819 | free_string_array(argvlist, *argc); |
| 4820 | return NULL; |
| 4821 | } |
| 4822 | #endif |
| 4823 | |
| 4824 | #ifdef HAVE_EXECV |
| 4825 | PyDoc_STRVAR(posix_execv__doc__, |
| 4826 | "execv(path, args)\n\n\ |
| 4827 | Execute an executable path with arguments, replacing current process.\n\ |
| 4828 | \n\ |
| 4829 | path: path of executable file\n\ |
| 4830 | args: tuple or list of strings"); |
| 4831 | |
| 4832 | static PyObject * |
| 4833 | posix_execv(PyObject *self, PyObject *args) |
| 4834 | { |
| 4835 | PyObject *opath; |
| 4836 | char *path; |
| 4837 | PyObject *argv; |
| 4838 | char **argvlist; |
| 4839 | Py_ssize_t argc; |
| 4840 | |
| 4841 | /* execv has two arguments: (path, argv), where |
| 4842 | argv is a list or tuple of strings. */ |
| 4843 | |
| 4844 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4845 | PyUnicode_FSConverter, |
| 4846 | &opath, &argv)) |
| 4847 | return NULL; |
| 4848 | path = PyBytes_AsString(opath); |
| 4849 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4850 | PyErr_SetString(PyExc_TypeError, |
| 4851 | "execv() arg 2 must be a tuple or list"); |
| 4852 | Py_DECREF(opath); |
| 4853 | return NULL; |
| 4854 | } |
| 4855 | argc = PySequence_Size(argv); |
| 4856 | if (argc < 1) { |
| 4857 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4858 | Py_DECREF(opath); |
| 4859 | return NULL; |
| 4860 | } |
| 4861 | |
| 4862 | argvlist = parse_arglist(argv, &argc); |
| 4863 | if (argvlist == NULL) { |
| 4864 | Py_DECREF(opath); |
| 4865 | return NULL; |
| 4866 | } |
| 4867 | |
| 4868 | execv(path, argvlist); |
| 4869 | |
| 4870 | /* If we get here it's definitely an error */ |
| 4871 | |
| 4872 | free_string_array(argvlist, argc); |
| 4873 | Py_DECREF(opath); |
| 4874 | return posix_error(); |
| 4875 | } |
| 4876 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4877 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4878 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4879 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4880 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4881 | path: path of executable file\n\ |
| 4882 | args: tuple or list of arguments\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4883 | env: dictionary of strings mapping to strings\n\ |
| 4884 | \n\ |
| 4885 | On some platforms, you may specify an open file descriptor for path;\n\ |
| 4886 | execve will execute the program the file descriptor is open to.\n\ |
| 4887 | If this functionality is unavailable, using it raises NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4888 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4889 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4890 | posix_execve(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4891 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4892 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4893 | PyObject *argv, *env; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4894 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4895 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4896 | Py_ssize_t argc, envc; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4897 | static char *keywords[] = {"path", "argv", "environment", NULL}; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4898 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4899 | /* execve has three arguments: (path, argv, env), where |
| 4900 | argv is a list or tuple of strings and env is a dictionary |
| 4901 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4902 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4903 | memset(&path, 0, sizeof(path)); |
| 4904 | #ifdef HAVE_FEXECVE |
| 4905 | path.allow_fd = 1; |
| 4906 | #endif |
| 4907 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords, |
| 4908 | path_converter, &path, |
| 4909 | &argv, &env |
| 4910 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4911 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4912 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4913 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4914 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4915 | "execve: argv must be a tuple or list"); |
| 4916 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4917 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4918 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4919 | if (!PyMapping_Check(env)) { |
| 4920 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4921 | "execve: environment must be a mapping object"); |
| 4922 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4923 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4924 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4925 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4926 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4927 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4928 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4929 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4930 | envlist = parse_envlist(env, &envc); |
| 4931 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4932 | goto fail; |
| 4933 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4934 | #ifdef HAVE_FEXECVE |
| 4935 | if (path.fd > -1) |
| 4936 | fexecve(path.fd, argvlist, envlist); |
| 4937 | else |
| 4938 | #endif |
| 4939 | execve(path.narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4940 | |
| 4941 | /* If we get here it's definitely an error */ |
| 4942 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4943 | path_posix_error("execve", &path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4944 | |
| 4945 | while (--envc >= 0) |
| 4946 | PyMem_DEL(envlist[envc]); |
| 4947 | PyMem_DEL(envlist); |
| 4948 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4949 | if (argvlist) |
| 4950 | free_string_array(argvlist, argc); |
| 4951 | path_cleanup(&path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4952 | return NULL; |
| 4953 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4954 | #endif /* HAVE_EXECV */ |
| 4955 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4956 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4957 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4958 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4959 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4960 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4961 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4962 | mode: mode of process creation\n\ |
| 4963 | path: path of executable file\n\ |
| 4964 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4965 | |
| 4966 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4967 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4968 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4969 | PyObject *opath; |
| 4970 | char *path; |
| 4971 | PyObject *argv; |
| 4972 | char **argvlist; |
| 4973 | int mode, i; |
| 4974 | Py_ssize_t argc; |
| 4975 | Py_intptr_t spawnval; |
| 4976 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4977 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4978 | /* spawnv has three arguments: (mode, path, argv), where |
| 4979 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4980 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4981 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4982 | PyUnicode_FSConverter, |
| 4983 | &opath, &argv)) |
| 4984 | return NULL; |
| 4985 | path = PyBytes_AsString(opath); |
| 4986 | if (PyList_Check(argv)) { |
| 4987 | argc = PyList_Size(argv); |
| 4988 | getitem = PyList_GetItem; |
| 4989 | } |
| 4990 | else if (PyTuple_Check(argv)) { |
| 4991 | argc = PyTuple_Size(argv); |
| 4992 | getitem = PyTuple_GetItem; |
| 4993 | } |
| 4994 | else { |
| 4995 | PyErr_SetString(PyExc_TypeError, |
| 4996 | "spawnv() arg 2 must be a tuple or list"); |
| 4997 | Py_DECREF(opath); |
| 4998 | return NULL; |
| 4999 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5000 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5001 | argvlist = PyMem_NEW(char *, argc+1); |
| 5002 | if (argvlist == NULL) { |
| 5003 | Py_DECREF(opath); |
| 5004 | return PyErr_NoMemory(); |
| 5005 | } |
| 5006 | for (i = 0; i < argc; i++) { |
| 5007 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5008 | &argvlist[i])) { |
| 5009 | free_string_array(argvlist, i); |
| 5010 | PyErr_SetString( |
| 5011 | PyExc_TypeError, |
| 5012 | "spawnv() arg 2 must contain only strings"); |
| 5013 | Py_DECREF(opath); |
| 5014 | return NULL; |
| 5015 | } |
| 5016 | } |
| 5017 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5018 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5019 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5020 | Py_BEGIN_ALLOW_THREADS |
| 5021 | spawnval = spawnv(mode, path, argvlist); |
| 5022 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5023 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5024 | if (mode == _OLD_P_OVERLAY) |
| 5025 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5026 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5027 | Py_BEGIN_ALLOW_THREADS |
| 5028 | spawnval = _spawnv(mode, path, argvlist); |
| 5029 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5030 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5032 | free_string_array(argvlist, argc); |
| 5033 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5034 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5035 | if (spawnval == -1) |
| 5036 | return posix_error(); |
| 5037 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 5038 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5039 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5040 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5041 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5042 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5046 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5047 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5048 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5049 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5050 | mode: mode of process creation\n\ |
| 5051 | path: path of executable file\n\ |
| 5052 | args: tuple or list of arguments\n\ |
| 5053 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5054 | |
| 5055 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5056 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5057 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5058 | PyObject *opath; |
| 5059 | char *path; |
| 5060 | PyObject *argv, *env; |
| 5061 | char **argvlist; |
| 5062 | char **envlist; |
| 5063 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5064 | int mode; |
| 5065 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5066 | Py_intptr_t spawnval; |
| 5067 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5068 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5069 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5070 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5071 | argv is a list or tuple of strings and env is a dictionary |
| 5072 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5073 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5074 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 5075 | PyUnicode_FSConverter, |
| 5076 | &opath, &argv, &env)) |
| 5077 | return NULL; |
| 5078 | path = PyBytes_AsString(opath); |
| 5079 | if (PyList_Check(argv)) { |
| 5080 | argc = PyList_Size(argv); |
| 5081 | getitem = PyList_GetItem; |
| 5082 | } |
| 5083 | else if (PyTuple_Check(argv)) { |
| 5084 | argc = PyTuple_Size(argv); |
| 5085 | getitem = PyTuple_GetItem; |
| 5086 | } |
| 5087 | else { |
| 5088 | PyErr_SetString(PyExc_TypeError, |
| 5089 | "spawnve() arg 2 must be a tuple or list"); |
| 5090 | goto fail_0; |
| 5091 | } |
| 5092 | if (!PyMapping_Check(env)) { |
| 5093 | PyErr_SetString(PyExc_TypeError, |
| 5094 | "spawnve() arg 3 must be a mapping object"); |
| 5095 | goto fail_0; |
| 5096 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5098 | argvlist = PyMem_NEW(char *, argc+1); |
| 5099 | if (argvlist == NULL) { |
| 5100 | PyErr_NoMemory(); |
| 5101 | goto fail_0; |
| 5102 | } |
| 5103 | for (i = 0; i < argc; i++) { |
| 5104 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5105 | &argvlist[i])) |
| 5106 | { |
| 5107 | lastarg = i; |
| 5108 | goto fail_1; |
| 5109 | } |
| 5110 | } |
| 5111 | lastarg = argc; |
| 5112 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5114 | envlist = parse_envlist(env, &envc); |
| 5115 | if (envlist == NULL) |
| 5116 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5117 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5118 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5119 | Py_BEGIN_ALLOW_THREADS |
| 5120 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 5121 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5122 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5123 | if (mode == _OLD_P_OVERLAY) |
| 5124 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5125 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5126 | Py_BEGIN_ALLOW_THREADS |
| 5127 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 5128 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5129 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 5130 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5131 | if (spawnval == -1) |
| 5132 | (void) posix_error(); |
| 5133 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 5134 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5135 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5136 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5137 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5138 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5139 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5140 | while (--envc >= 0) |
| 5141 | PyMem_DEL(envlist[envc]); |
| 5142 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 5143 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5144 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5145 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5146 | Py_DECREF(opath); |
| 5147 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5148 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5149 | |
| 5150 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 5151 | #if defined(PYOS_OS2) |
| 5152 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 5153 | "spawnvp(mode, file, args)\n\n\ |
| 5154 | Execute the program 'file' in a new process, using the environment\n\ |
| 5155 | search path to find the file.\n\ |
| 5156 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5157 | mode: mode of process creation\n\ |
| 5158 | file: executable file name\n\ |
| 5159 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5160 | |
| 5161 | static PyObject * |
| 5162 | posix_spawnvp(PyObject *self, PyObject *args) |
| 5163 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5164 | PyObject *opath; |
| 5165 | char *path; |
| 5166 | PyObject *argv; |
| 5167 | char **argvlist; |
| 5168 | int mode, i, argc; |
| 5169 | Py_intptr_t spawnval; |
| 5170 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5171 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5172 | /* spawnvp has three arguments: (mode, path, argv), where |
| 5173 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5174 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5175 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 5176 | PyUnicode_FSConverter, |
| 5177 | &opath, &argv)) |
| 5178 | return NULL; |
| 5179 | path = PyBytes_AsString(opath); |
| 5180 | if (PyList_Check(argv)) { |
| 5181 | argc = PyList_Size(argv); |
| 5182 | getitem = PyList_GetItem; |
| 5183 | } |
| 5184 | else if (PyTuple_Check(argv)) { |
| 5185 | argc = PyTuple_Size(argv); |
| 5186 | getitem = PyTuple_GetItem; |
| 5187 | } |
| 5188 | else { |
| 5189 | PyErr_SetString(PyExc_TypeError, |
| 5190 | "spawnvp() arg 2 must be a tuple or list"); |
| 5191 | Py_DECREF(opath); |
| 5192 | return NULL; |
| 5193 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5194 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5195 | argvlist = PyMem_NEW(char *, argc+1); |
| 5196 | if (argvlist == NULL) { |
| 5197 | Py_DECREF(opath); |
| 5198 | return PyErr_NoMemory(); |
| 5199 | } |
| 5200 | for (i = 0; i < argc; i++) { |
| 5201 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5202 | &argvlist[i])) { |
| 5203 | free_string_array(argvlist, i); |
| 5204 | PyErr_SetString( |
| 5205 | PyExc_TypeError, |
| 5206 | "spawnvp() arg 2 must contain only strings"); |
| 5207 | Py_DECREF(opath); |
| 5208 | return NULL; |
| 5209 | } |
| 5210 | } |
| 5211 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5212 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5213 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5214 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5215 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5216 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5217 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5218 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5219 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5221 | free_string_array(argvlist, argc); |
| 5222 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5223 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5224 | if (spawnval == -1) |
| 5225 | return posix_error(); |
| 5226 | else |
| 5227 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | |
| 5231 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 5232 | "spawnvpe(mode, file, args, env)\n\n\ |
| 5233 | Execute the program 'file' in a new process, using the environment\n\ |
| 5234 | search path to find the file.\n\ |
| 5235 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5236 | mode: mode of process creation\n\ |
| 5237 | file: executable file name\n\ |
| 5238 | args: tuple or list of arguments\n\ |
| 5239 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5240 | |
| 5241 | static PyObject * |
| 5242 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 5243 | { |
Ross Lagerwall | dcfde5a | 2011-11-04 07:09:14 +0200 | [diff] [blame] | 5244 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5245 | char *path; |
| 5246 | PyObject *argv, *env; |
| 5247 | char **argvlist; |
| 5248 | char **envlist; |
| 5249 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5250 | int mode; |
| 5251 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5252 | Py_intptr_t spawnval; |
| 5253 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 5254 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5255 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5256 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 5257 | argv is a list or tuple of strings and env is a dictionary |
| 5258 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5259 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5260 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 5261 | PyUnicode_FSConverter, |
| 5262 | &opath, &argv, &env)) |
| 5263 | return NULL; |
| 5264 | path = PyBytes_AsString(opath); |
| 5265 | if (PyList_Check(argv)) { |
| 5266 | argc = PyList_Size(argv); |
| 5267 | getitem = PyList_GetItem; |
| 5268 | } |
| 5269 | else if (PyTuple_Check(argv)) { |
| 5270 | argc = PyTuple_Size(argv); |
| 5271 | getitem = PyTuple_GetItem; |
| 5272 | } |
| 5273 | else { |
| 5274 | PyErr_SetString(PyExc_TypeError, |
| 5275 | "spawnvpe() arg 2 must be a tuple or list"); |
| 5276 | goto fail_0; |
| 5277 | } |
| 5278 | if (!PyMapping_Check(env)) { |
| 5279 | PyErr_SetString(PyExc_TypeError, |
| 5280 | "spawnvpe() arg 3 must be a mapping object"); |
| 5281 | goto fail_0; |
| 5282 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5283 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5284 | argvlist = PyMem_NEW(char *, argc+1); |
| 5285 | if (argvlist == NULL) { |
| 5286 | PyErr_NoMemory(); |
| 5287 | goto fail_0; |
| 5288 | } |
| 5289 | for (i = 0; i < argc; i++) { |
| 5290 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5291 | &argvlist[i])) |
| 5292 | { |
| 5293 | lastarg = i; |
| 5294 | goto fail_1; |
| 5295 | } |
| 5296 | } |
| 5297 | lastarg = argc; |
| 5298 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5299 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5300 | envlist = parse_envlist(env, &envc); |
| 5301 | if (envlist == NULL) |
| 5302 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5303 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5304 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5305 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5306 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5307 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5308 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5309 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5310 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5311 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5312 | if (spawnval == -1) |
| 5313 | (void) posix_error(); |
| 5314 | else |
| 5315 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5316 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5317 | while (--envc >= 0) |
| 5318 | PyMem_DEL(envlist[envc]); |
| 5319 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5320 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5321 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5322 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5323 | Py_DECREF(opath); |
| 5324 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 5325 | } |
| 5326 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5327 | #endif /* HAVE_SPAWNV */ |
| 5328 | |
| 5329 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5330 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5331 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5332 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5333 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 5334 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5335 | 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] | 5336 | |
| 5337 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5338 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5339 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5340 | pid_t pid; |
| 5341 | int result = 0; |
| 5342 | _PyImport_AcquireLock(); |
| 5343 | pid = fork1(); |
| 5344 | if (pid == 0) { |
| 5345 | /* child: this clobbers and resets the import lock. */ |
| 5346 | PyOS_AfterFork(); |
| 5347 | } else { |
| 5348 | /* parent: release the import lock. */ |
| 5349 | result = _PyImport_ReleaseLock(); |
| 5350 | } |
| 5351 | if (pid == -1) |
| 5352 | return posix_error(); |
| 5353 | if (result < 0) { |
| 5354 | /* Don't clobber the OSError if the fork failed. */ |
| 5355 | PyErr_SetString(PyExc_RuntimeError, |
| 5356 | "not holding the import lock"); |
| 5357 | return NULL; |
| 5358 | } |
| 5359 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5360 | } |
| 5361 | #endif |
| 5362 | |
| 5363 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5364 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5365 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5366 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5367 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5368 | 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] | 5369 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5370 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5371 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5372 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5373 | pid_t pid; |
| 5374 | int result = 0; |
| 5375 | _PyImport_AcquireLock(); |
| 5376 | pid = fork(); |
| 5377 | if (pid == 0) { |
| 5378 | /* child: this clobbers and resets the import lock. */ |
| 5379 | PyOS_AfterFork(); |
| 5380 | } else { |
| 5381 | /* parent: release the import lock. */ |
| 5382 | result = _PyImport_ReleaseLock(); |
| 5383 | } |
| 5384 | if (pid == -1) |
| 5385 | return posix_error(); |
| 5386 | if (result < 0) { |
| 5387 | /* Don't clobber the OSError if the fork failed. */ |
| 5388 | PyErr_SetString(PyExc_RuntimeError, |
| 5389 | "not holding the import lock"); |
| 5390 | return NULL; |
| 5391 | } |
| 5392 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5393 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5394 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5395 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5396 | #ifdef HAVE_SCHED_H |
| 5397 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5398 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5399 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5400 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5401 | "sched_get_priority_max(policy)\n\n\ |
| 5402 | Get the maximum scheduling priority for *policy*."); |
| 5403 | |
| 5404 | static PyObject * |
| 5405 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5406 | { |
| 5407 | int policy, max; |
| 5408 | |
| 5409 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5410 | return NULL; |
| 5411 | max = sched_get_priority_max(policy); |
| 5412 | if (max < 0) |
| 5413 | return posix_error(); |
| 5414 | return PyLong_FromLong(max); |
| 5415 | } |
| 5416 | |
| 5417 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5418 | "sched_get_priority_min(policy)\n\n\ |
| 5419 | Get the minimum scheduling priority for *policy*."); |
| 5420 | |
| 5421 | static PyObject * |
| 5422 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5423 | { |
| 5424 | int policy, min; |
| 5425 | |
| 5426 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5427 | return NULL; |
| 5428 | min = sched_get_priority_min(policy); |
| 5429 | if (min < 0) |
| 5430 | return posix_error(); |
| 5431 | return PyLong_FromLong(min); |
| 5432 | } |
| 5433 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5434 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5435 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5436 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5437 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5438 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5439 | "sched_getscheduler(pid)\n\n\ |
| 5440 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5441 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5442 | |
| 5443 | static PyObject * |
| 5444 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5445 | { |
| 5446 | pid_t pid; |
| 5447 | int policy; |
| 5448 | |
| 5449 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5450 | return NULL; |
| 5451 | policy = sched_getscheduler(pid); |
| 5452 | if (policy < 0) |
| 5453 | return posix_error(); |
| 5454 | return PyLong_FromLong(policy); |
| 5455 | } |
| 5456 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5457 | #endif |
| 5458 | |
| 5459 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5460 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5461 | static PyObject * |
| 5462 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5463 | { |
| 5464 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5465 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5466 | |
| 5467 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5468 | return NULL; |
| 5469 | res = PyStructSequence_New(type); |
| 5470 | if (!res) |
| 5471 | return NULL; |
| 5472 | Py_INCREF(priority); |
| 5473 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5474 | return res; |
| 5475 | } |
| 5476 | |
| 5477 | PyDoc_STRVAR(sched_param__doc__, |
| 5478 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5479 | Current has only one field: sched_priority"); |
| 5480 | |
| 5481 | static PyStructSequence_Field sched_param_fields[] = { |
| 5482 | {"sched_priority", "the scheduling priority"}, |
| 5483 | {0} |
| 5484 | }; |
| 5485 | |
| 5486 | static PyStructSequence_Desc sched_param_desc = { |
| 5487 | "sched_param", /* name */ |
| 5488 | sched_param__doc__, /* doc */ |
| 5489 | sched_param_fields, |
| 5490 | 1 |
| 5491 | }; |
| 5492 | |
| 5493 | static int |
| 5494 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5495 | { |
| 5496 | long priority; |
| 5497 | |
| 5498 | if (Py_TYPE(param) != &SchedParamType) { |
| 5499 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5500 | return 0; |
| 5501 | } |
| 5502 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5503 | if (priority == -1 && PyErr_Occurred()) |
| 5504 | return 0; |
| 5505 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5506 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5507 | return 0; |
| 5508 | } |
| 5509 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5510 | return 1; |
| 5511 | } |
| 5512 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5513 | #endif |
| 5514 | |
| 5515 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5516 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5517 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5518 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5519 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5520 | If *pid* is 0, the calling process is changed.\n\ |
| 5521 | *param* is an instance of sched_param."); |
| 5522 | |
| 5523 | static PyObject * |
| 5524 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5525 | { |
| 5526 | pid_t pid; |
| 5527 | int policy; |
| 5528 | struct sched_param param; |
| 5529 | |
| 5530 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5531 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5532 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5533 | |
| 5534 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5535 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5536 | ** scheduling policy under Solaris/Illumos, and others. |
| 5537 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5538 | */ |
| 5539 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5540 | return posix_error(); |
| 5541 | Py_RETURN_NONE; |
| 5542 | } |
| 5543 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5544 | #endif |
| 5545 | |
| 5546 | #ifdef HAVE_SCHED_SETPARAM |
| 5547 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5548 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5549 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5550 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5551 | sched_param class. A PID of 0 means the calling process."); |
| 5552 | |
| 5553 | static PyObject * |
| 5554 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5555 | { |
| 5556 | pid_t pid; |
| 5557 | struct sched_param param; |
| 5558 | PyObject *res, *priority; |
| 5559 | |
| 5560 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5561 | return NULL; |
| 5562 | if (sched_getparam(pid, ¶m)) |
| 5563 | return posix_error(); |
| 5564 | res = PyStructSequence_New(&SchedParamType); |
| 5565 | if (!res) |
| 5566 | return NULL; |
| 5567 | priority = PyLong_FromLong(param.sched_priority); |
| 5568 | if (!priority) { |
| 5569 | Py_DECREF(res); |
| 5570 | return NULL; |
| 5571 | } |
| 5572 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5573 | return res; |
| 5574 | } |
| 5575 | |
| 5576 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5577 | "sched_setparam(pid, param)\n\n\ |
| 5578 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5579 | A PID of 0 means the calling process."); |
| 5580 | |
| 5581 | static PyObject * |
| 5582 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5583 | { |
| 5584 | pid_t pid; |
| 5585 | struct sched_param param; |
| 5586 | |
| 5587 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5588 | &pid, &convert_sched_param, ¶m)) |
| 5589 | return NULL; |
| 5590 | if (sched_setparam(pid, ¶m)) |
| 5591 | return posix_error(); |
| 5592 | Py_RETURN_NONE; |
| 5593 | } |
| 5594 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5595 | #endif |
| 5596 | |
| 5597 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5598 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5599 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5600 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5601 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5602 | |
| 5603 | static PyObject * |
| 5604 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5605 | { |
| 5606 | pid_t pid; |
| 5607 | struct timespec interval; |
| 5608 | |
| 5609 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5610 | return NULL; |
| 5611 | if (sched_rr_get_interval(pid, &interval)) |
| 5612 | return posix_error(); |
| 5613 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5614 | } |
| 5615 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5616 | #endif |
| 5617 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5618 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5619 | "sched_yield()\n\n\ |
| 5620 | Voluntarily relinquish the CPU."); |
| 5621 | |
| 5622 | static PyObject * |
| 5623 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5624 | { |
| 5625 | if (sched_yield()) |
| 5626 | return posix_error(); |
| 5627 | Py_RETURN_NONE; |
| 5628 | } |
| 5629 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5630 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5631 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5632 | typedef struct { |
| 5633 | PyObject_HEAD; |
| 5634 | Py_ssize_t size; |
| 5635 | int ncpus; |
| 5636 | cpu_set_t *set; |
| 5637 | } Py_cpu_set; |
| 5638 | |
| 5639 | static PyTypeObject cpu_set_type; |
| 5640 | |
| 5641 | static void |
| 5642 | cpu_set_dealloc(Py_cpu_set *set) |
| 5643 | { |
| 5644 | assert(set->set); |
| 5645 | CPU_FREE(set->set); |
| 5646 | Py_TYPE(set)->tp_free(set); |
| 5647 | } |
| 5648 | |
| 5649 | static Py_cpu_set * |
| 5650 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 5651 | { |
| 5652 | Py_cpu_set *set; |
| 5653 | |
| 5654 | if (size < 0) { |
| 5655 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 5656 | return NULL; |
| 5657 | } |
| 5658 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 5659 | if (!set) |
| 5660 | return NULL; |
| 5661 | set->ncpus = size; |
| 5662 | set->size = CPU_ALLOC_SIZE(size); |
| 5663 | set->set = CPU_ALLOC(size); |
| 5664 | if (!set->set) { |
| 5665 | type->tp_free(set); |
| 5666 | PyErr_NoMemory(); |
| 5667 | return NULL; |
| 5668 | } |
| 5669 | CPU_ZERO_S(set->size, set->set); |
| 5670 | return set; |
| 5671 | } |
| 5672 | |
| 5673 | static PyObject * |
| 5674 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5675 | { |
| 5676 | int size; |
| 5677 | |
| 5678 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 5679 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 5680 | return NULL; |
| 5681 | return (PyObject *)make_new_cpu_set(type, size); |
| 5682 | } |
| 5683 | |
| 5684 | static PyObject * |
| 5685 | cpu_set_repr(Py_cpu_set *set) |
| 5686 | { |
| 5687 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 5688 | } |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5689 | |
| 5690 | static Py_ssize_t |
| 5691 | cpu_set_len(Py_cpu_set *set) |
| 5692 | { |
| 5693 | return set->ncpus; |
| 5694 | } |
| 5695 | |
| 5696 | static int |
| 5697 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 5698 | { |
| 5699 | int cpu; |
| 5700 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 5701 | return -1; |
| 5702 | if (cpu < 0) { |
| 5703 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 5704 | return -1; |
| 5705 | } |
| 5706 | if (cpu >= set->ncpus) { |
| 5707 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 5708 | return -1; |
| 5709 | } |
| 5710 | return cpu; |
| 5711 | } |
| 5712 | |
| 5713 | PyDoc_STRVAR(cpu_set_set_doc, |
| 5714 | "cpu_set.set(i)\n\n\ |
| 5715 | Add CPU *i* to the set."); |
| 5716 | |
| 5717 | static PyObject * |
| 5718 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 5719 | { |
| 5720 | int cpu = _get_cpu(set, "i|set", args); |
| 5721 | if (cpu == -1) |
| 5722 | return NULL; |
| 5723 | CPU_SET_S(cpu, set->size, set->set); |
| 5724 | Py_RETURN_NONE; |
| 5725 | } |
| 5726 | |
| 5727 | PyDoc_STRVAR(cpu_set_count_doc, |
| 5728 | "cpu_set.count() -> int\n\n\ |
| 5729 | Return the number of CPUs active in the set."); |
| 5730 | |
| 5731 | static PyObject * |
| 5732 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 5733 | { |
| 5734 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 5735 | } |
| 5736 | |
| 5737 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 5738 | "cpu_set.clear(i)\n\n\ |
| 5739 | Remove CPU *i* from the set."); |
| 5740 | |
| 5741 | static PyObject * |
| 5742 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 5743 | { |
| 5744 | int cpu = _get_cpu(set, "i|clear", args); |
| 5745 | if (cpu == -1) |
| 5746 | return NULL; |
| 5747 | CPU_CLR_S(cpu, set->size, set->set); |
| 5748 | Py_RETURN_NONE; |
| 5749 | } |
| 5750 | |
| 5751 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 5752 | "cpu_set.isset(i) -> bool\n\n\ |
| 5753 | Test if CPU *i* is in the set."); |
| 5754 | |
| 5755 | static PyObject * |
| 5756 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 5757 | { |
| 5758 | int cpu = _get_cpu(set, "i|isset", args); |
| 5759 | if (cpu == -1) |
| 5760 | return NULL; |
| 5761 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 5762 | Py_RETURN_TRUE; |
| 5763 | Py_RETURN_FALSE; |
| 5764 | } |
| 5765 | |
| 5766 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 5767 | "cpu_set.zero()\n\n\ |
| 5768 | Clear the cpu_set."); |
| 5769 | |
| 5770 | static PyObject * |
| 5771 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 5772 | { |
| 5773 | CPU_ZERO_S(set->size, set->set); |
| 5774 | Py_RETURN_NONE; |
| 5775 | } |
| 5776 | |
| 5777 | static PyObject * |
| 5778 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 5779 | { |
| 5780 | int eq; |
| 5781 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5782 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) |
| 5783 | Py_RETURN_NOTIMPLEMENTED; |
| 5784 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5785 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 5786 | if ((op == Py_EQ) ? eq : !eq) |
| 5787 | Py_RETURN_TRUE; |
| 5788 | else |
| 5789 | Py_RETURN_FALSE; |
| 5790 | } |
| 5791 | |
| 5792 | #define CPU_SET_BINOP(name, op) \ |
| 5793 | static PyObject * \ |
| 5794 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 5795 | if (res) { \ |
| 5796 | Py_INCREF(res); \ |
| 5797 | } \ |
| 5798 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 5799 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5800 | if (!res) \ |
| 5801 | return NULL; \ |
| 5802 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 5803 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5804 | Py_DECREF(res); \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5805 | Py_RETURN_NOTIMPLEMENTED; \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5806 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 5807 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5808 | op(res->size, res->set, left->set, right->set); \ |
| 5809 | return (PyObject *)res; \ |
| 5810 | } \ |
| 5811 | static PyObject * \ |
| 5812 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5813 | return do_cpu_set_##name(left, right, NULL); \ |
| 5814 | } \ |
| 5815 | static PyObject * \ |
| 5816 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5817 | return do_cpu_set_##name(left, right, left); \ |
| 5818 | } \ |
| 5819 | |
| 5820 | CPU_SET_BINOP(and, CPU_AND_S) |
| 5821 | CPU_SET_BINOP(or, CPU_OR_S) |
| 5822 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 5823 | #undef CPU_SET_BINOP |
| 5824 | |
| 5825 | PyDoc_STRVAR(cpu_set_doc, |
| 5826 | "cpu_set(size)\n\n\ |
| 5827 | Create an empty mask of CPUs."); |
| 5828 | |
| 5829 | static PyNumberMethods cpu_set_as_number = { |
| 5830 | 0, /*nb_add*/ |
| 5831 | 0, /*nb_subtract*/ |
| 5832 | 0, /*nb_multiply*/ |
| 5833 | 0, /*nb_remainder*/ |
| 5834 | 0, /*nb_divmod*/ |
| 5835 | 0, /*nb_power*/ |
| 5836 | 0, /*nb_negative*/ |
| 5837 | 0, /*nb_positive*/ |
| 5838 | 0, /*nb_absolute*/ |
| 5839 | 0, /*nb_bool*/ |
| 5840 | 0, /*nb_invert*/ |
| 5841 | 0, /*nb_lshift*/ |
| 5842 | 0, /*nb_rshift*/ |
| 5843 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 5844 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 5845 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 5846 | 0, /*nb_int*/ |
| 5847 | 0, /*nb_reserved*/ |
| 5848 | 0, /*nb_float*/ |
| 5849 | 0, /*nb_inplace_add*/ |
| 5850 | 0, /*nb_inplace_subtract*/ |
| 5851 | 0, /*nb_inplace_multiply*/ |
| 5852 | 0, /*nb_inplace_remainder*/ |
| 5853 | 0, /*nb_inplace_power*/ |
| 5854 | 0, /*nb_inplace_lshift*/ |
| 5855 | 0, /*nb_inplace_rshift*/ |
| 5856 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5857 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5858 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5859 | }; |
| 5860 | |
| 5861 | static PySequenceMethods cpu_set_as_sequence = { |
| 5862 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5863 | }; |
| 5864 | |
| 5865 | static PyMethodDef cpu_set_methods[] = { |
| 5866 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5867 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5868 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5869 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5870 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5871 | {NULL, NULL} /* sentinel */ |
| 5872 | }; |
| 5873 | |
| 5874 | static PyTypeObject cpu_set_type = { |
| 5875 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5876 | "posix.cpu_set", /* tp_name */ |
| 5877 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5878 | 0, /* tp_itemsize */ |
| 5879 | /* methods */ |
| 5880 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5881 | 0, /* tp_print */ |
| 5882 | 0, /* tp_getattr */ |
| 5883 | 0, /* tp_setattr */ |
| 5884 | 0, /* tp_reserved */ |
| 5885 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5886 | &cpu_set_as_number, /* tp_as_number */ |
| 5887 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5888 | 0, /* tp_as_mapping */ |
| 5889 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5890 | 0, /* tp_call */ |
| 5891 | 0, /* tp_str */ |
| 5892 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5893 | 0, /* tp_setattro */ |
| 5894 | 0, /* tp_as_buffer */ |
| 5895 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5896 | cpu_set_doc, /* tp_doc */ |
| 5897 | 0, /* tp_traverse */ |
| 5898 | 0, /* tp_clear */ |
| 5899 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5900 | 0, /* tp_weaklistoffset */ |
| 5901 | 0, /* tp_iter */ |
| 5902 | 0, /* tp_iternext */ |
| 5903 | cpu_set_methods, /* tp_methods */ |
| 5904 | 0, /* tp_members */ |
| 5905 | 0, /* tp_getset */ |
| 5906 | 0, /* tp_base */ |
| 5907 | 0, /* tp_dict */ |
| 5908 | 0, /* tp_descr_get */ |
| 5909 | 0, /* tp_descr_set */ |
| 5910 | 0, /* tp_dictoffset */ |
| 5911 | 0, /* tp_init */ |
| 5912 | PyType_GenericAlloc, /* tp_alloc */ |
| 5913 | cpu_set_new, /* tp_new */ |
| 5914 | PyObject_Del, /* tp_free */ |
| 5915 | }; |
| 5916 | |
| 5917 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5918 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5919 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5920 | |
| 5921 | static PyObject * |
| 5922 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5923 | { |
| 5924 | pid_t pid; |
| 5925 | Py_cpu_set *cpu_set; |
| 5926 | |
Benjamin Peterson | a17a5d6 | 2011-08-09 16:49:13 -0500 | [diff] [blame] | 5927 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5928 | &pid, &cpu_set_type, &cpu_set)) |
| 5929 | return NULL; |
| 5930 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5931 | return posix_error(); |
| 5932 | Py_RETURN_NONE; |
| 5933 | } |
| 5934 | |
| 5935 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5936 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5937 | Return the affinity of the process with PID *pid*.\n\ |
| 5938 | The returned cpu_set will be of size *ncpus*."); |
| 5939 | |
| 5940 | static PyObject * |
| 5941 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5942 | { |
| 5943 | pid_t pid; |
| 5944 | int ncpus; |
| 5945 | Py_cpu_set *res; |
| 5946 | |
Benjamin Peterson | 7ac9214 | 2011-08-03 08:54:26 -0500 | [diff] [blame] | 5947 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5948 | &pid, &ncpus)) |
| 5949 | return NULL; |
| 5950 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5951 | if (!res) |
| 5952 | return NULL; |
| 5953 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5954 | Py_DECREF(res); |
| 5955 | return posix_error(); |
| 5956 | } |
| 5957 | return (PyObject *)res; |
| 5958 | } |
| 5959 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5960 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5961 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5962 | #endif /* HAVE_SCHED_H */ |
| 5963 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5964 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5965 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5966 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5967 | #define DEV_PTY_FILE "/dev/ptc" |
| 5968 | #define HAVE_DEV_PTMX |
| 5969 | #else |
| 5970 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5971 | #endif |
| 5972 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5973 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5974 | #ifdef HAVE_PTY_H |
| 5975 | #include <pty.h> |
| 5976 | #else |
| 5977 | #ifdef HAVE_LIBUTIL_H |
| 5978 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5979 | #else |
| 5980 | #ifdef HAVE_UTIL_H |
| 5981 | #include <util.h> |
| 5982 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5983 | #endif /* HAVE_LIBUTIL_H */ |
| 5984 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5985 | #ifdef HAVE_STROPTS_H |
| 5986 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5987 | #endif |
| 5988 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5989 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5990 | #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] | 5991 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5992 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5993 | 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] | 5994 | |
| 5995 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5996 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5997 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5998 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5999 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6000 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6001 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6002 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6003 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6004 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6005 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6006 | #endif |
| 6007 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6008 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6009 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6010 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 6011 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6012 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6013 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6014 | if (slave_name == NULL) |
| 6015 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6016 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6017 | slave_fd = open(slave_name, O_RDWR); |
| 6018 | if (slave_fd < 0) |
| 6019 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6020 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6021 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 6022 | if (master_fd < 0) |
| 6023 | return posix_error(); |
| 6024 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 6025 | /* change permission of slave */ |
| 6026 | if (grantpt(master_fd) < 0) { |
| 6027 | PyOS_setsig(SIGCHLD, sig_saved); |
| 6028 | return posix_error(); |
| 6029 | } |
| 6030 | /* unlock slave */ |
| 6031 | if (unlockpt(master_fd) < 0) { |
| 6032 | PyOS_setsig(SIGCHLD, sig_saved); |
| 6033 | return posix_error(); |
| 6034 | } |
| 6035 | PyOS_setsig(SIGCHLD, sig_saved); |
| 6036 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6037 | if (slave_name == NULL) |
| 6038 | return posix_error(); |
| 6039 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 6040 | if (slave_fd < 0) |
| 6041 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6042 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6043 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6044 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6045 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6046 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6047 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6048 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6049 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6051 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6052 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6053 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6054 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6055 | |
| 6056 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6057 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6058 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6059 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 6060 | 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] | 6061 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6062 | |
| 6063 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6064 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6065 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6066 | int master_fd = -1, result = 0; |
| 6067 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6068 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6069 | _PyImport_AcquireLock(); |
| 6070 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6071 | if (pid == 0) { |
| 6072 | /* child: this clobbers and resets the import lock. */ |
| 6073 | PyOS_AfterFork(); |
| 6074 | } else { |
| 6075 | /* parent: release the import lock. */ |
| 6076 | result = _PyImport_ReleaseLock(); |
| 6077 | } |
| 6078 | if (pid == -1) |
| 6079 | return posix_error(); |
| 6080 | if (result < 0) { |
| 6081 | /* Don't clobber the OSError if the fork failed. */ |
| 6082 | PyErr_SetString(PyExc_RuntimeError, |
| 6083 | "not holding the import lock"); |
| 6084 | return NULL; |
| 6085 | } |
| 6086 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6087 | } |
| 6088 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6089 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6090 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6091 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6092 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6093 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6094 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6095 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6096 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6097 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6098 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6099 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6100 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6101 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6102 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6103 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6104 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6105 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6106 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6107 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6109 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6110 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6111 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6112 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6113 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6114 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6115 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6116 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6117 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6118 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6119 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6120 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6121 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6122 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6123 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6124 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6125 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6126 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6127 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6128 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6129 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6130 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6131 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6132 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6133 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6134 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6135 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6136 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6137 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6138 | } |
| 6139 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6140 | #ifdef HAVE_GETGROUPLIST |
| 6141 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6142 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6143 | Returns a list of groups to which a user belongs.\n\n\ |
| 6144 | user: username to lookup\n\ |
| 6145 | group: base group id of the user"); |
| 6146 | |
| 6147 | static PyObject * |
| 6148 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6149 | { |
| 6150 | #ifdef NGROUPS_MAX |
| 6151 | #define MAX_GROUPS NGROUPS_MAX |
| 6152 | #else |
| 6153 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6154 | #define MAX_GROUPS 64 |
| 6155 | #endif |
| 6156 | |
| 6157 | const char *user; |
| 6158 | int i, ngroups; |
| 6159 | PyObject *list; |
| 6160 | #ifdef __APPLE__ |
| 6161 | int *groups, basegid; |
| 6162 | #else |
| 6163 | gid_t *groups, basegid; |
| 6164 | #endif |
| 6165 | ngroups = MAX_GROUPS; |
| 6166 | |
| 6167 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 6168 | return NULL; |
| 6169 | |
| 6170 | #ifdef __APPLE__ |
| 6171 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 6172 | #else |
| 6173 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 6174 | #endif |
| 6175 | if (groups == NULL) |
| 6176 | return PyErr_NoMemory(); |
| 6177 | |
| 6178 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6179 | PyMem_Del(groups); |
| 6180 | return posix_error(); |
| 6181 | } |
| 6182 | |
| 6183 | list = PyList_New(ngroups); |
| 6184 | if (list == NULL) { |
| 6185 | PyMem_Del(groups); |
| 6186 | return NULL; |
| 6187 | } |
| 6188 | |
| 6189 | for (i = 0; i < ngroups; i++) { |
| 6190 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 6191 | if (o == NULL) { |
| 6192 | Py_DECREF(list); |
| 6193 | PyMem_Del(groups); |
| 6194 | return NULL; |
| 6195 | } |
| 6196 | PyList_SET_ITEM(list, i, o); |
| 6197 | } |
| 6198 | |
| 6199 | PyMem_Del(groups); |
| 6200 | |
| 6201 | return list; |
| 6202 | } |
| 6203 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6204 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6205 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6206 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6207 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6208 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6209 | |
| 6210 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6211 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6212 | { |
| 6213 | PyObject *result = NULL; |
| 6214 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6215 | #ifdef NGROUPS_MAX |
| 6216 | #define MAX_GROUPS NGROUPS_MAX |
| 6217 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6218 | /* 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] | 6219 | #define MAX_GROUPS 64 |
| 6220 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6221 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6222 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6223 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6224 | * This is a helper variable to store the intermediate result when |
| 6225 | * that happens. |
| 6226 | * |
| 6227 | * To keep the code readable the OSX behaviour is unconditional, |
| 6228 | * according to the POSIX spec this should be safe on all unix-y |
| 6229 | * systems. |
| 6230 | */ |
| 6231 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6232 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6233 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6234 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6235 | if (n < 0) { |
| 6236 | if (errno == EINVAL) { |
| 6237 | n = getgroups(0, NULL); |
| 6238 | if (n == -1) { |
| 6239 | return posix_error(); |
| 6240 | } |
| 6241 | if (n == 0) { |
| 6242 | /* Avoid malloc(0) */ |
| 6243 | alt_grouplist = grouplist; |
| 6244 | } else { |
| 6245 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 6246 | if (alt_grouplist == NULL) { |
| 6247 | errno = EINVAL; |
| 6248 | return posix_error(); |
| 6249 | } |
| 6250 | n = getgroups(n, alt_grouplist); |
| 6251 | if (n == -1) { |
| 6252 | PyMem_Free(alt_grouplist); |
| 6253 | return posix_error(); |
| 6254 | } |
| 6255 | } |
| 6256 | } else { |
| 6257 | return posix_error(); |
| 6258 | } |
| 6259 | } |
| 6260 | result = PyList_New(n); |
| 6261 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6262 | int i; |
| 6263 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6264 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6265 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 6266 | Py_DECREF(result); |
| 6267 | result = NULL; |
| 6268 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6269 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6270 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6271 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | if (alt_grouplist != grouplist) { |
| 6275 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6276 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6277 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6278 | return result; |
| 6279 | } |
| 6280 | #endif |
| 6281 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6282 | #ifdef HAVE_INITGROUPS |
| 6283 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 6284 | "initgroups(username, gid) -> None\n\n\ |
| 6285 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 6286 | the groups of which the specified username is a member, plus the specified\n\ |
| 6287 | group id."); |
| 6288 | |
| 6289 | static PyObject * |
| 6290 | posix_initgroups(PyObject *self, PyObject *args) |
| 6291 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6292 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6293 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6294 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6295 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6296 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6297 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 6298 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6299 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6300 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6301 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 6302 | res = initgroups(username, (gid_t) gid); |
| 6303 | Py_DECREF(oname); |
| 6304 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6305 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6306 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6307 | Py_INCREF(Py_None); |
| 6308 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 6309 | } |
| 6310 | #endif |
| 6311 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6312 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6313 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6314 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 6315 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6316 | |
| 6317 | static PyObject * |
| 6318 | posix_getpgid(PyObject *self, PyObject *args) |
| 6319 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6320 | pid_t pid, pgid; |
| 6321 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 6322 | return NULL; |
| 6323 | pgid = getpgid(pid); |
| 6324 | if (pgid < 0) |
| 6325 | return posix_error(); |
| 6326 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6327 | } |
| 6328 | #endif /* HAVE_GETPGID */ |
| 6329 | |
| 6330 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6331 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6332 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6333 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6334 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6335 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6336 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6337 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6338 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6339 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6340 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6341 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6342 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6343 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6344 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6345 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6346 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6347 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6348 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6349 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6350 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 6351 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6352 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6353 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6354 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6355 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6356 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6357 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6358 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6359 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6360 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6361 | return posix_error(); |
| 6362 | Py_INCREF(Py_None); |
| 6363 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6364 | } |
| 6365 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6366 | #endif /* HAVE_SETPGRP */ |
| 6367 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6368 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6369 | |
| 6370 | #ifdef MS_WINDOWS |
| 6371 | #include <tlhelp32.h> |
| 6372 | |
| 6373 | static PyObject* |
| 6374 | win32_getppid() |
| 6375 | { |
| 6376 | HANDLE snapshot; |
| 6377 | pid_t mypid; |
| 6378 | PyObject* result = NULL; |
| 6379 | BOOL have_record; |
| 6380 | PROCESSENTRY32 pe; |
| 6381 | |
| 6382 | mypid = getpid(); /* This function never fails */ |
| 6383 | |
| 6384 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6385 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6386 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6387 | |
| 6388 | pe.dwSize = sizeof(pe); |
| 6389 | have_record = Process32First(snapshot, &pe); |
| 6390 | while (have_record) { |
| 6391 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6392 | /* We could cache the ulong value in a static variable. */ |
| 6393 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6394 | break; |
| 6395 | } |
| 6396 | |
| 6397 | have_record = Process32Next(snapshot, &pe); |
| 6398 | } |
| 6399 | |
| 6400 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6401 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6402 | * error anyway, so let's raise it. */ |
| 6403 | if (!result) |
| 6404 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6405 | |
| 6406 | CloseHandle(snapshot); |
| 6407 | |
| 6408 | return result; |
| 6409 | } |
| 6410 | #endif /*MS_WINDOWS*/ |
| 6411 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6412 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6413 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6414 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6415 | Windows machines will still return its id; others systems will return the id\n\ |
| 6416 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6417 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6418 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6419 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6420 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6421 | #ifdef MS_WINDOWS |
| 6422 | return win32_getppid(); |
| 6423 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6424 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6425 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6426 | } |
| 6427 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6428 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6429 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6430 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6431 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6432 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6433 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6434 | |
| 6435 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6436 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6437 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6438 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6439 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6440 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6441 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6442 | |
| 6443 | if (GetUserNameW(user_name, &num_chars)) { |
| 6444 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6445 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6446 | } |
| 6447 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6448 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6449 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6450 | char *name; |
| 6451 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6452 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6453 | errno = 0; |
| 6454 | name = getlogin(); |
| 6455 | if (name == NULL) { |
| 6456 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6457 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6458 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6459 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6460 | } |
| 6461 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6462 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6463 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6464 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6465 | return result; |
| 6466 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6467 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6468 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6469 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6470 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6471 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6472 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6473 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6474 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6475 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6476 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6477 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6478 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6479 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6480 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6481 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6482 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6483 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6484 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6485 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6486 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6487 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6488 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6489 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6490 | pid_t pid; |
| 6491 | int sig; |
| 6492 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6493 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6494 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6495 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 6496 | APIRET rc; |
| 6497 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6498 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6499 | |
| 6500 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 6501 | APIRET rc; |
| 6502 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6503 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6504 | |
| 6505 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 6506 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6507 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6508 | if (kill(pid, sig) == -1) |
| 6509 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6510 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6511 | Py_INCREF(Py_None); |
| 6512 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6513 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6514 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6515 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6516 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6517 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6518 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6519 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6520 | |
| 6521 | static PyObject * |
| 6522 | posix_killpg(PyObject *self, PyObject *args) |
| 6523 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6524 | int sig; |
| 6525 | pid_t pgid; |
| 6526 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6527 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6528 | take the same type. Moreover, pid_t is always at least as wide as |
| 6529 | int (else compilation of this module fails), which is safe. */ |
| 6530 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6531 | return NULL; |
| 6532 | if (killpg(pgid, sig) == -1) |
| 6533 | return posix_error(); |
| 6534 | Py_INCREF(Py_None); |
| 6535 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6536 | } |
| 6537 | #endif |
| 6538 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6539 | #ifdef MS_WINDOWS |
| 6540 | PyDoc_STRVAR(win32_kill__doc__, |
| 6541 | "kill(pid, sig)\n\n\ |
| 6542 | Kill a process with a signal."); |
| 6543 | |
| 6544 | static PyObject * |
| 6545 | win32_kill(PyObject *self, PyObject *args) |
| 6546 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6547 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6548 | DWORD pid, sig, err; |
| 6549 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6550 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6551 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 6552 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6553 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6554 | /* Console processes which share a common console can be sent CTRL+C or |
| 6555 | CTRL+BREAK events, provided they handle said events. */ |
| 6556 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 6557 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 6558 | err = GetLastError(); |
| 6559 | PyErr_SetFromWindowsErr(err); |
| 6560 | } |
| 6561 | else |
| 6562 | Py_RETURN_NONE; |
| 6563 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6564 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6565 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6566 | attempt to open and terminate the process. */ |
| 6567 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 6568 | if (handle == NULL) { |
| 6569 | err = GetLastError(); |
| 6570 | return PyErr_SetFromWindowsErr(err); |
| 6571 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6572 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6573 | if (TerminateProcess(handle, sig) == 0) { |
| 6574 | err = GetLastError(); |
| 6575 | result = PyErr_SetFromWindowsErr(err); |
| 6576 | } else { |
| 6577 | Py_INCREF(Py_None); |
| 6578 | result = Py_None; |
| 6579 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6580 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6581 | CloseHandle(handle); |
| 6582 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6583 | } |
| 6584 | #endif /* MS_WINDOWS */ |
| 6585 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6586 | #ifdef HAVE_PLOCK |
| 6587 | |
| 6588 | #ifdef HAVE_SYS_LOCK_H |
| 6589 | #include <sys/lock.h> |
| 6590 | #endif |
| 6591 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6592 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6593 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6594 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6595 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6596 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6597 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6598 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6599 | int op; |
| 6600 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6601 | return NULL; |
| 6602 | if (plock(op) == -1) |
| 6603 | return posix_error(); |
| 6604 | Py_INCREF(Py_None); |
| 6605 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6606 | } |
| 6607 | #endif |
| 6608 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6609 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6610 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6611 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6612 | Set the current process's user id."); |
| 6613 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6614 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6615 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6616 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6617 | long uid_arg; |
| 6618 | uid_t uid; |
| 6619 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 6620 | return NULL; |
| 6621 | uid = uid_arg; |
| 6622 | if (uid != uid_arg) { |
| 6623 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6624 | return NULL; |
| 6625 | } |
| 6626 | if (setuid(uid) < 0) |
| 6627 | return posix_error(); |
| 6628 | Py_INCREF(Py_None); |
| 6629 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6630 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6631 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6632 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6633 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6634 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6635 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6636 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6637 | Set the current process's effective user id."); |
| 6638 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6639 | static PyObject * |
| 6640 | posix_seteuid (PyObject *self, PyObject *args) |
| 6641 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6642 | long euid_arg; |
| 6643 | uid_t euid; |
| 6644 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 6645 | return NULL; |
| 6646 | euid = euid_arg; |
| 6647 | if (euid != euid_arg) { |
| 6648 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6649 | return NULL; |
| 6650 | } |
| 6651 | if (seteuid(euid) < 0) { |
| 6652 | return posix_error(); |
| 6653 | } else { |
| 6654 | Py_INCREF(Py_None); |
| 6655 | return Py_None; |
| 6656 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6657 | } |
| 6658 | #endif /* HAVE_SETEUID */ |
| 6659 | |
| 6660 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6661 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6662 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6663 | Set the current process's effective group id."); |
| 6664 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6665 | static PyObject * |
| 6666 | posix_setegid (PyObject *self, PyObject *args) |
| 6667 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6668 | long egid_arg; |
| 6669 | gid_t egid; |
| 6670 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 6671 | return NULL; |
| 6672 | egid = egid_arg; |
| 6673 | if (egid != egid_arg) { |
| 6674 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6675 | return NULL; |
| 6676 | } |
| 6677 | if (setegid(egid) < 0) { |
| 6678 | return posix_error(); |
| 6679 | } else { |
| 6680 | Py_INCREF(Py_None); |
| 6681 | return Py_None; |
| 6682 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6683 | } |
| 6684 | #endif /* HAVE_SETEGID */ |
| 6685 | |
| 6686 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6687 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6688 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6689 | Set the current process's real and effective user ids."); |
| 6690 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6691 | static PyObject * |
| 6692 | posix_setreuid (PyObject *self, PyObject *args) |
| 6693 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6694 | long ruid_arg, euid_arg; |
| 6695 | uid_t ruid, euid; |
| 6696 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 6697 | return NULL; |
| 6698 | if (ruid_arg == -1) |
| 6699 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 6700 | else |
| 6701 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 6702 | if (euid_arg == -1) |
| 6703 | euid = (uid_t)-1; |
| 6704 | else |
| 6705 | euid = euid_arg; |
| 6706 | if ((euid_arg != -1 && euid != euid_arg) || |
| 6707 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 6708 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6709 | return NULL; |
| 6710 | } |
| 6711 | if (setreuid(ruid, euid) < 0) { |
| 6712 | return posix_error(); |
| 6713 | } else { |
| 6714 | Py_INCREF(Py_None); |
| 6715 | return Py_None; |
| 6716 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6717 | } |
| 6718 | #endif /* HAVE_SETREUID */ |
| 6719 | |
| 6720 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6721 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6722 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6723 | Set the current process's real and effective group ids."); |
| 6724 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6725 | static PyObject * |
| 6726 | posix_setregid (PyObject *self, PyObject *args) |
| 6727 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6728 | long rgid_arg, egid_arg; |
| 6729 | gid_t rgid, egid; |
| 6730 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 6731 | return NULL; |
| 6732 | if (rgid_arg == -1) |
| 6733 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 6734 | else |
| 6735 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6736 | if (egid_arg == -1) |
| 6737 | egid = (gid_t)-1; |
| 6738 | else |
| 6739 | egid = egid_arg; |
| 6740 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6741 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6742 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6743 | return NULL; |
| 6744 | } |
| 6745 | if (setregid(rgid, egid) < 0) { |
| 6746 | return posix_error(); |
| 6747 | } else { |
| 6748 | Py_INCREF(Py_None); |
| 6749 | return Py_None; |
| 6750 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6751 | } |
| 6752 | #endif /* HAVE_SETREGID */ |
| 6753 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6754 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6755 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6756 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6757 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6758 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6759 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6760 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6761 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6762 | long gid_arg; |
| 6763 | gid_t gid; |
| 6764 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6765 | return NULL; |
| 6766 | gid = gid_arg; |
| 6767 | if (gid != gid_arg) { |
| 6768 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6769 | return NULL; |
| 6770 | } |
| 6771 | if (setgid(gid) < 0) |
| 6772 | return posix_error(); |
| 6773 | Py_INCREF(Py_None); |
| 6774 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6775 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6776 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6777 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6778 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6779 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6780 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6781 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6782 | |
| 6783 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6784 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6785 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6786 | int i, len; |
| 6787 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6788 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6789 | if (!PySequence_Check(groups)) { |
| 6790 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6791 | return NULL; |
| 6792 | } |
| 6793 | len = PySequence_Size(groups); |
| 6794 | if (len > MAX_GROUPS) { |
| 6795 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6796 | return NULL; |
| 6797 | } |
| 6798 | for(i = 0; i < len; i++) { |
| 6799 | PyObject *elem; |
| 6800 | elem = PySequence_GetItem(groups, i); |
| 6801 | if (!elem) |
| 6802 | return NULL; |
| 6803 | if (!PyLong_Check(elem)) { |
| 6804 | PyErr_SetString(PyExc_TypeError, |
| 6805 | "groups must be integers"); |
| 6806 | Py_DECREF(elem); |
| 6807 | return NULL; |
| 6808 | } else { |
| 6809 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6810 | if (PyErr_Occurred()) { |
| 6811 | PyErr_SetString(PyExc_TypeError, |
| 6812 | "group id too big"); |
| 6813 | Py_DECREF(elem); |
| 6814 | return NULL; |
| 6815 | } |
| 6816 | grouplist[i] = x; |
| 6817 | /* read back the value to see if it fitted in gid_t */ |
| 6818 | if (grouplist[i] != x) { |
| 6819 | PyErr_SetString(PyExc_TypeError, |
| 6820 | "group id too big"); |
| 6821 | Py_DECREF(elem); |
| 6822 | return NULL; |
| 6823 | } |
| 6824 | } |
| 6825 | Py_DECREF(elem); |
| 6826 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6827 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6828 | if (setgroups(len, grouplist) < 0) |
| 6829 | return posix_error(); |
| 6830 | Py_INCREF(Py_None); |
| 6831 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6832 | } |
| 6833 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6834 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6835 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6836 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6837 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6838 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6839 | PyObject *result; |
| 6840 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6841 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6842 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6843 | if (pid == -1) |
| 6844 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6845 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6846 | if (struct_rusage == NULL) { |
| 6847 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6848 | if (m == NULL) |
| 6849 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6850 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6851 | Py_DECREF(m); |
| 6852 | if (struct_rusage == NULL) |
| 6853 | return NULL; |
| 6854 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6855 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6856 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6857 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6858 | if (!result) |
| 6859 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6860 | |
| 6861 | #ifndef doubletime |
| 6862 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6863 | #endif |
| 6864 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6865 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6866 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6867 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6868 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6869 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6870 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6871 | SET_INT(result, 2, ru->ru_maxrss); |
| 6872 | SET_INT(result, 3, ru->ru_ixrss); |
| 6873 | SET_INT(result, 4, ru->ru_idrss); |
| 6874 | SET_INT(result, 5, ru->ru_isrss); |
| 6875 | SET_INT(result, 6, ru->ru_minflt); |
| 6876 | SET_INT(result, 7, ru->ru_majflt); |
| 6877 | SET_INT(result, 8, ru->ru_nswap); |
| 6878 | SET_INT(result, 9, ru->ru_inblock); |
| 6879 | SET_INT(result, 10, ru->ru_oublock); |
| 6880 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6881 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6882 | SET_INT(result, 13, ru->ru_nsignals); |
| 6883 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6884 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6885 | #undef SET_INT |
| 6886 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6887 | if (PyErr_Occurred()) { |
| 6888 | Py_DECREF(result); |
| 6889 | return NULL; |
| 6890 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6891 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6892 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6893 | } |
| 6894 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6895 | |
| 6896 | #ifdef HAVE_WAIT3 |
| 6897 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6898 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6899 | Wait for completion of a child process."); |
| 6900 | |
| 6901 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6902 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6903 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6904 | pid_t pid; |
| 6905 | int options; |
| 6906 | struct rusage ru; |
| 6907 | WAIT_TYPE status; |
| 6908 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6909 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6910 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6911 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6913 | Py_BEGIN_ALLOW_THREADS |
| 6914 | pid = wait3(&status, options, &ru); |
| 6915 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6916 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6917 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6918 | } |
| 6919 | #endif /* HAVE_WAIT3 */ |
| 6920 | |
| 6921 | #ifdef HAVE_WAIT4 |
| 6922 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6923 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6924 | Wait for completion of a given child process."); |
| 6925 | |
| 6926 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6927 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6928 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6929 | pid_t pid; |
| 6930 | int options; |
| 6931 | struct rusage ru; |
| 6932 | WAIT_TYPE status; |
| 6933 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6934 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6935 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6936 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6937 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6938 | Py_BEGIN_ALLOW_THREADS |
| 6939 | pid = wait4(pid, &status, options, &ru); |
| 6940 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6941 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6942 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6943 | } |
| 6944 | #endif /* HAVE_WAIT4 */ |
| 6945 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6946 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6947 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6948 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6949 | Wait for the completion of one or more child processes.\n\n\ |
| 6950 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6951 | id specifies the pid to wait on.\n\ |
| 6952 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6953 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6954 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6955 | no children in a waitable state."); |
| 6956 | |
| 6957 | static PyObject * |
| 6958 | posix_waitid(PyObject *self, PyObject *args) |
| 6959 | { |
| 6960 | PyObject *result; |
| 6961 | idtype_t idtype; |
| 6962 | id_t id; |
| 6963 | int options, res; |
| 6964 | siginfo_t si; |
| 6965 | si.si_pid = 0; |
| 6966 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6967 | return NULL; |
| 6968 | Py_BEGIN_ALLOW_THREADS |
| 6969 | res = waitid(idtype, id, &si, options); |
| 6970 | Py_END_ALLOW_THREADS |
| 6971 | if (res == -1) |
| 6972 | return posix_error(); |
| 6973 | |
| 6974 | if (si.si_pid == 0) |
| 6975 | Py_RETURN_NONE; |
| 6976 | |
| 6977 | result = PyStructSequence_New(&WaitidResultType); |
| 6978 | if (!result) |
| 6979 | return NULL; |
| 6980 | |
| 6981 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6982 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6983 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6984 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6985 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6986 | if (PyErr_Occurred()) { |
| 6987 | Py_DECREF(result); |
| 6988 | return NULL; |
| 6989 | } |
| 6990 | |
| 6991 | return result; |
| 6992 | } |
| 6993 | #endif |
| 6994 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6995 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6996 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6997 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6998 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6999 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7000 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7001 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7002 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7003 | pid_t pid; |
| 7004 | int options; |
| 7005 | WAIT_TYPE status; |
| 7006 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7007 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7008 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 7009 | return NULL; |
| 7010 | Py_BEGIN_ALLOW_THREADS |
| 7011 | pid = waitpid(pid, &status, options); |
| 7012 | Py_END_ALLOW_THREADS |
| 7013 | if (pid == -1) |
| 7014 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7015 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7016 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7017 | } |
| 7018 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7019 | #elif defined(HAVE_CWAIT) |
| 7020 | |
| 7021 | /* 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] | 7022 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7023 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7024 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7025 | |
| 7026 | static PyObject * |
| 7027 | posix_waitpid(PyObject *self, PyObject *args) |
| 7028 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7029 | Py_intptr_t pid; |
| 7030 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7032 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 7033 | return NULL; |
| 7034 | Py_BEGIN_ALLOW_THREADS |
| 7035 | pid = _cwait(&status, pid, options); |
| 7036 | Py_END_ALLOW_THREADS |
| 7037 | if (pid == -1) |
| 7038 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7039 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7040 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 7041 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7042 | } |
| 7043 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7044 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7045 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7046 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7047 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7048 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7049 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7050 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7051 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7052 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7053 | pid_t pid; |
| 7054 | WAIT_TYPE status; |
| 7055 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7056 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7057 | Py_BEGIN_ALLOW_THREADS |
| 7058 | pid = wait(&status); |
| 7059 | Py_END_ALLOW_THREADS |
| 7060 | if (pid == -1) |
| 7061 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7062 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7063 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7064 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7065 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7066 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7067 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7068 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 7069 | PyDoc_STRVAR(readlink__doc__, |
| 7070 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 7071 | Return a string representing the path to which the symbolic link points.\n\ |
| 7072 | \n\ |
| 7073 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7074 | and path should be relative; path will then be relative to that directory.\n\ |
| 7075 | dir_fd may not be implemented on your platform.\n\ |
| 7076 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7077 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7078 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7079 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7080 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7081 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7082 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7083 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7084 | path_t path; |
| 7085 | int dir_fd = DEFAULT_DIR_FD; |
| 7086 | char buffer[MAXPATHLEN]; |
| 7087 | ssize_t length; |
| 7088 | PyObject *return_value = NULL; |
| 7089 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7090 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7091 | memset(&path, 0, sizeof(path)); |
| 7092 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 7093 | path_converter, &path, |
| 7094 | #ifdef HAVE_READLINKAT |
| 7095 | dir_fd_converter, &dir_fd |
| 7096 | #else |
| 7097 | dir_fd_unavailable, &dir_fd |
| 7098 | #endif |
| 7099 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7100 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7101 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7102 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7103 | #ifdef HAVE_READLINKAT |
| 7104 | if (dir_fd != DEFAULT_DIR_FD) |
| 7105 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 7106 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7107 | #endif |
| 7108 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 7109 | Py_END_ALLOW_THREADS |
| 7110 | |
| 7111 | if (length < 0) { |
| 7112 | return_value = path_posix_error("readlink", &path); |
| 7113 | goto exit; |
| 7114 | } |
| 7115 | |
| 7116 | if (PyUnicode_Check(path.object)) |
| 7117 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7118 | else |
| 7119 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 7120 | exit: |
| 7121 | path_cleanup(&path); |
| 7122 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7123 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7124 | |
| 7125 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7126 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7127 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7128 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7129 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7130 | PyDoc_STRVAR(posix_symlink__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7131 | "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 7132 | Create a symbolic link pointing to src named dst.\n\n\ |
| 7133 | target_is_directory is required on Windows if the target is to be\n\ |
| 7134 | interpreted as a directory. (On Windows, symlink requires\n\ |
| 7135 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\ |
| 7136 | target_is_directory is ignored on non-Windows platforms.\n\ |
| 7137 | \n\ |
| 7138 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7139 | and path should be relative; path will then be relative to that directory.\n\ |
| 7140 | dir_fd may not be implemented on your platform.\n\ |
| 7141 | If it is unavailable, using it will raise a NotImplementedError."); |
| 7142 | |
| 7143 | #if defined(MS_WINDOWS) |
| 7144 | |
| 7145 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 7146 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 7147 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
| 7148 | static int |
| 7149 | check_CreateSymbolicLink() |
| 7150 | { |
| 7151 | HINSTANCE hKernel32; |
| 7152 | /* only recheck */ |
| 7153 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 7154 | return 1; |
| 7155 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 7156 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 7157 | "CreateSymbolicLinkW"); |
| 7158 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 7159 | "CreateSymbolicLinkA"); |
| 7160 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 7161 | } |
| 7162 | |
| 7163 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7164 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7165 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7166 | posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7167 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7168 | path_t src; |
| 7169 | path_t dst; |
| 7170 | int dir_fd = DEFAULT_DIR_FD; |
| 7171 | int target_is_directory = 0; |
| 7172 | static char *keywords[] = {"src", "dst", "target_is_directory", |
| 7173 | "dir_fd", NULL}; |
| 7174 | PyObject *return_value; |
| 7175 | #ifdef MS_WINDOWS |
| 7176 | DWORD result; |
| 7177 | #else |
| 7178 | int result; |
| 7179 | #endif |
| 7180 | |
| 7181 | memset(&src, 0, sizeof(src)); |
| 7182 | src.argument_name = "src"; |
| 7183 | memset(&dst, 0, sizeof(dst)); |
| 7184 | dst.argument_name = "dst"; |
| 7185 | |
| 7186 | #ifdef MS_WINDOWS |
| 7187 | if (!check_CreateSymbolicLink()) { |
| 7188 | PyErr_SetString(PyExc_NotImplementedError, |
| 7189 | "CreateSymbolicLink functions not found"); |
| 7190 | return NULL; |
| 7191 | } |
| 7192 | if (!win32_can_symlink) { |
| 7193 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
| 7194 | return NULL; |
| 7195 | } |
| 7196 | #endif |
| 7197 | |
| 7198 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink", |
| 7199 | keywords, |
| 7200 | path_converter, &src, |
| 7201 | path_converter, &dst, |
| 7202 | &target_is_directory, |
| 7203 | #ifdef HAVE_SYMLINKAT |
| 7204 | dir_fd_converter, &dir_fd |
| 7205 | #else |
| 7206 | dir_fd_unavailable, &dir_fd |
| 7207 | #endif |
| 7208 | )) |
| 7209 | return NULL; |
| 7210 | |
| 7211 | if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) { |
| 7212 | PyErr_SetString(PyExc_ValueError, |
| 7213 | "symlink: src and dst must be the same type"); |
| 7214 | return_value = NULL; |
| 7215 | goto exit; |
| 7216 | } |
| 7217 | |
| 7218 | #ifdef MS_WINDOWS |
| 7219 | Py_BEGIN_ALLOW_THREADS |
| 7220 | if (dst.wide) |
| 7221 | result = Py_CreateSymbolicLinkW(dst.wide, src.wide, |
| 7222 | target_is_directory); |
| 7223 | else |
| 7224 | result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow, |
| 7225 | target_is_directory); |
| 7226 | Py_END_ALLOW_THREADS |
| 7227 | |
| 7228 | if (!result) { |
| 7229 | return_value = win32_error_object("symlink", src.object); |
| 7230 | goto exit; |
| 7231 | } |
| 7232 | |
| 7233 | #else |
| 7234 | |
| 7235 | Py_BEGIN_ALLOW_THREADS |
| 7236 | #if HAVE_SYMLINKAT |
| 7237 | if (dir_fd != DEFAULT_DIR_FD) |
| 7238 | result = symlinkat(src.narrow, dir_fd, dst.narrow); |
| 7239 | else |
| 7240 | #endif |
| 7241 | result = symlink(src.narrow, dst.narrow); |
| 7242 | Py_END_ALLOW_THREADS |
| 7243 | |
| 7244 | if (result) { |
| 7245 | return_value = path_error("symlink", &dst); |
| 7246 | goto exit; |
| 7247 | } |
| 7248 | #endif |
| 7249 | |
| 7250 | return_value = Py_None; |
| 7251 | Py_INCREF(Py_None); |
| 7252 | goto exit; /* silence "unused label" warning */ |
| 7253 | exit: |
| 7254 | path_cleanup(&src); |
| 7255 | path_cleanup(&dst); |
| 7256 | return return_value; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7257 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7258 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7259 | #endif /* HAVE_SYMLINK */ |
| 7260 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7261 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7262 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 7263 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7264 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7265 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7266 | { |
| 7267 | wchar_t *path; |
| 7268 | DWORD n_bytes_returned; |
| 7269 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7270 | PyObject *po, *result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7271 | int dir_fd; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7272 | HANDLE reparse_point_handle; |
| 7273 | |
| 7274 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7275 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 7276 | wchar_t *print_name; |
| 7277 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7278 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 7279 | |
| 7280 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 7281 | &po, |
| 7282 | dir_fd_unavailable, &dir_fd |
| 7283 | )) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7284 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7285 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7286 | path = PyUnicode_AsUnicode(po); |
| 7287 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7288 | return NULL; |
| 7289 | |
| 7290 | /* First get a handle to the reparse point */ |
| 7291 | Py_BEGIN_ALLOW_THREADS |
| 7292 | reparse_point_handle = CreateFileW( |
| 7293 | path, |
| 7294 | 0, |
| 7295 | 0, |
| 7296 | 0, |
| 7297 | OPEN_EXISTING, |
| 7298 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7299 | 0); |
| 7300 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7301 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7302 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7303 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7304 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7305 | Py_BEGIN_ALLOW_THREADS |
| 7306 | /* New call DeviceIoControl to read the reparse point */ |
| 7307 | io_result = DeviceIoControl( |
| 7308 | reparse_point_handle, |
| 7309 | FSCTL_GET_REPARSE_POINT, |
| 7310 | 0, 0, /* in buffer */ |
| 7311 | target_buffer, sizeof(target_buffer), |
| 7312 | &n_bytes_returned, |
| 7313 | 0 /* we're not using OVERLAPPED_IO */ |
| 7314 | ); |
| 7315 | CloseHandle(reparse_point_handle); |
| 7316 | Py_END_ALLOW_THREADS |
| 7317 | |
| 7318 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7319 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7320 | |
| 7321 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 7322 | { |
| 7323 | PyErr_SetString(PyExc_ValueError, |
| 7324 | "not a symbolic link"); |
| 7325 | return NULL; |
| 7326 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 7327 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7328 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 7329 | |
| 7330 | result = PyUnicode_FromWideChar(print_name, |
| 7331 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 7332 | return result; |
| 7333 | } |
| 7334 | |
| 7335 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 7336 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7337 | |
| 7338 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7339 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 7340 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7341 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7342 | { |
| 7343 | ULONG value = 0; |
| 7344 | |
| 7345 | Py_BEGIN_ALLOW_THREADS |
| 7346 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 7347 | Py_END_ALLOW_THREADS |
| 7348 | |
| 7349 | return value; |
| 7350 | } |
| 7351 | |
| 7352 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7353 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7354 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7355 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7356 | return Py_BuildValue("ddddd", |
| 7357 | (double)0 /* t.tms_utime / HZ */, |
| 7358 | (double)0 /* t.tms_stime / HZ */, |
| 7359 | (double)0 /* t.tms_cutime / HZ */, |
| 7360 | (double)0 /* t.tms_cstime / HZ */, |
| 7361 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7362 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7363 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7364 | #define NEED_TICKS_PER_SECOND |
| 7365 | static long ticks_per_second = -1; |
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 | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7368 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7369 | struct tms t; |
| 7370 | clock_t c; |
| 7371 | errno = 0; |
| 7372 | c = times(&t); |
| 7373 | if (c == (clock_t) -1) |
| 7374 | return posix_error(); |
| 7375 | return Py_BuildValue("ddddd", |
| 7376 | (double)t.tms_utime / ticks_per_second, |
| 7377 | (double)t.tms_stime / ticks_per_second, |
| 7378 | (double)t.tms_cutime / ticks_per_second, |
| 7379 | (double)t.tms_cstime / ticks_per_second, |
| 7380 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7381 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7382 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7383 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7384 | |
| 7385 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7386 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7387 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7388 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7389 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7390 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7391 | FILETIME create, exit, kernel, user; |
| 7392 | HANDLE hProc; |
| 7393 | hProc = GetCurrentProcess(); |
| 7394 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7395 | /* The fields of a FILETIME structure are the hi and lo part |
| 7396 | of a 64-bit value expressed in 100 nanosecond units. |
| 7397 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7398 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7399 | */ |
| 7400 | return Py_BuildValue( |
| 7401 | "ddddd", |
| 7402 | (double)(user.dwHighDateTime*429.4967296 + |
| 7403 | user.dwLowDateTime*1e-7), |
| 7404 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7405 | kernel.dwLowDateTime*1e-7), |
| 7406 | (double)0, |
| 7407 | (double)0, |
| 7408 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7409 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7410 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7411 | |
| 7412 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7413 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7414 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7415 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7416 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7417 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7418 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7419 | #ifdef HAVE_GETSID |
| 7420 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7421 | "getsid(pid) -> sid\n\n\ |
| 7422 | Call the system call getsid()."); |
| 7423 | |
| 7424 | static PyObject * |
| 7425 | posix_getsid(PyObject *self, PyObject *args) |
| 7426 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7427 | pid_t pid; |
| 7428 | int sid; |
| 7429 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7430 | return NULL; |
| 7431 | sid = getsid(pid); |
| 7432 | if (sid < 0) |
| 7433 | return posix_error(); |
| 7434 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7435 | } |
| 7436 | #endif /* HAVE_GETSID */ |
| 7437 | |
| 7438 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7439 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7440 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7441 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7442 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7443 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7444 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7445 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7446 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7447 | if (setsid() < 0) |
| 7448 | return posix_error(); |
| 7449 | Py_INCREF(Py_None); |
| 7450 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7451 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7452 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7453 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7454 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7455 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7456 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7457 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7458 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7459 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7460 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7461 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7462 | pid_t pid; |
| 7463 | int pgrp; |
| 7464 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7465 | return NULL; |
| 7466 | if (setpgid(pid, pgrp) < 0) |
| 7467 | return posix_error(); |
| 7468 | Py_INCREF(Py_None); |
| 7469 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7470 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7471 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7472 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7473 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7474 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7475 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7476 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7477 | 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] | 7478 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7479 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7480 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7481 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7482 | int fd; |
| 7483 | pid_t pgid; |
| 7484 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7485 | return NULL; |
| 7486 | pgid = tcgetpgrp(fd); |
| 7487 | if (pgid < 0) |
| 7488 | return posix_error(); |
| 7489 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7490 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7491 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7492 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7493 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7494 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7495 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7496 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7497 | 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] | 7498 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7499 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7500 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7501 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7502 | int fd; |
| 7503 | pid_t pgid; |
| 7504 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7505 | return NULL; |
| 7506 | if (tcsetpgrp(fd, pgid) < 0) |
| 7507 | return posix_error(); |
| 7508 | Py_INCREF(Py_None); |
| 7509 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7510 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7511 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7512 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7513 | /* Functions acting on file descriptors */ |
| 7514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7515 | PyDoc_STRVAR(posix_open__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7516 | "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 7517 | Open a file for low level IO. Returns a file handle (integer).\n\ |
| 7518 | \n\ |
| 7519 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 7520 | and path should be relative; path will then be relative to that directory.\n\ |
| 7521 | dir_fd may not be implemented on your platform.\n\ |
| 7522 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7523 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7524 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7525 | posix_open(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7526 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7527 | path_t path; |
| 7528 | int flags; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7529 | int mode = 0777; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7530 | int dir_fd = DEFAULT_DIR_FD; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7531 | int fd; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7532 | PyObject *return_value = NULL; |
| 7533 | static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7534 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7535 | memset(&path, 0, sizeof(path)); |
| 7536 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords, |
| 7537 | path_converter, &path, |
| 7538 | &flags, &mode, |
| 7539 | #ifdef HAVE_OPENAT |
| 7540 | dir_fd_converter, &dir_fd |
| 7541 | #else |
| 7542 | dir_fd_unavailable, &dir_fd |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7543 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7544 | )) |
| 7545 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7546 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7547 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7548 | #ifdef MS_WINDOWS |
| 7549 | if (path.wide) |
| 7550 | fd = _wopen(path.wide, flags, mode); |
| 7551 | else |
| 7552 | #endif |
| 7553 | #ifdef HAVE_OPENAT |
| 7554 | if (dir_fd != DEFAULT_DIR_FD) |
| 7555 | fd = openat(dir_fd, path.narrow, flags, mode); |
| 7556 | else |
| 7557 | #endif |
| 7558 | fd = open(path.narrow, flags, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7559 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7560 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7561 | if (fd == -1) { |
| 7562 | #ifdef MS_WINDOWS |
| 7563 | /* force use of posix_error here for exact backwards compatibility */ |
| 7564 | if (path.wide) |
| 7565 | return_value = posix_error(); |
| 7566 | else |
| 7567 | #endif |
| 7568 | return_value = path_error("open", &path); |
| 7569 | goto exit; |
| 7570 | } |
| 7571 | |
| 7572 | return_value = PyLong_FromLong((long)fd); |
| 7573 | |
| 7574 | exit: |
| 7575 | path_cleanup(&path); |
| 7576 | return return_value; |
| 7577 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7578 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7579 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7580 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7581 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7582 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7583 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7584 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7585 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7586 | int fd, res; |
| 7587 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7588 | return NULL; |
| 7589 | if (!_PyVerify_fd(fd)) |
| 7590 | return posix_error(); |
| 7591 | Py_BEGIN_ALLOW_THREADS |
| 7592 | res = close(fd); |
| 7593 | Py_END_ALLOW_THREADS |
| 7594 | if (res < 0) |
| 7595 | return posix_error(); |
| 7596 | Py_INCREF(Py_None); |
| 7597 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7598 | } |
| 7599 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7600 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7601 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7602 | "closerange(fd_low, fd_high)\n\n\ |
| 7603 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7604 | |
| 7605 | static PyObject * |
| 7606 | posix_closerange(PyObject *self, PyObject *args) |
| 7607 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7608 | int fd_from, fd_to, i; |
| 7609 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7610 | return NULL; |
| 7611 | Py_BEGIN_ALLOW_THREADS |
| 7612 | for (i = fd_from; i < fd_to; i++) |
| 7613 | if (_PyVerify_fd(i)) |
| 7614 | close(i); |
| 7615 | Py_END_ALLOW_THREADS |
| 7616 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7617 | } |
| 7618 | |
| 7619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7620 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7621 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7622 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7623 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7624 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7625 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7626 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7627 | int fd; |
| 7628 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7629 | return NULL; |
| 7630 | if (!_PyVerify_fd(fd)) |
| 7631 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7632 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7633 | if (fd < 0) |
| 7634 | return posix_error(); |
| 7635 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7636 | } |
| 7637 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7638 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7639 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7640 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7641 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7642 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7643 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7644 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7645 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7646 | int fd, fd2, res; |
| 7647 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 7648 | return NULL; |
| 7649 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7650 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7651 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7652 | if (res < 0) |
| 7653 | return posix_error(); |
| 7654 | Py_INCREF(Py_None); |
| 7655 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7656 | } |
| 7657 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7658 | #ifdef HAVE_LOCKF |
| 7659 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7660 | "lockf(fd, cmd, len)\n\n\ |
| 7661 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7662 | fd is an open file descriptor.\n\ |
| 7663 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7664 | F_TEST.\n\ |
| 7665 | len specifies the section of the file to lock."); |
| 7666 | |
| 7667 | static PyObject * |
| 7668 | posix_lockf(PyObject *self, PyObject *args) |
| 7669 | { |
| 7670 | int fd, cmd, res; |
| 7671 | off_t len; |
| 7672 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7673 | &fd, &cmd, _parse_off_t, &len)) |
| 7674 | return NULL; |
| 7675 | |
| 7676 | Py_BEGIN_ALLOW_THREADS |
| 7677 | res = lockf(fd, cmd, len); |
| 7678 | Py_END_ALLOW_THREADS |
| 7679 | |
| 7680 | if (res < 0) |
| 7681 | return posix_error(); |
| 7682 | |
| 7683 | Py_RETURN_NONE; |
| 7684 | } |
| 7685 | #endif |
| 7686 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7687 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7688 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7689 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7690 | Set the current position of a file descriptor.\n\ |
| 7691 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7692 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7693 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7694 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7695 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7696 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7697 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7698 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7699 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7700 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7701 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7702 | PyObject *posobj; |
| 7703 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7704 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7705 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7706 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7707 | switch (how) { |
| 7708 | case 0: how = SEEK_SET; break; |
| 7709 | case 1: how = SEEK_CUR; break; |
| 7710 | case 2: how = SEEK_END; break; |
| 7711 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7712 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7713 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7714 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7715 | pos = PyLong_AsLong(posobj); |
| 7716 | #else |
| 7717 | pos = PyLong_AsLongLong(posobj); |
| 7718 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7719 | if (PyErr_Occurred()) |
| 7720 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7721 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7722 | if (!_PyVerify_fd(fd)) |
| 7723 | return posix_error(); |
| 7724 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7725 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7726 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7727 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7728 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7729 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7730 | Py_END_ALLOW_THREADS |
| 7731 | if (res < 0) |
| 7732 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7733 | |
| 7734 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7735 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7736 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7737 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7738 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7739 | } |
| 7740 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7742 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7743 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7744 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7745 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7746 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7747 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7748 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7749 | int fd, size; |
| 7750 | Py_ssize_t n; |
| 7751 | PyObject *buffer; |
| 7752 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7753 | return NULL; |
| 7754 | if (size < 0) { |
| 7755 | errno = EINVAL; |
| 7756 | return posix_error(); |
| 7757 | } |
| 7758 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7759 | if (buffer == NULL) |
| 7760 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7761 | if (!_PyVerify_fd(fd)) { |
| 7762 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7763 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7764 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7765 | Py_BEGIN_ALLOW_THREADS |
| 7766 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7767 | Py_END_ALLOW_THREADS |
| 7768 | if (n < 0) { |
| 7769 | Py_DECREF(buffer); |
| 7770 | return posix_error(); |
| 7771 | } |
| 7772 | if (n != size) |
| 7773 | _PyBytes_Resize(&buffer, n); |
| 7774 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7775 | } |
| 7776 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7777 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7778 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7779 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7780 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7781 | { |
| 7782 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7783 | Py_ssize_t blen, total = 0; |
| 7784 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7785 | *iov = PyMem_New(struct iovec, cnt); |
| 7786 | if (*iov == NULL) { |
| 7787 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7788 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7789 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7790 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7791 | *buf = PyMem_New(Py_buffer, cnt); |
| 7792 | if (*buf == NULL) { |
| 7793 | PyMem_Del(*iov); |
| 7794 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7795 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
| 7798 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7799 | PyObject *item = PySequence_GetItem(seq, i); |
| 7800 | if (item == NULL) |
| 7801 | goto fail; |
| 7802 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7803 | Py_DECREF(item); |
| 7804 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7805 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7806 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7807 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7808 | blen = (*buf)[i].len; |
| 7809 | (*iov)[i].iov_len = blen; |
| 7810 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7811 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7812 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7813 | |
| 7814 | fail: |
| 7815 | PyMem_Del(*iov); |
| 7816 | for (j = 0; j < i; j++) { |
| 7817 | PyBuffer_Release(&(*buf)[j]); |
| 7818 | } |
| 7819 | PyMem_Del(*buf); |
| 7820 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7821 | } |
| 7822 | |
| 7823 | static void |
| 7824 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7825 | { |
| 7826 | int i; |
| 7827 | PyMem_Del(iov); |
| 7828 | for (i = 0; i < cnt; i++) { |
| 7829 | PyBuffer_Release(&buf[i]); |
| 7830 | } |
| 7831 | PyMem_Del(buf); |
| 7832 | } |
| 7833 | #endif |
| 7834 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7835 | #ifdef HAVE_READV |
| 7836 | PyDoc_STRVAR(posix_readv__doc__, |
| 7837 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7838 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7839 | is an arbitrary sequence of writable buffers.\n\ |
| 7840 | Returns the total number of bytes read."); |
| 7841 | |
| 7842 | static PyObject * |
| 7843 | posix_readv(PyObject *self, PyObject *args) |
| 7844 | { |
| 7845 | int fd, cnt; |
| 7846 | Py_ssize_t n; |
| 7847 | PyObject *seq; |
| 7848 | struct iovec *iov; |
| 7849 | Py_buffer *buf; |
| 7850 | |
| 7851 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7852 | return NULL; |
| 7853 | if (!PySequence_Check(seq)) { |
| 7854 | PyErr_SetString(PyExc_TypeError, |
| 7855 | "readv() arg 2 must be a sequence"); |
| 7856 | return NULL; |
| 7857 | } |
| 7858 | cnt = PySequence_Size(seq); |
| 7859 | |
| 7860 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7861 | return NULL; |
| 7862 | |
| 7863 | Py_BEGIN_ALLOW_THREADS |
| 7864 | n = readv(fd, iov, cnt); |
| 7865 | Py_END_ALLOW_THREADS |
| 7866 | |
| 7867 | iov_cleanup(iov, buf, cnt); |
| 7868 | return PyLong_FromSsize_t(n); |
| 7869 | } |
| 7870 | #endif |
| 7871 | |
| 7872 | #ifdef HAVE_PREAD |
| 7873 | PyDoc_STRVAR(posix_pread__doc__, |
| 7874 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7875 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7876 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7877 | |
| 7878 | static PyObject * |
| 7879 | posix_pread(PyObject *self, PyObject *args) |
| 7880 | { |
| 7881 | int fd, size; |
| 7882 | off_t offset; |
| 7883 | Py_ssize_t n; |
| 7884 | PyObject *buffer; |
| 7885 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7886 | return NULL; |
| 7887 | |
| 7888 | if (size < 0) { |
| 7889 | errno = EINVAL; |
| 7890 | return posix_error(); |
| 7891 | } |
| 7892 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7893 | if (buffer == NULL) |
| 7894 | return NULL; |
| 7895 | if (!_PyVerify_fd(fd)) { |
| 7896 | Py_DECREF(buffer); |
| 7897 | return posix_error(); |
| 7898 | } |
| 7899 | Py_BEGIN_ALLOW_THREADS |
| 7900 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7901 | Py_END_ALLOW_THREADS |
| 7902 | if (n < 0) { |
| 7903 | Py_DECREF(buffer); |
| 7904 | return posix_error(); |
| 7905 | } |
| 7906 | if (n != size) |
| 7907 | _PyBytes_Resize(&buffer, n); |
| 7908 | return buffer; |
| 7909 | } |
| 7910 | #endif |
| 7911 | |
| 7912 | PyDoc_STRVAR(posix_write__doc__, |
| 7913 | "write(fd, string) -> byteswritten\n\n\ |
| 7914 | Write a string to a file descriptor."); |
| 7915 | |
| 7916 | static PyObject * |
| 7917 | posix_write(PyObject *self, PyObject *args) |
| 7918 | { |
| 7919 | Py_buffer pbuf; |
| 7920 | int fd; |
| 7921 | Py_ssize_t size, len; |
| 7922 | |
| 7923 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7924 | return NULL; |
| 7925 | if (!_PyVerify_fd(fd)) { |
| 7926 | PyBuffer_Release(&pbuf); |
| 7927 | return posix_error(); |
| 7928 | } |
| 7929 | len = pbuf.len; |
| 7930 | Py_BEGIN_ALLOW_THREADS |
| 7931 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7932 | if (len > INT_MAX) |
| 7933 | len = INT_MAX; |
| 7934 | size = write(fd, pbuf.buf, (int)len); |
| 7935 | #else |
| 7936 | size = write(fd, pbuf.buf, len); |
| 7937 | #endif |
| 7938 | Py_END_ALLOW_THREADS |
| 7939 | PyBuffer_Release(&pbuf); |
| 7940 | if (size < 0) |
| 7941 | return posix_error(); |
| 7942 | return PyLong_FromSsize_t(size); |
| 7943 | } |
| 7944 | |
| 7945 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7946 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7947 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7948 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7949 | -> byteswritten\n\ |
| 7950 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7951 | |
| 7952 | static PyObject * |
| 7953 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7954 | { |
| 7955 | int in, out; |
| 7956 | Py_ssize_t ret; |
| 7957 | off_t offset; |
| 7958 | |
| 7959 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7960 | #ifndef __APPLE__ |
| 7961 | Py_ssize_t len; |
| 7962 | #endif |
| 7963 | PyObject *headers = NULL, *trailers = NULL; |
| 7964 | Py_buffer *hbuf, *tbuf; |
| 7965 | off_t sbytes; |
| 7966 | struct sf_hdtr sf; |
| 7967 | int flags = 0; |
| 7968 | sf.headers = NULL; |
| 7969 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7970 | static char *keywords[] = {"out", "in", |
| 7971 | "offset", "count", |
| 7972 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7973 | |
| 7974 | #ifdef __APPLE__ |
| 7975 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7976 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7977 | #else |
| 7978 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7979 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7980 | #endif |
| 7981 | &headers, &trailers, &flags)) |
| 7982 | return NULL; |
| 7983 | if (headers != NULL) { |
| 7984 | if (!PySequence_Check(headers)) { |
| 7985 | PyErr_SetString(PyExc_TypeError, |
| 7986 | "sendfile() headers must be a sequence or None"); |
| 7987 | return NULL; |
| 7988 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7989 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7990 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7991 | if (sf.hdr_cnt > 0 && |
| 7992 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7993 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7994 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7995 | #ifdef __APPLE__ |
| 7996 | sbytes += i; |
| 7997 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7998 | } |
| 7999 | } |
| 8000 | if (trailers != NULL) { |
| 8001 | if (!PySequence_Check(trailers)) { |
| 8002 | PyErr_SetString(PyExc_TypeError, |
| 8003 | "sendfile() trailers must be a sequence or None"); |
| 8004 | return NULL; |
| 8005 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8006 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8007 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8008 | if (sf.trl_cnt > 0 && |
| 8009 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 8010 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8011 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8012 | #ifdef __APPLE__ |
| 8013 | sbytes += i; |
| 8014 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8015 | } |
| 8016 | } |
| 8017 | |
| 8018 | Py_BEGIN_ALLOW_THREADS |
| 8019 | #ifdef __APPLE__ |
| 8020 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 8021 | #else |
| 8022 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 8023 | #endif |
| 8024 | Py_END_ALLOW_THREADS |
| 8025 | |
| 8026 | if (sf.headers != NULL) |
| 8027 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 8028 | if (sf.trailers != NULL) |
| 8029 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 8030 | |
| 8031 | if (ret < 0) { |
| 8032 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 8033 | if (sbytes != 0) { |
| 8034 | // some data has been sent |
| 8035 | goto done; |
| 8036 | } |
| 8037 | else { |
| 8038 | // no data has been sent; upper application is supposed |
| 8039 | // to retry on EAGAIN or EBUSY |
| 8040 | return posix_error(); |
| 8041 | } |
| 8042 | } |
| 8043 | return posix_error(); |
| 8044 | } |
| 8045 | goto done; |
| 8046 | |
| 8047 | done: |
| 8048 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 8049 | return Py_BuildValue("l", sbytes); |
| 8050 | #else |
| 8051 | return Py_BuildValue("L", sbytes); |
| 8052 | #endif |
| 8053 | |
| 8054 | #else |
| 8055 | Py_ssize_t count; |
| 8056 | PyObject *offobj; |
| 8057 | static char *keywords[] = {"out", "in", |
| 8058 | "offset", "count", NULL}; |
| 8059 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 8060 | keywords, &out, &in, &offobj, &count)) |
| 8061 | return NULL; |
| 8062 | #ifdef linux |
| 8063 | if (offobj == Py_None) { |
| 8064 | Py_BEGIN_ALLOW_THREADS |
| 8065 | ret = sendfile(out, in, NULL, count); |
| 8066 | Py_END_ALLOW_THREADS |
| 8067 | if (ret < 0) |
| 8068 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 8069 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8070 | } |
| 8071 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 8072 | if (!_parse_off_t(offobj, &offset)) |
| 8073 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8074 | Py_BEGIN_ALLOW_THREADS |
| 8075 | ret = sendfile(out, in, &offset, count); |
| 8076 | Py_END_ALLOW_THREADS |
| 8077 | if (ret < 0) |
| 8078 | return posix_error(); |
| 8079 | return Py_BuildValue("n", ret); |
| 8080 | #endif |
| 8081 | } |
| 8082 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8083 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8084 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8085 | "fstat(fd) -> stat result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8086 | Like stat(), but for an open file descriptor.\n\ |
| 8087 | Equivalent to stat(fd=fd)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8088 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8089 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8090 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8091 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8092 | int fd; |
| 8093 | STRUCT_STAT st; |
| 8094 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8095 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8096 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 8097 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8098 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 8099 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 8100 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8101 | if (!_PyVerify_fd(fd)) |
| 8102 | return posix_error(); |
| 8103 | Py_BEGIN_ALLOW_THREADS |
| 8104 | res = FSTAT(fd, &st); |
| 8105 | Py_END_ALLOW_THREADS |
| 8106 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8107 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8108 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8109 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8110 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 8111 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8112 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8113 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8114 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8115 | } |
| 8116 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8117 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8118 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8119 | 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] | 8120 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8121 | |
| 8122 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 8123 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8124 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8125 | int fd; |
| 8126 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 8127 | return NULL; |
| 8128 | if (!_PyVerify_fd(fd)) |
| 8129 | return PyBool_FromLong(0); |
| 8130 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8131 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8132 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8133 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8134 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8135 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8136 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8137 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8138 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8139 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8140 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8141 | #if defined(PYOS_OS2) |
| 8142 | HFILE read, write; |
| 8143 | APIRET rc; |
| 8144 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8145 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8146 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8147 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8148 | |
| 8149 | return Py_BuildValue("(ii)", read, write); |
| 8150 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8151 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8152 | int fds[2]; |
| 8153 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8154 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8155 | if (res != 0) |
| 8156 | return posix_error(); |
| 8157 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8158 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8159 | HANDLE read, write; |
| 8160 | int read_fd, write_fd; |
| 8161 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8162 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8163 | if (!ok) |
| 8164 | return win32_error("CreatePipe", NULL); |
| 8165 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 8166 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 8167 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8168 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8169 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8170 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8171 | #endif /* HAVE_PIPE */ |
| 8172 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8173 | #ifdef HAVE_PIPE2 |
| 8174 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8175 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 8176 | Create a pipe with flags set atomically.\n\ |
| 8177 | flags can be constructed by ORing together one or more of these values:\n\ |
| 8178 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8179 | "); |
| 8180 | |
| 8181 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8182 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8183 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8184 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8185 | int fds[2]; |
| 8186 | int res; |
| 8187 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 8188 | flags = PyLong_AsLong(arg); |
| 8189 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 8190 | return NULL; |
| 8191 | |
| 8192 | res = pipe2(fds, flags); |
| 8193 | if (res != 0) |
| 8194 | return posix_error(); |
| 8195 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 8196 | } |
| 8197 | #endif /* HAVE_PIPE2 */ |
| 8198 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8199 | #ifdef HAVE_WRITEV |
| 8200 | PyDoc_STRVAR(posix_writev__doc__, |
| 8201 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 8202 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 8203 | arbitrary sequence of buffers.\n\ |
| 8204 | Returns the total bytes written."); |
| 8205 | |
| 8206 | static PyObject * |
| 8207 | posix_writev(PyObject *self, PyObject *args) |
| 8208 | { |
| 8209 | int fd, cnt; |
| 8210 | Py_ssize_t res; |
| 8211 | PyObject *seq; |
| 8212 | struct iovec *iov; |
| 8213 | Py_buffer *buf; |
| 8214 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 8215 | return NULL; |
| 8216 | if (!PySequence_Check(seq)) { |
| 8217 | PyErr_SetString(PyExc_TypeError, |
| 8218 | "writev() arg 2 must be a sequence"); |
| 8219 | return NULL; |
| 8220 | } |
| 8221 | cnt = PySequence_Size(seq); |
| 8222 | |
| 8223 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 8224 | return NULL; |
| 8225 | } |
| 8226 | |
| 8227 | Py_BEGIN_ALLOW_THREADS |
| 8228 | res = writev(fd, iov, cnt); |
| 8229 | Py_END_ALLOW_THREADS |
| 8230 | |
| 8231 | iov_cleanup(iov, buf, cnt); |
| 8232 | return PyLong_FromSsize_t(res); |
| 8233 | } |
| 8234 | #endif |
| 8235 | |
| 8236 | #ifdef HAVE_PWRITE |
| 8237 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 8238 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 8239 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 8240 | offset unchanged."); |
| 8241 | |
| 8242 | static PyObject * |
| 8243 | posix_pwrite(PyObject *self, PyObject *args) |
| 8244 | { |
| 8245 | Py_buffer pbuf; |
| 8246 | int fd; |
| 8247 | off_t offset; |
| 8248 | Py_ssize_t size; |
| 8249 | |
| 8250 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 8251 | return NULL; |
| 8252 | |
| 8253 | if (!_PyVerify_fd(fd)) { |
| 8254 | PyBuffer_Release(&pbuf); |
| 8255 | return posix_error(); |
| 8256 | } |
| 8257 | Py_BEGIN_ALLOW_THREADS |
| 8258 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 8259 | Py_END_ALLOW_THREADS |
| 8260 | PyBuffer_Release(&pbuf); |
| 8261 | if (size < 0) |
| 8262 | return posix_error(); |
| 8263 | return PyLong_FromSsize_t(size); |
| 8264 | } |
| 8265 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8266 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8267 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8268 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8269 | "mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\ |
| 8270 | Create a FIFO (a POSIX named pipe).\n\ |
| 8271 | \n\ |
| 8272 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8273 | and path should be relative; path will then be relative to that directory.\n\ |
| 8274 | dir_fd may not be implemented on your platform.\n\ |
| 8275 | If it is unavailable, using it will raise a NotImplementedError."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8276 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8277 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8278 | posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8279 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8280 | path_t path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8281 | int mode = 0666; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8282 | int dir_fd = DEFAULT_DIR_FD; |
| 8283 | int result; |
| 8284 | PyObject *return_value = NULL; |
| 8285 | static char *keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 8286 | |
| 8287 | memset(&path, 0, sizeof(path)); |
| 8288 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords, |
| 8289 | path_converter, &path, |
| 8290 | &mode, |
| 8291 | #ifdef HAVE_MKFIFOAT |
| 8292 | dir_fd_converter, &dir_fd |
| 8293 | #else |
| 8294 | dir_fd_unavailable, &dir_fd |
| 8295 | #endif |
| 8296 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8297 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8298 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8299 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8300 | #ifdef HAVE_MKFIFOAT |
| 8301 | if (dir_fd != DEFAULT_DIR_FD) |
| 8302 | result = mkfifoat(dir_fd, path.narrow, mode); |
| 8303 | else |
| 8304 | #endif |
| 8305 | result = mkfifo(path.narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8306 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8307 | |
| 8308 | if (result < 0) { |
| 8309 | return_value = posix_error(); |
| 8310 | goto exit; |
| 8311 | } |
| 8312 | |
| 8313 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8314 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8315 | |
| 8316 | exit: |
| 8317 | path_cleanup(&path); |
| 8318 | return return_value; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8319 | } |
| 8320 | #endif |
| 8321 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8322 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8323 | PyDoc_STRVAR(posix_mknod__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8324 | "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] | 8325 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 8326 | named filename. mode specifies both the permissions to use and the\n\ |
| 8327 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 8328 | 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] | 8329 | device defines the newly created device special file (probably using\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8330 | os.makedev()), otherwise it is ignored.\n\ |
| 8331 | \n\ |
| 8332 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 8333 | and path should be relative; path will then be relative to that directory.\n\ |
| 8334 | dir_fd may not be implemented on your platform.\n\ |
| 8335 | If it is unavailable, using it will raise a NotImplementedError."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8336 | |
| 8337 | |
| 8338 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8339 | posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8340 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8341 | path_t path; |
| 8342 | int mode = 0666; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8343 | int device = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8344 | int dir_fd = DEFAULT_DIR_FD; |
| 8345 | int result; |
| 8346 | PyObject *return_value = NULL; |
| 8347 | static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 8348 | |
| 8349 | memset(&path, 0, sizeof(path)); |
| 8350 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords, |
| 8351 | path_converter, &path, |
| 8352 | &mode, &device, |
| 8353 | #ifdef HAVE_MKNODAT |
| 8354 | dir_fd_converter, &dir_fd |
| 8355 | #else |
| 8356 | dir_fd_unavailable, &dir_fd |
| 8357 | #endif |
| 8358 | )) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8359 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8360 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8361 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8362 | #ifdef HAVE_MKNODAT |
| 8363 | if (dir_fd != DEFAULT_DIR_FD) |
| 8364 | result = mknodat(dir_fd, path.narrow, mode, device); |
| 8365 | else |
| 8366 | #endif |
| 8367 | result = mknod(path.narrow, mode, device); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8368 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8369 | |
| 8370 | if (result < 0) { |
| 8371 | return_value = posix_error(); |
| 8372 | goto exit; |
| 8373 | } |
| 8374 | |
| 8375 | return_value = Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8376 | Py_INCREF(Py_None); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8377 | |
| 8378 | exit: |
| 8379 | path_cleanup(&path); |
| 8380 | return return_value; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8381 | } |
| 8382 | #endif |
| 8383 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8384 | #ifdef HAVE_DEVICE_MACROS |
| 8385 | PyDoc_STRVAR(posix_major__doc__, |
| 8386 | "major(device) -> major number\n\ |
| 8387 | Extracts a device major number from a raw device number."); |
| 8388 | |
| 8389 | static PyObject * |
| 8390 | posix_major(PyObject *self, PyObject *args) |
| 8391 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8392 | int device; |
| 8393 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 8394 | return NULL; |
| 8395 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8396 | } |
| 8397 | |
| 8398 | PyDoc_STRVAR(posix_minor__doc__, |
| 8399 | "minor(device) -> minor number\n\ |
| 8400 | Extracts a device minor number from a raw device number."); |
| 8401 | |
| 8402 | static PyObject * |
| 8403 | posix_minor(PyObject *self, PyObject *args) |
| 8404 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8405 | int device; |
| 8406 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 8407 | return NULL; |
| 8408 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8409 | } |
| 8410 | |
| 8411 | PyDoc_STRVAR(posix_makedev__doc__, |
| 8412 | "makedev(major, minor) -> device number\n\ |
| 8413 | Composes a raw device number from the major and minor device numbers."); |
| 8414 | |
| 8415 | static PyObject * |
| 8416 | posix_makedev(PyObject *self, PyObject *args) |
| 8417 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8418 | int major, minor; |
| 8419 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 8420 | return NULL; |
| 8421 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8422 | } |
| 8423 | #endif /* device macros */ |
| 8424 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8425 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8426 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8427 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8428 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8429 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8430 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8431 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8432 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8433 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8434 | int fd; |
| 8435 | off_t length; |
| 8436 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8437 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8438 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8439 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8440 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8441 | Py_BEGIN_ALLOW_THREADS |
| 8442 | res = ftruncate(fd, length); |
| 8443 | Py_END_ALLOW_THREADS |
| 8444 | if (res < 0) |
| 8445 | return posix_error(); |
| 8446 | Py_INCREF(Py_None); |
| 8447 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8448 | } |
| 8449 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8450 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8451 | #ifdef HAVE_TRUNCATE |
| 8452 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8453 | "truncate(path, length)\n\n\ |
| 8454 | Truncate the file given by path to length bytes."); |
| 8455 | |
| 8456 | static PyObject * |
| 8457 | posix_truncate(PyObject *self, PyObject *args) |
| 8458 | { |
| 8459 | PyObject *opath; |
| 8460 | const char *path; |
| 8461 | off_t length; |
| 8462 | int res; |
| 8463 | |
| 8464 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 8465 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 8466 | return NULL; |
| 8467 | path = PyBytes_AsString(opath); |
| 8468 | |
| 8469 | Py_BEGIN_ALLOW_THREADS |
| 8470 | res = truncate(path, length); |
| 8471 | Py_END_ALLOW_THREADS |
| 8472 | Py_DECREF(opath); |
| 8473 | if (res < 0) |
| 8474 | return posix_error(); |
| 8475 | Py_RETURN_NONE; |
| 8476 | } |
| 8477 | #endif |
| 8478 | |
| 8479 | #ifdef HAVE_POSIX_FALLOCATE |
| 8480 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8481 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8482 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8483 | starting from offset and continuing for len bytes."); |
| 8484 | |
| 8485 | static PyObject * |
| 8486 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8487 | { |
| 8488 | off_t len, offset; |
| 8489 | int res, fd; |
| 8490 | |
| 8491 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8492 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8493 | return NULL; |
| 8494 | |
| 8495 | Py_BEGIN_ALLOW_THREADS |
| 8496 | res = posix_fallocate(fd, offset, len); |
| 8497 | Py_END_ALLOW_THREADS |
| 8498 | if (res != 0) { |
| 8499 | errno = res; |
| 8500 | return posix_error(); |
| 8501 | } |
| 8502 | Py_RETURN_NONE; |
| 8503 | } |
| 8504 | #endif |
| 8505 | |
| 8506 | #ifdef HAVE_POSIX_FADVISE |
| 8507 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8508 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8509 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8510 | the kernel to make optimizations.\n\ |
| 8511 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8512 | offset and continuing for len bytes.\n\ |
| 8513 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8514 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8515 | POSIX_FADV_DONTNEED."); |
| 8516 | |
| 8517 | static PyObject * |
| 8518 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8519 | { |
| 8520 | off_t len, offset; |
| 8521 | int res, fd, advice; |
| 8522 | |
| 8523 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8524 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8525 | return NULL; |
| 8526 | |
| 8527 | Py_BEGIN_ALLOW_THREADS |
| 8528 | res = posix_fadvise(fd, offset, len, advice); |
| 8529 | Py_END_ALLOW_THREADS |
| 8530 | if (res != 0) { |
| 8531 | errno = res; |
| 8532 | return posix_error(); |
| 8533 | } |
| 8534 | Py_RETURN_NONE; |
| 8535 | } |
| 8536 | #endif |
| 8537 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8538 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8539 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8540 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8541 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8542 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8543 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8544 | * get re-set with another call for the same key. */ |
| 8545 | static PyObject *posix_putenv_garbage; |
| 8546 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8547 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8548 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8549 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8550 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8551 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8552 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8553 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8554 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8555 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8556 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8557 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8558 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8559 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8560 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8561 | if (newstr == NULL) { |
| 8562 | PyErr_NoMemory(); |
| 8563 | goto error; |
| 8564 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8565 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8566 | PyErr_Format(PyExc_ValueError, |
| 8567 | "the environment variable is longer than %u characters", |
| 8568 | _MAX_ENV); |
| 8569 | goto error; |
| 8570 | } |
| 8571 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8572 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8573 | if (newenv == NULL) |
| 8574 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8575 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8576 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8577 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8578 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8579 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8580 | PyObject *os1, *os2; |
| 8581 | char *s1, *s2; |
| 8582 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8583 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8584 | if (!PyArg_ParseTuple(args, |
| 8585 | "O&O&:putenv", |
| 8586 | PyUnicode_FSConverter, &os1, |
| 8587 | PyUnicode_FSConverter, &os2)) |
| 8588 | return NULL; |
| 8589 | s1 = PyBytes_AsString(os1); |
| 8590 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8591 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8592 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8593 | if (newstr == NULL) { |
| 8594 | PyErr_NoMemory(); |
| 8595 | goto error; |
| 8596 | } |
| 8597 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8598 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8599 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8600 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8601 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8602 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8603 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8604 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8605 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8606 | * this will cause previous value to be collected. This has to |
| 8607 | * happen after the real putenv() call because the old value |
| 8608 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8609 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8610 | /* really not much we can do; just leak */ |
| 8611 | PyErr_Clear(); |
| 8612 | } |
| 8613 | else { |
| 8614 | Py_DECREF(newstr); |
| 8615 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8616 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8617 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8618 | Py_DECREF(os1); |
| 8619 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8620 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8621 | Py_RETURN_NONE; |
| 8622 | |
| 8623 | error: |
| 8624 | #ifndef MS_WINDOWS |
| 8625 | Py_DECREF(os1); |
| 8626 | Py_DECREF(os2); |
| 8627 | #endif |
| 8628 | Py_XDECREF(newstr); |
| 8629 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8630 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8631 | #endif /* putenv */ |
| 8632 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8633 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8634 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8635 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8636 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8637 | |
| 8638 | static PyObject * |
| 8639 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8640 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8641 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8642 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8643 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8644 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8645 | |
| 8646 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8647 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8648 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8649 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8650 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8651 | #ifdef HAVE_BROKEN_UNSETENV |
| 8652 | unsetenv(PyBytes_AS_STRING(name)); |
| 8653 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8654 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8655 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8656 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8657 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8658 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8659 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8660 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8661 | /* Remove the key from posix_putenv_garbage; |
| 8662 | * this will cause it to be collected. This has to |
| 8663 | * happen after the real unsetenv() call because the |
| 8664 | * old value was still accessible until then. |
| 8665 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8666 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8667 | /* really not much we can do; just leak */ |
| 8668 | PyErr_Clear(); |
| 8669 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8670 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8671 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8672 | } |
| 8673 | #endif /* unsetenv */ |
| 8674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8675 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8676 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8677 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8678 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8679 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8680 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8681 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8682 | int code; |
| 8683 | char *message; |
| 8684 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8685 | return NULL; |
| 8686 | message = strerror(code); |
| 8687 | if (message == NULL) { |
| 8688 | PyErr_SetString(PyExc_ValueError, |
| 8689 | "strerror() argument out of range"); |
| 8690 | return NULL; |
| 8691 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8692 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8693 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8694 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8695 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8696 | #ifdef HAVE_SYS_WAIT_H |
| 8697 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8698 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8699 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8700 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8701 | 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] | 8702 | |
| 8703 | static PyObject * |
| 8704 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 8705 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8706 | WAIT_TYPE status; |
| 8707 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8708 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8709 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 8710 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8711 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8713 | } |
| 8714 | #endif /* WCOREDUMP */ |
| 8715 | |
| 8716 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8717 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8718 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8719 | 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] | 8720 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8721 | |
| 8722 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8723 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8724 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8725 | WAIT_TYPE status; |
| 8726 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8727 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8728 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 8729 | return NULL; |
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 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8732 | } |
| 8733 | #endif /* WIFCONTINUED */ |
| 8734 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8735 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8736 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8737 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8738 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8739 | |
| 8740 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8741 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8742 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8743 | WAIT_TYPE status; |
| 8744 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8745 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8746 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 8747 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8748 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8749 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8750 | } |
| 8751 | #endif /* WIFSTOPPED */ |
| 8752 | |
| 8753 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8754 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8755 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8756 | 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] | 8757 | |
| 8758 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8759 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8760 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8761 | WAIT_TYPE status; |
| 8762 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8763 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8764 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 8765 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8766 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8767 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8768 | } |
| 8769 | #endif /* WIFSIGNALED */ |
| 8770 | |
| 8771 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8772 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8773 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8774 | 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] | 8775 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8776 | |
| 8777 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8778 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8779 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8780 | WAIT_TYPE status; |
| 8781 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8782 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8783 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 8784 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8785 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8786 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8787 | } |
| 8788 | #endif /* WIFEXITED */ |
| 8789 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8790 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8791 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8792 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8793 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8794 | |
| 8795 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8796 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8797 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8798 | WAIT_TYPE status; |
| 8799 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8800 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8801 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8802 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8803 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8804 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8805 | } |
| 8806 | #endif /* WEXITSTATUS */ |
| 8807 | |
| 8808 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8809 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8810 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8811 | 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] | 8812 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8813 | |
| 8814 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8815 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8816 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8817 | WAIT_TYPE status; |
| 8818 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8819 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8820 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8821 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8822 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8823 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8824 | } |
| 8825 | #endif /* WTERMSIG */ |
| 8826 | |
| 8827 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8828 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8829 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8830 | Return the signal that stopped the process that provided\n\ |
| 8831 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8832 | |
| 8833 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8834 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8835 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8836 | WAIT_TYPE status; |
| 8837 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8838 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8839 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8840 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8841 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8842 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8843 | } |
| 8844 | #endif /* WSTOPSIG */ |
| 8845 | |
| 8846 | #endif /* HAVE_SYS_WAIT_H */ |
| 8847 | |
| 8848 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8849 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8850 | #ifdef _SCO_DS |
| 8851 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8852 | needed definitions in sys/statvfs.h */ |
| 8853 | #define _SVID3 |
| 8854 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8855 | #include <sys/statvfs.h> |
| 8856 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8857 | static PyObject* |
| 8858 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8859 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8860 | if (v == NULL) |
| 8861 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8862 | |
| 8863 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8864 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8865 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8866 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8867 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8868 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8869 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8870 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8871 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8872 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8873 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8874 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8875 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8876 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8877 | PyStructSequence_SET_ITEM(v, 2, |
| 8878 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8879 | PyStructSequence_SET_ITEM(v, 3, |
| 8880 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8881 | PyStructSequence_SET_ITEM(v, 4, |
| 8882 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8883 | PyStructSequence_SET_ITEM(v, 5, |
| 8884 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8885 | PyStructSequence_SET_ITEM(v, 6, |
| 8886 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8887 | PyStructSequence_SET_ITEM(v, 7, |
| 8888 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8889 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8890 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8891 | #endif |
| 8892 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8893 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8894 | } |
| 8895 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8896 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8897 | "fstatvfs(fd) -> statvfs result\n\n\ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8898 | Perform an fstatvfs system call on the given fd.\n\ |
| 8899 | Equivalent to statvfs(fd)."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8900 | |
| 8901 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8902 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8903 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8904 | int fd, res; |
| 8905 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8906 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8907 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8908 | return NULL; |
| 8909 | Py_BEGIN_ALLOW_THREADS |
| 8910 | res = fstatvfs(fd, &st); |
| 8911 | Py_END_ALLOW_THREADS |
| 8912 | if (res != 0) |
| 8913 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8914 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8915 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8916 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8917 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8918 | |
| 8919 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8920 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8921 | #include <sys/statvfs.h> |
| 8922 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8923 | PyDoc_STRVAR(posix_statvfs__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8924 | "statvfs(path)\n\n\ |
| 8925 | Perform a statvfs system call on the given path.\n\ |
| 8926 | \n\ |
| 8927 | path may always be specified as a string.\n\ |
| 8928 | On some platforms, path may also be specified as an open file descriptor.\n\ |
| 8929 | If this functionality is unavailable, using it raises an exception."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8930 | |
| 8931 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8932 | posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8933 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8934 | static char *keywords[] = {"path", NULL}; |
| 8935 | path_t path; |
| 8936 | int result; |
| 8937 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8938 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8939 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8940 | memset(&path, 0, sizeof(path)); |
| 8941 | #ifdef HAVE_FSTATVFS |
| 8942 | path.allow_fd = 1; |
| 8943 | #endif |
| 8944 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords, |
| 8945 | path_converter, &path |
| 8946 | )) |
| 8947 | return NULL; |
| 8948 | |
| 8949 | Py_BEGIN_ALLOW_THREADS |
| 8950 | #ifdef HAVE_FSTATVFS |
| 8951 | if (path.fd != -1) { |
| 8952 | #ifdef __APPLE__ |
| 8953 | /* handle weak-linking on Mac OS X 10.3 */ |
| 8954 | if (fstatvfs == NULL) { |
| 8955 | fd_specified("statvfs", path.fd); |
| 8956 | goto exit; |
| 8957 | } |
| 8958 | #endif |
| 8959 | result = fstatvfs(path.fd, &st); |
| 8960 | } |
| 8961 | else |
| 8962 | #endif |
| 8963 | result = statvfs(path.narrow, &st); |
| 8964 | Py_END_ALLOW_THREADS |
| 8965 | |
| 8966 | if (result) { |
| 8967 | return_value = path_posix_error("statvfs", &path); |
| 8968 | goto exit; |
| 8969 | } |
| 8970 | |
| 8971 | return_value = _pystatvfs_fromstructstatvfs(st); |
| 8972 | |
| 8973 | exit: |
| 8974 | path_cleanup(&path); |
| 8975 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8976 | } |
| 8977 | #endif /* HAVE_STATVFS */ |
| 8978 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8979 | #ifdef MS_WINDOWS |
| 8980 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8981 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8982 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8983 | |
| 8984 | static PyObject * |
| 8985 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8986 | { |
| 8987 | BOOL retval; |
| 8988 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8989 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8990 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8991 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8992 | return NULL; |
| 8993 | |
| 8994 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8995 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8996 | Py_END_ALLOW_THREADS |
| 8997 | if (retval == 0) |
| 8998 | return PyErr_SetFromWindowsErr(0); |
| 8999 | |
| 9000 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 9001 | } |
| 9002 | #endif |
| 9003 | |
| 9004 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9005 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 9006 | * It maps strings representing configuration variable names to |
| 9007 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9008 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9009 | * rarely-used constants. There are three separate tables that use |
| 9010 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9011 | * |
| 9012 | * This code is always included, even if none of the interfaces that |
| 9013 | * need it are included. The #if hackery needed to avoid it would be |
| 9014 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9015 | */ |
| 9016 | struct constdef { |
| 9017 | char *name; |
| 9018 | long value; |
| 9019 | }; |
| 9020 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9021 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9022 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 9023 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9024 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9025 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9026 | *valuep = PyLong_AS_LONG(arg); |
| 9027 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9028 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 9029 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9030 | /* look up the value in the table using a binary search */ |
| 9031 | size_t lo = 0; |
| 9032 | size_t mid; |
| 9033 | size_t hi = tablesize; |
| 9034 | int cmp; |
| 9035 | const char *confname; |
| 9036 | if (!PyUnicode_Check(arg)) { |
| 9037 | PyErr_SetString(PyExc_TypeError, |
| 9038 | "configuration names must be strings or integers"); |
| 9039 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9040 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9041 | confname = _PyUnicode_AsString(arg); |
| 9042 | if (confname == NULL) |
| 9043 | return 0; |
| 9044 | while (lo < hi) { |
| 9045 | mid = (lo + hi) / 2; |
| 9046 | cmp = strcmp(confname, table[mid].name); |
| 9047 | if (cmp < 0) |
| 9048 | hi = mid; |
| 9049 | else if (cmp > 0) |
| 9050 | lo = mid + 1; |
| 9051 | else { |
| 9052 | *valuep = table[mid].value; |
| 9053 | return 1; |
| 9054 | } |
| 9055 | } |
| 9056 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 9057 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9058 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9059 | } |
| 9060 | |
| 9061 | |
| 9062 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 9063 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9064 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9065 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9066 | #endif |
| 9067 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9068 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9069 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9070 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9071 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9072 | #endif |
| 9073 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9074 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9075 | #endif |
| 9076 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9078 | #endif |
| 9079 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9080 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9081 | #endif |
| 9082 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9084 | #endif |
| 9085 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9086 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9087 | #endif |
| 9088 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9089 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9090 | #endif |
| 9091 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9092 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9093 | #endif |
| 9094 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9095 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9096 | #endif |
| 9097 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9098 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9099 | #endif |
| 9100 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9101 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9102 | #endif |
| 9103 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9104 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9105 | #endif |
| 9106 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9107 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9108 | #endif |
| 9109 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9110 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9111 | #endif |
| 9112 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9113 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9114 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 9115 | #ifdef _PC_ACL_ENABLED |
| 9116 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 9117 | #endif |
| 9118 | #ifdef _PC_MIN_HOLE_SIZE |
| 9119 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 9120 | #endif |
| 9121 | #ifdef _PC_ALLOC_SIZE_MIN |
| 9122 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 9123 | #endif |
| 9124 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 9125 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 9126 | #endif |
| 9127 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 9128 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 9129 | #endif |
| 9130 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 9131 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 9132 | #endif |
| 9133 | #ifdef _PC_REC_XFER_ALIGN |
| 9134 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 9135 | #endif |
| 9136 | #ifdef _PC_SYMLINK_MAX |
| 9137 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 9138 | #endif |
| 9139 | #ifdef _PC_XATTR_ENABLED |
| 9140 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 9141 | #endif |
| 9142 | #ifdef _PC_XATTR_EXISTS |
| 9143 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 9144 | #endif |
| 9145 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 9146 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 9147 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9148 | }; |
| 9149 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9150 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9151 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9152 | { |
| 9153 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 9154 | sizeof(posix_constants_pathconf) |
| 9155 | / sizeof(struct constdef)); |
| 9156 | } |
| 9157 | #endif |
| 9158 | |
| 9159 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9160 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9161 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9162 | 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] | 9163 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9164 | |
| 9165 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9166 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9167 | { |
| 9168 | PyObject *result = NULL; |
| 9169 | int name, fd; |
| 9170 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 9171 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 9172 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9173 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9174 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9175 | errno = 0; |
| 9176 | limit = fpathconf(fd, name); |
| 9177 | if (limit == -1 && errno != 0) |
| 9178 | posix_error(); |
| 9179 | else |
| 9180 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9181 | } |
| 9182 | return result; |
| 9183 | } |
| 9184 | #endif |
| 9185 | |
| 9186 | |
| 9187 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9188 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9189 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9190 | Return the configuration limit name for the file or directory path.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9191 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9192 | |
| 9193 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9194 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9195 | { |
| 9196 | PyObject *result = NULL; |
| 9197 | int name; |
| 9198 | char *path; |
| 9199 | |
| 9200 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 9201 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9202 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9203 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9204 | errno = 0; |
| 9205 | limit = pathconf(path, name); |
| 9206 | if (limit == -1 && errno != 0) { |
| 9207 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9208 | /* could be a path or name problem */ |
| 9209 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9210 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9211 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9212 | } |
| 9213 | else |
| 9214 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9215 | } |
| 9216 | return result; |
| 9217 | } |
| 9218 | #endif |
| 9219 | |
| 9220 | #ifdef HAVE_CONFSTR |
| 9221 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9222 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9223 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9224 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9225 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9226 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9227 | #endif |
| 9228 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9229 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 9230 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9231 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9232 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9233 | #endif |
| 9234 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9235 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9236 | #endif |
| 9237 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9238 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9239 | #endif |
| 9240 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9241 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9242 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9243 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9244 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9245 | #endif |
| 9246 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9247 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9248 | #endif |
| 9249 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9250 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9251 | #endif |
| 9252 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9253 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9254 | #endif |
| 9255 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9256 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9257 | #endif |
| 9258 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9259 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9260 | #endif |
| 9261 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9262 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9263 | #endif |
| 9264 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9265 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9266 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9267 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9268 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9269 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9270 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9271 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9272 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9273 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9274 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9275 | #endif |
| 9276 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9277 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9278 | #endif |
| 9279 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9280 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9281 | #endif |
| 9282 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9283 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9284 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9285 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9286 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9287 | #endif |
| 9288 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9289 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9290 | #endif |
| 9291 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9292 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9293 | #endif |
| 9294 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9295 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9296 | #endif |
| 9297 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9298 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9299 | #endif |
| 9300 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9301 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9302 | #endif |
| 9303 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9304 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9305 | #endif |
| 9306 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9307 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9308 | #endif |
| 9309 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9310 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9311 | #endif |
| 9312 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9313 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9314 | #endif |
| 9315 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9316 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9317 | #endif |
| 9318 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9319 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9320 | #endif |
| 9321 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9322 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9323 | #endif |
| 9324 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9325 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9326 | #endif |
| 9327 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9328 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9329 | #endif |
| 9330 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9331 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9332 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9333 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9334 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9335 | #endif |
| 9336 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9337 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9338 | #endif |
| 9339 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9340 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9341 | #endif |
| 9342 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9343 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9344 | #endif |
| 9345 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9346 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9347 | #endif |
| 9348 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9349 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9350 | #endif |
| 9351 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9352 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9353 | #endif |
| 9354 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9355 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9356 | #endif |
| 9357 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9358 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9359 | #endif |
| 9360 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9361 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9362 | #endif |
| 9363 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9364 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9365 | #endif |
| 9366 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9367 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9368 | #endif |
| 9369 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9370 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9371 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9372 | }; |
| 9373 | |
| 9374 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9375 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9376 | { |
| 9377 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 9378 | sizeof(posix_constants_confstr) |
| 9379 | / sizeof(struct constdef)); |
| 9380 | } |
| 9381 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9382 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9383 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9384 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9385 | |
| 9386 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9387 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9388 | { |
| 9389 | PyObject *result = NULL; |
| 9390 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9391 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 9392 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9393 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9394 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 9395 | return NULL; |
| 9396 | |
| 9397 | errno = 0; |
| 9398 | len = confstr(name, buffer, sizeof(buffer)); |
| 9399 | if (len == 0) { |
| 9400 | if (errno) { |
| 9401 | posix_error(); |
| 9402 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9403 | } |
| 9404 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9405 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9406 | } |
| 9407 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 9408 | |
| 9409 | if ((unsigned int)len >= sizeof(buffer)) { |
| 9410 | char *buf = PyMem_Malloc(len); |
| 9411 | if (buf == NULL) |
| 9412 | return PyErr_NoMemory(); |
| 9413 | confstr(name, buf, len); |
| 9414 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 9415 | PyMem_Free(buf); |
| 9416 | } |
| 9417 | else |
| 9418 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9419 | return result; |
| 9420 | } |
| 9421 | #endif |
| 9422 | |
| 9423 | |
| 9424 | #ifdef HAVE_SYSCONF |
| 9425 | static struct constdef posix_constants_sysconf[] = { |
| 9426 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9427 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9428 | #endif |
| 9429 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9430 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9431 | #endif |
| 9432 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9433 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9434 | #endif |
| 9435 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9436 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9437 | #endif |
| 9438 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9439 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9440 | #endif |
| 9441 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9442 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9443 | #endif |
| 9444 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9445 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9446 | #endif |
| 9447 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9448 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9449 | #endif |
| 9450 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9451 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9452 | #endif |
| 9453 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9454 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9455 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9456 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9457 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9458 | #endif |
| 9459 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9460 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9461 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9462 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9463 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9464 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9465 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9466 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9467 | #endif |
| 9468 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9469 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9470 | #endif |
| 9471 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9472 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9473 | #endif |
| 9474 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9475 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9476 | #endif |
| 9477 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9478 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9479 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9480 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9481 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9482 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9483 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9484 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9485 | #endif |
| 9486 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9487 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9488 | #endif |
| 9489 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9490 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9491 | #endif |
| 9492 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9493 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9494 | #endif |
| 9495 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9496 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9497 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9498 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9499 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9500 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9501 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9502 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9503 | #endif |
| 9504 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9505 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9506 | #endif |
| 9507 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9508 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9509 | #endif |
| 9510 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9511 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9512 | #endif |
| 9513 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9514 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9515 | #endif |
| 9516 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9517 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9518 | #endif |
| 9519 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9520 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9521 | #endif |
| 9522 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9523 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9524 | #endif |
| 9525 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9526 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9527 | #endif |
| 9528 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9529 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9530 | #endif |
| 9531 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9532 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9533 | #endif |
| 9534 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9535 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9536 | #endif |
| 9537 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9538 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9539 | #endif |
| 9540 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9541 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9542 | #endif |
| 9543 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9544 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9545 | #endif |
| 9546 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9547 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9548 | #endif |
| 9549 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9550 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9551 | #endif |
| 9552 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9553 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9554 | #endif |
| 9555 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9556 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9557 | #endif |
| 9558 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9559 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9560 | #endif |
| 9561 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9562 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9563 | #endif |
| 9564 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9565 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9566 | #endif |
| 9567 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9568 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9569 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9570 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9571 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9572 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9573 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9574 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9575 | #endif |
| 9576 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9577 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9578 | #endif |
| 9579 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9580 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9581 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9582 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9583 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9584 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9585 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9586 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9587 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9588 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9589 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9590 | #endif |
| 9591 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9592 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9593 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9594 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9595 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9596 | #endif |
| 9597 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9598 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9599 | #endif |
| 9600 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9601 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9602 | #endif |
| 9603 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9604 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9605 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9606 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9607 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9608 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9609 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9610 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9611 | #endif |
| 9612 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9613 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9614 | #endif |
| 9615 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9616 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9617 | #endif |
| 9618 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9619 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9620 | #endif |
| 9621 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9622 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9623 | #endif |
| 9624 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9625 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9626 | #endif |
| 9627 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9628 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9629 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9630 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9631 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9632 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9633 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9634 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9635 | #endif |
| 9636 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9637 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9638 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9639 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9640 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9641 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9642 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9643 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9644 | #endif |
| 9645 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9646 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9647 | #endif |
| 9648 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9649 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9650 | #endif |
| 9651 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9652 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9653 | #endif |
| 9654 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9655 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9656 | #endif |
| 9657 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9658 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9659 | #endif |
| 9660 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9661 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9662 | #endif |
| 9663 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9664 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9665 | #endif |
| 9666 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9667 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9668 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9669 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9670 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9671 | #endif |
| 9672 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9673 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9674 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9675 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9676 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9677 | #endif |
| 9678 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9679 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9680 | #endif |
| 9681 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9682 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9683 | #endif |
| 9684 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9685 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9686 | #endif |
| 9687 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9688 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9689 | #endif |
| 9690 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9691 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9692 | #endif |
| 9693 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9694 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9695 | #endif |
| 9696 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9697 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9698 | #endif |
| 9699 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9700 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9701 | #endif |
| 9702 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9703 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9704 | #endif |
| 9705 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9706 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9707 | #endif |
| 9708 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9709 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9710 | #endif |
| 9711 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9712 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9713 | #endif |
| 9714 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9715 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9716 | #endif |
| 9717 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9718 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9719 | #endif |
| 9720 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9721 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9722 | #endif |
| 9723 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9724 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9725 | #endif |
| 9726 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9727 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9728 | #endif |
| 9729 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9730 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9731 | #endif |
| 9732 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9733 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9734 | #endif |
| 9735 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9736 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9737 | #endif |
| 9738 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9739 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9740 | #endif |
| 9741 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9742 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9743 | #endif |
| 9744 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9745 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9746 | #endif |
| 9747 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9748 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9749 | #endif |
| 9750 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9751 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9752 | #endif |
| 9753 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9754 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9755 | #endif |
| 9756 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9757 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9758 | #endif |
| 9759 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9760 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9761 | #endif |
| 9762 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9763 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9764 | #endif |
| 9765 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9766 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9767 | #endif |
| 9768 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9769 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9770 | #endif |
| 9771 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9772 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9773 | #endif |
| 9774 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9775 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9776 | #endif |
| 9777 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9778 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9779 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9780 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9781 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9782 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9783 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9784 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9785 | #endif |
| 9786 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9787 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9788 | #endif |
| 9789 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9790 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9791 | #endif |
| 9792 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9793 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9794 | #endif |
| 9795 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9796 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9797 | #endif |
| 9798 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9799 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9800 | #endif |
| 9801 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9802 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9803 | #endif |
| 9804 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9805 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9806 | #endif |
| 9807 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9808 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9809 | #endif |
| 9810 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9811 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9812 | #endif |
| 9813 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9814 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9815 | #endif |
| 9816 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9817 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9818 | #endif |
| 9819 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9820 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9821 | #endif |
| 9822 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9823 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9824 | #endif |
| 9825 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9826 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9827 | #endif |
| 9828 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9829 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9830 | #endif |
| 9831 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9832 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9833 | #endif |
| 9834 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9835 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9836 | #endif |
| 9837 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9838 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9839 | #endif |
| 9840 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9841 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9842 | #endif |
| 9843 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9844 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9845 | #endif |
| 9846 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9847 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9848 | #endif |
| 9849 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9850 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9851 | #endif |
| 9852 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9853 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9854 | #endif |
| 9855 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9856 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9857 | #endif |
| 9858 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9859 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9860 | #endif |
| 9861 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9862 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9863 | #endif |
| 9864 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9865 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9866 | #endif |
| 9867 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9868 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9869 | #endif |
| 9870 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9871 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9872 | #endif |
| 9873 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9874 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9875 | #endif |
| 9876 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9877 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9878 | #endif |
| 9879 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9880 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9881 | #endif |
| 9882 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9883 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9884 | #endif |
| 9885 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9886 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9887 | #endif |
| 9888 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9889 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9890 | #endif |
| 9891 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9892 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9893 | #endif |
| 9894 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9895 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9896 | #endif |
| 9897 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9898 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9899 | #endif |
| 9900 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9901 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9902 | #endif |
| 9903 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9904 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9905 | #endif |
| 9906 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9907 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9908 | #endif |
| 9909 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9910 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9911 | #endif |
| 9912 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9913 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9914 | #endif |
| 9915 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9916 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9917 | #endif |
| 9918 | }; |
| 9919 | |
| 9920 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9921 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9922 | { |
| 9923 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9924 | sizeof(posix_constants_sysconf) |
| 9925 | / sizeof(struct constdef)); |
| 9926 | } |
| 9927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9928 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9929 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9930 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9931 | |
| 9932 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9933 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9934 | { |
| 9935 | PyObject *result = NULL; |
| 9936 | int name; |
| 9937 | |
| 9938 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9939 | int value; |
| 9940 | |
| 9941 | errno = 0; |
| 9942 | value = sysconf(name); |
| 9943 | if (value == -1 && errno != 0) |
| 9944 | posix_error(); |
| 9945 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9946 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9947 | } |
| 9948 | return result; |
| 9949 | } |
| 9950 | #endif |
| 9951 | |
| 9952 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9953 | /* This code is used to ensure that the tables of configuration value names |
| 9954 | * are in sorted order as required by conv_confname(), and also to build the |
| 9955 | * the exported dictionaries that are used to publish information about the |
| 9956 | * names available on the host platform. |
| 9957 | * |
| 9958 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9959 | * when used, even for platforms we're not able to test on. It also makes |
| 9960 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9961 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9962 | |
| 9963 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9964 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9965 | { |
| 9966 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9967 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9968 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9969 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9970 | |
| 9971 | return strcmp(c1->name, c2->name); |
| 9972 | } |
| 9973 | |
| 9974 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9975 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9976 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9977 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9978 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9979 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9980 | |
| 9981 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9982 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9983 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9984 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9985 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9986 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9987 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9988 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9989 | Py_XDECREF(o); |
| 9990 | Py_DECREF(d); |
| 9991 | return -1; |
| 9992 | } |
| 9993 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9994 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9995 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9996 | } |
| 9997 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9998 | /* Return -1 on failure, 0 on success. */ |
| 9999 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10000 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10001 | { |
| 10002 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10003 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10004 | sizeof(posix_constants_pathconf) |
| 10005 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10006 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10007 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10008 | #endif |
| 10009 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10010 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10011 | sizeof(posix_constants_confstr) |
| 10012 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10013 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10014 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10015 | #endif |
| 10016 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10017 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10018 | sizeof(posix_constants_sysconf) |
| 10019 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10020 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10021 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10022 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10023 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10024 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10025 | |
| 10026 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10027 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 10028 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10029 | 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] | 10030 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10031 | |
| 10032 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10033 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10034 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10035 | abort(); |
| 10036 | /*NOTREACHED*/ |
| 10037 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 10038 | return NULL; |
| 10039 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10040 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10041 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10042 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10043 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 10044 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10045 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 10046 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 10047 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 10048 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 10049 | application (if any) its extension is associated.\n\ |
| 10050 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 10051 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10052 | \n\ |
| 10053 | startfile returns as soon as the associated application is launched.\n\ |
| 10054 | There is no option to wait for the application to close, and no way\n\ |
| 10055 | to retrieve the application's exit status.\n\ |
| 10056 | \n\ |
| 10057 | The filepath is relative to the current directory. If you want to use\n\ |
| 10058 | 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] | 10059 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10060 | |
| 10061 | static PyObject * |
| 10062 | win32_startfile(PyObject *self, PyObject *args) |
| 10063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10064 | PyObject *ofilepath; |
| 10065 | char *filepath; |
| 10066 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10067 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10068 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 10069 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10070 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10071 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 10072 | &unipath, &operation)) { |
| 10073 | PyErr_Clear(); |
| 10074 | goto normal; |
| 10075 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10076 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10077 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10078 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10079 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10080 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10081 | PyErr_Clear(); |
| 10082 | operation = NULL; |
| 10083 | goto normal; |
| 10084 | } |
| 10085 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 10086 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10087 | wpath = PyUnicode_AsUnicode(unipath); |
| 10088 | if (wpath == NULL) |
| 10089 | goto normal; |
| 10090 | if (uoperation) { |
| 10091 | woperation = PyUnicode_AsUnicode(uoperation); |
| 10092 | if (woperation == NULL) |
| 10093 | goto normal; |
| 10094 | } |
| 10095 | else |
| 10096 | woperation = NULL; |
| 10097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10098 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10099 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 10100 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10101 | Py_END_ALLOW_THREADS |
| 10102 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10103 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10104 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10105 | win32_error_object("startfile", unipath); |
| 10106 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10107 | } |
| 10108 | Py_INCREF(Py_None); |
| 10109 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10110 | |
| 10111 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10112 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 10113 | PyUnicode_FSConverter, &ofilepath, |
| 10114 | &operation)) |
| 10115 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 10116 | if (win32_warn_bytes_api()) { |
| 10117 | Py_DECREF(ofilepath); |
| 10118 | return NULL; |
| 10119 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10120 | filepath = PyBytes_AsString(ofilepath); |
| 10121 | Py_BEGIN_ALLOW_THREADS |
| 10122 | rc = ShellExecute((HWND)0, operation, filepath, |
| 10123 | NULL, NULL, SW_SHOWNORMAL); |
| 10124 | Py_END_ALLOW_THREADS |
| 10125 | if (rc <= (HINSTANCE)32) { |
| 10126 | PyObject *errval = win32_error("startfile", filepath); |
| 10127 | Py_DECREF(ofilepath); |
| 10128 | return errval; |
| 10129 | } |
| 10130 | Py_DECREF(ofilepath); |
| 10131 | Py_INCREF(Py_None); |
| 10132 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 10133 | } |
| 10134 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10135 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10136 | #ifdef HAVE_GETLOADAVG |
| 10137 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 10138 | "getloadavg() -> (float, float, float)\n\n\ |
| 10139 | Return the number of processes in the system run queue averaged over\n\ |
| 10140 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 10141 | was unobtainable"); |
| 10142 | |
| 10143 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10144 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10145 | { |
| 10146 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10147 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10148 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 10149 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10150 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10151 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10152 | } |
| 10153 | #endif |
| 10154 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10155 | PyDoc_STRVAR(device_encoding__doc__, |
| 10156 | "device_encoding(fd) -> str\n\n\ |
| 10157 | Return a string describing the encoding of the device\n\ |
| 10158 | if the output is a terminal; else return None."); |
| 10159 | |
| 10160 | static PyObject * |
| 10161 | device_encoding(PyObject *self, PyObject *args) |
| 10162 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10163 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10164 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10165 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 10166 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10167 | |
| 10168 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 10169 | } |
| 10170 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10171 | #ifdef __VMS |
| 10172 | /* Use openssl random routine */ |
| 10173 | #include <openssl/rand.h> |
| 10174 | PyDoc_STRVAR(vms_urandom__doc__, |
| 10175 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 10176 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10177 | |
| 10178 | static PyObject* |
| 10179 | vms_urandom(PyObject *self, PyObject *args) |
| 10180 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10181 | int howMany; |
| 10182 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10184 | /* Read arguments */ |
| 10185 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 10186 | return NULL; |
| 10187 | if (howMany < 0) |
| 10188 | return PyErr_Format(PyExc_ValueError, |
| 10189 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10190 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10191 | /* Allocate bytes */ |
| 10192 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 10193 | if (result != NULL) { |
| 10194 | /* Get random data */ |
| 10195 | if (RAND_pseudo_bytes((unsigned char*) |
| 10196 | PyBytes_AS_STRING(result), |
| 10197 | howMany) < 0) { |
| 10198 | Py_DECREF(result); |
| 10199 | return PyErr_Format(PyExc_ValueError, |
| 10200 | "RAND_pseudo_bytes"); |
| 10201 | } |
| 10202 | } |
| 10203 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10204 | } |
| 10205 | #endif |
| 10206 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10207 | #ifdef HAVE_SETRESUID |
| 10208 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 10209 | "setresuid(ruid, euid, suid)\n\n\ |
| 10210 | Set the current process's real, effective, and saved user ids."); |
| 10211 | |
| 10212 | static PyObject* |
| 10213 | posix_setresuid (PyObject *self, PyObject *args) |
| 10214 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10215 | /* We assume uid_t is no larger than a long. */ |
| 10216 | long ruid, euid, suid; |
| 10217 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 10218 | return NULL; |
| 10219 | if (setresuid(ruid, euid, suid) < 0) |
| 10220 | return posix_error(); |
| 10221 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10222 | } |
| 10223 | #endif |
| 10224 | |
| 10225 | #ifdef HAVE_SETRESGID |
| 10226 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 10227 | "setresgid(rgid, egid, sgid)\n\n\ |
| 10228 | Set the current process's real, effective, and saved group ids."); |
| 10229 | |
| 10230 | static PyObject* |
| 10231 | posix_setresgid (PyObject *self, PyObject *args) |
| 10232 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10233 | /* We assume uid_t is no larger than a long. */ |
| 10234 | long rgid, egid, sgid; |
| 10235 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 10236 | return NULL; |
| 10237 | if (setresgid(rgid, egid, sgid) < 0) |
| 10238 | return posix_error(); |
| 10239 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10240 | } |
| 10241 | #endif |
| 10242 | |
| 10243 | #ifdef HAVE_GETRESUID |
| 10244 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 10245 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 10246 | Get tuple of the current process's real, effective, and saved user ids."); |
| 10247 | |
| 10248 | static PyObject* |
| 10249 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 10250 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10251 | uid_t ruid, euid, suid; |
| 10252 | long l_ruid, l_euid, l_suid; |
| 10253 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 10254 | return posix_error(); |
| 10255 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 10256 | l_ruid = ruid; |
| 10257 | l_euid = euid; |
| 10258 | l_suid = suid; |
| 10259 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10260 | } |
| 10261 | #endif |
| 10262 | |
| 10263 | #ifdef HAVE_GETRESGID |
| 10264 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 10265 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 10266 | 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] | 10267 | |
| 10268 | static PyObject* |
| 10269 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 10270 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10271 | uid_t rgid, egid, sgid; |
| 10272 | long l_rgid, l_egid, l_sgid; |
| 10273 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 10274 | return posix_error(); |
| 10275 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 10276 | l_rgid = rgid; |
| 10277 | l_egid = egid; |
| 10278 | l_sgid = sgid; |
| 10279 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10280 | } |
| 10281 | #endif |
| 10282 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10283 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10284 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10285 | PyDoc_STRVAR(posix_getxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10286 | "getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\ |
| 10287 | Return the value of extended attribute attribute on path.\n\ |
| 10288 | \n\ |
| 10289 | path may be either a string or an open file descriptor.\n\ |
| 10290 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10291 | link, getxattr will examine the symbolic link itself instead of the file\n\ |
| 10292 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10293 | |
| 10294 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10295 | posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
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 | path_t path; |
| 10298 | path_t attribute; |
| 10299 | int follow_symlinks = 1; |
| 10300 | PyObject *buffer = NULL; |
| 10301 | int i; |
| 10302 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10303 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10304 | memset(&path, 0, sizeof(path)); |
| 10305 | memset(&attribute, 0, sizeof(attribute)); |
| 10306 | path.allow_fd = 1; |
| 10307 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords, |
| 10308 | path_converter, &path, |
| 10309 | path_converter, &attribute, |
| 10310 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10311 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10312 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10313 | if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks)) |
| 10314 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10315 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10316 | for (i = 0; ; i++) { |
| 10317 | void *ptr; |
| 10318 | ssize_t result; |
| 10319 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 10320 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10321 | if (!buffer_size) { |
| 10322 | path_error("getxattr", &path); |
| 10323 | goto exit; |
| 10324 | } |
| 10325 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 10326 | if (!buffer) |
| 10327 | goto exit; |
| 10328 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10329 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10330 | Py_BEGIN_ALLOW_THREADS; |
| 10331 | if (path.fd >= 0) |
| 10332 | result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size); |
| 10333 | else if (follow_symlinks) |
| 10334 | result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10335 | else |
| 10336 | result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size); |
| 10337 | Py_END_ALLOW_THREADS; |
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 | if (result < 0) { |
| 10340 | Py_DECREF(buffer); |
| 10341 | buffer = NULL; |
| 10342 | if (errno == ERANGE) |
| 10343 | continue; |
| 10344 | path_error("getxattr", &path); |
| 10345 | goto exit; |
| 10346 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10347 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10348 | if (result != buffer_size) { |
| 10349 | /* Can only shrink. */ |
| 10350 | _PyBytes_Resize(&buffer, result); |
| 10351 | } |
| 10352 | break; |
| 10353 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10354 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10355 | exit: |
| 10356 | path_cleanup(&path); |
| 10357 | path_cleanup(&attribute); |
| 10358 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10359 | } |
| 10360 | |
| 10361 | PyDoc_STRVAR(posix_setxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10362 | "setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\ |
| 10363 | Set extended attribute attribute on path to value.\n\ |
| 10364 | path may be either a string or an open file descriptor.\n\ |
| 10365 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10366 | link, setxattr will modify the symbolic link itself instead of the file\n\ |
| 10367 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10368 | |
| 10369 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10370 | posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10371 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10372 | path_t path; |
| 10373 | path_t attribute; |
| 10374 | Py_buffer value; |
| 10375 | int flags = 0; |
| 10376 | int follow_symlinks = 1; |
| 10377 | int result; |
| 10378 | PyObject *return_value = NULL; |
| 10379 | static char *keywords[] = {"path", "attribute", "value", |
| 10380 | "flags", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10381 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10382 | memset(&path, 0, sizeof(path)); |
| 10383 | path.allow_fd = 1; |
| 10384 | memset(&attribute, 0, sizeof(attribute)); |
| 10385 | memset(&value, 0, sizeof(value)); |
| 10386 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", |
| 10387 | keywords, |
| 10388 | path_converter, &path, |
| 10389 | path_converter, &attribute, |
| 10390 | &value, &flags, |
| 10391 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10392 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10393 | |
| 10394 | if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks)) |
| 10395 | goto exit; |
| 10396 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10397 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10398 | if (path.fd > -1) |
| 10399 | result = fsetxattr(path.fd, attribute.narrow, |
| 10400 | value.buf, value.len, flags); |
| 10401 | else if (follow_symlinks) |
| 10402 | result = setxattr(path.narrow, attribute.narrow, |
| 10403 | value.buf, value.len, flags); |
| 10404 | else |
| 10405 | result = lsetxattr(path.narrow, attribute.narrow, |
| 10406 | value.buf, value.len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10407 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10408 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10409 | if (result) { |
| 10410 | return_value = path_error("setxattr", &path); |
| 10411 | goto exit; |
| 10412 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10413 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10414 | return_value = Py_None; |
| 10415 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10416 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10417 | exit: |
| 10418 | path_cleanup(&path); |
| 10419 | path_cleanup(&attribute); |
| 10420 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10421 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10422 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10423 | } |
| 10424 | |
| 10425 | PyDoc_STRVAR(posix_removexattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10426 | "removexattr(path, attribute, *, follow_symlinks=True)\n\n\ |
| 10427 | Remove extended attribute attribute on path.\n\ |
| 10428 | path may be either a string or an open file descriptor.\n\ |
| 10429 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10430 | link, removexattr will modify the symbolic link itself instead of the file\n\ |
| 10431 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10432 | |
| 10433 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10434 | posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10435 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10436 | path_t path; |
| 10437 | path_t attribute; |
| 10438 | int follow_symlinks = 1; |
| 10439 | int result; |
| 10440 | PyObject *return_value = NULL; |
| 10441 | static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10442 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10443 | memset(&path, 0, sizeof(path)); |
| 10444 | memset(&attribute, 0, sizeof(attribute)); |
| 10445 | path.allow_fd = 1; |
| 10446 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", |
| 10447 | keywords, |
| 10448 | path_converter, &path, |
| 10449 | path_converter, &attribute, |
| 10450 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10451 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10452 | |
| 10453 | if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks)) |
| 10454 | goto exit; |
| 10455 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10456 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10457 | if (path.fd > -1) |
| 10458 | result = fremovexattr(path.fd, attribute.narrow); |
| 10459 | else if (follow_symlinks) |
| 10460 | result = removexattr(path.narrow, attribute.narrow); |
| 10461 | else |
| 10462 | result = lremovexattr(path.narrow, attribute.narrow); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10463 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10464 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10465 | if (result) { |
| 10466 | return_value = path_error("removexattr", &path); |
| 10467 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10468 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10469 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10470 | return_value = Py_None; |
| 10471 | Py_INCREF(return_value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10472 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10473 | exit: |
| 10474 | path_cleanup(&path); |
| 10475 | path_cleanup(&attribute); |
| 10476 | |
| 10477 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10478 | } |
| 10479 | |
| 10480 | PyDoc_STRVAR(posix_listxattr__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10481 | "listxattr(path='.', *, follow_symlinks=True)\n\n\ |
| 10482 | Return a list of extended attributes on path.\n\ |
| 10483 | \n\ |
| 10484 | path may be either None, a string, or an open file descriptor.\n\ |
| 10485 | if path is None, listxattr will examine the current directory.\n\ |
| 10486 | If follow_symlinks is False, and the last element of the path is a symbolic\n\ |
| 10487 | link, listxattr will examine the symbolic link itself instead of the file\n\ |
| 10488 | the link points to."); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10489 | |
| 10490 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10491 | posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs) |
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 | path_t path; |
| 10494 | int follow_symlinks = 1; |
| 10495 | Py_ssize_t i; |
| 10496 | PyObject *result = NULL; |
| 10497 | char *buffer = NULL; |
| 10498 | char *name; |
| 10499 | static char *keywords[] = {"path", "follow_symlinks", NULL}; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10500 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10501 | memset(&path, 0, sizeof(path)); |
| 10502 | path.allow_fd = 1; |
| 10503 | path.fd = -1; |
| 10504 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords, |
| 10505 | path_converter, &path, |
| 10506 | &follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10507 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10508 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10509 | if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks)) |
| 10510 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10511 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10512 | name = path.narrow ? path.narrow : "."; |
| 10513 | for (i = 0; ; i++) { |
| 10514 | char *start, *trace, *end; |
| 10515 | ssize_t length; |
| 10516 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 10517 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 10518 | if (!buffer_size) { |
| 10519 | // ERANGE |
| 10520 | path_error("listxattr", &path); |
| 10521 | break; |
| 10522 | } |
| 10523 | buffer = PyMem_MALLOC(buffer_size); |
| 10524 | if (!buffer) { |
| 10525 | PyErr_NoMemory(); |
| 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 | Py_BEGIN_ALLOW_THREADS; |
| 10530 | if (path.fd > -1) |
| 10531 | length = flistxattr(path.fd, buffer, buffer_size); |
| 10532 | else if (follow_symlinks) |
| 10533 | length = listxattr(name, buffer, buffer_size); |
| 10534 | else |
| 10535 | length = llistxattr(name, buffer, buffer_size); |
| 10536 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10537 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10538 | if (length < 0) { |
| 10539 | if (errno == ERANGE) |
| 10540 | continue; |
| 10541 | path_error("listxattr", &path); |
| 10542 | break; |
| 10543 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10544 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10545 | result = PyList_New(0); |
| 10546 | if (!result) { |
| 10547 | goto exit; |
| 10548 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10549 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10550 | end = buffer + length; |
| 10551 | for (trace = start = buffer; trace != end; trace++) { |
| 10552 | if (!*trace) { |
| 10553 | int error; |
| 10554 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 10555 | trace - start); |
| 10556 | if (!attribute) { |
| 10557 | Py_DECREF(result); |
| 10558 | result = NULL; |
| 10559 | goto exit; |
| 10560 | } |
| 10561 | error = PyList_Append(result, attribute); |
| 10562 | Py_DECREF(attribute); |
| 10563 | if (error) { |
| 10564 | Py_DECREF(result); |
| 10565 | result = NULL; |
| 10566 | goto exit; |
| 10567 | } |
| 10568 | start = trace + 1; |
| 10569 | } |
| 10570 | } |
| 10571 | break; |
| 10572 | } |
| 10573 | exit: |
| 10574 | path_cleanup(&path); |
| 10575 | if (buffer) |
| 10576 | PyMem_FREE(buffer); |
| 10577 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10578 | } |
| 10579 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10580 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10581 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10582 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10583 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10584 | "urandom(n) -> str\n\n\ |
| 10585 | Return n random bytes suitable for cryptographic use."); |
| 10586 | |
| 10587 | static PyObject * |
| 10588 | posix_urandom(PyObject *self, PyObject *args) |
| 10589 | { |
| 10590 | Py_ssize_t size; |
| 10591 | PyObject *result; |
| 10592 | int ret; |
| 10593 | |
| 10594 | /* Read arguments */ |
| 10595 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10596 | return NULL; |
| 10597 | if (size < 0) |
| 10598 | return PyErr_Format(PyExc_ValueError, |
| 10599 | "negative argument not allowed"); |
| 10600 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10601 | if (result == NULL) |
| 10602 | return NULL; |
| 10603 | |
| 10604 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10605 | PyBytes_GET_SIZE(result)); |
| 10606 | if (ret == -1) { |
| 10607 | Py_DECREF(result); |
| 10608 | return NULL; |
| 10609 | } |
| 10610 | return result; |
| 10611 | } |
| 10612 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10613 | /* Terminal size querying */ |
| 10614 | |
| 10615 | static PyTypeObject TerminalSizeType; |
| 10616 | |
| 10617 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10618 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10619 | |
| 10620 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10621 | {"columns", "width of the terminal window in characters"}, |
| 10622 | {"lines", "height of the terminal window in characters"}, |
| 10623 | {NULL, NULL} |
| 10624 | }; |
| 10625 | |
| 10626 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10627 | "os.terminal_size", |
| 10628 | TerminalSize_docstring, |
| 10629 | TerminalSize_fields, |
| 10630 | 2, |
| 10631 | }; |
| 10632 | |
| 10633 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10634 | PyDoc_STRVAR(termsize__doc__, |
| 10635 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10636 | "\n" \ |
| 10637 | "The optional argument fd (default standard output) specifies\n" \ |
| 10638 | "which file descriptor should be queried.\n" \ |
| 10639 | "\n" \ |
| 10640 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10641 | "is thrown.\n" \ |
| 10642 | "\n" \ |
| 10643 | "This function will only be defined if an implementation is\n" \ |
| 10644 | "available for this system.\n" \ |
| 10645 | "\n" \ |
| 10646 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10647 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10648 | |
| 10649 | static PyObject* |
| 10650 | get_terminal_size(PyObject *self, PyObject *args) |
| 10651 | { |
| 10652 | int columns, lines; |
| 10653 | PyObject *termsize; |
| 10654 | |
| 10655 | int fd = fileno(stdout); |
| 10656 | /* Under some conditions stdout may not be connected and |
| 10657 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10658 | * GUI apps don't have valid standard streams by default. |
| 10659 | * |
| 10660 | * If this happens, and the optional fd argument is not present, |
| 10661 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10662 | */ |
| 10663 | |
| 10664 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10665 | return NULL; |
| 10666 | |
| 10667 | #ifdef TERMSIZE_USE_IOCTL |
| 10668 | { |
| 10669 | struct winsize w; |
| 10670 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10671 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10672 | columns = w.ws_col; |
| 10673 | lines = w.ws_row; |
| 10674 | } |
| 10675 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10676 | |
| 10677 | #ifdef TERMSIZE_USE_CONIO |
| 10678 | { |
| 10679 | DWORD nhandle; |
| 10680 | HANDLE handle; |
| 10681 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10682 | switch (fd) { |
| 10683 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10684 | break; |
| 10685 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10686 | break; |
| 10687 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10688 | break; |
| 10689 | default: |
| 10690 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10691 | } |
| 10692 | handle = GetStdHandle(nhandle); |
| 10693 | if (handle == NULL) |
| 10694 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10695 | if (handle == INVALID_HANDLE_VALUE) |
| 10696 | return PyErr_SetFromWindowsErr(0); |
| 10697 | |
| 10698 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10699 | return PyErr_SetFromWindowsErr(0); |
| 10700 | |
| 10701 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10702 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10703 | } |
| 10704 | #endif /* TERMSIZE_USE_CONIO */ |
| 10705 | |
| 10706 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10707 | if (termsize == NULL) |
| 10708 | return NULL; |
| 10709 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10710 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10711 | if (PyErr_Occurred()) { |
| 10712 | Py_DECREF(termsize); |
| 10713 | return NULL; |
| 10714 | } |
| 10715 | return termsize; |
| 10716 | } |
| 10717 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10718 | |
| 10719 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10720 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10721 | {"access", (PyCFunction)posix_access, |
| 10722 | METH_VARARGS | METH_KEYWORDS, |
| 10723 | posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10724 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10725 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10726 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10727 | {"chdir", (PyCFunction)posix_chdir, |
| 10728 | METH_VARARGS | METH_KEYWORDS, |
| 10729 | posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10730 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10731 | {"chflags", (PyCFunction)posix_chflags, |
| 10732 | METH_VARARGS | METH_KEYWORDS, |
| 10733 | posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10734 | #endif /* HAVE_CHFLAGS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10735 | {"chmod", (PyCFunction)posix_chmod, |
| 10736 | METH_VARARGS | METH_KEYWORDS, |
| 10737 | posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10738 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10739 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10740 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10741 | #ifdef HAVE_CHOWN |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10742 | {"chown", (PyCFunction)posix_chown, |
| 10743 | METH_VARARGS | METH_KEYWORDS, |
| 10744 | posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10745 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10746 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10747 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10748 | #endif /* HAVE_LCHMOD */ |
| 10749 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10750 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10751 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10752 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10753 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10754 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10755 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10756 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10757 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10758 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10759 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10760 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10761 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10762 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10763 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10764 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10765 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10766 | METH_NOARGS, posix_getcwd__doc__}, |
| 10767 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10768 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10769 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10770 | #if defined(HAVE_LINK) || defined(MS_WINDOWS) |
| 10771 | {"link", (PyCFunction)posix_link, |
| 10772 | METH_VARARGS | METH_KEYWORDS, |
| 10773 | posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10774 | #endif /* HAVE_LINK */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10775 | {"listdir", (PyCFunction)posix_listdir, |
| 10776 | METH_VARARGS | METH_KEYWORDS, |
| 10777 | posix_listdir__doc__}, |
| 10778 | {"lstat", (PyCFunction)posix_lstat, |
| 10779 | METH_VARARGS | METH_KEYWORDS, |
| 10780 | posix_lstat__doc__}, |
| 10781 | {"mkdir", (PyCFunction)posix_mkdir, |
| 10782 | METH_VARARGS | METH_KEYWORDS, |
| 10783 | posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10784 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10785 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10786 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10787 | #ifdef HAVE_GETPRIORITY |
| 10788 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10789 | #endif /* HAVE_GETPRIORITY */ |
| 10790 | #ifdef HAVE_SETPRIORITY |
| 10791 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10792 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10793 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10794 | {"readlink", (PyCFunction)posix_readlink, |
| 10795 | METH_VARARGS | METH_KEYWORDS, |
| 10796 | readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10797 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10798 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10799 | {"readlink", (PyCFunction)win_readlink, |
| 10800 | METH_VARARGS | METH_KEYWORDS, |
| 10801 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10802 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10803 | {"rename", (PyCFunction)posix_rename, |
| 10804 | METH_VARARGS | METH_KEYWORDS, |
| 10805 | posix_rename__doc__}, |
| 10806 | {"replace", (PyCFunction)posix_replace, |
| 10807 | METH_VARARGS | METH_KEYWORDS, |
| 10808 | posix_replace__doc__}, |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10809 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10810 | {"stat", (PyCFunction)posix_stat, |
| 10811 | METH_VARARGS | METH_KEYWORDS, |
| 10812 | posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10813 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10814 | #if defined(HAVE_SYMLINK) |
| 10815 | {"symlink", (PyCFunction)posix_symlink, |
| 10816 | METH_VARARGS | METH_KEYWORDS, |
| 10817 | posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10818 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10819 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10820 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10821 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10822 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10823 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10824 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10825 | #endif /* HAVE_UNAME */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10826 | {"unlink", (PyCFunction)posix_unlink, |
| 10827 | METH_VARARGS | METH_KEYWORDS, |
| 10828 | posix_unlink__doc__}, |
| 10829 | {"remove", (PyCFunction)posix_unlink, |
| 10830 | METH_VARARGS | METH_KEYWORDS, |
| 10831 | posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10832 | {"utime", (PyCFunction)posix_utime, |
| 10833 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10834 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10835 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10836 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10837 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10838 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10839 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10840 | {"execve", (PyCFunction)posix_execve, |
| 10841 | METH_VARARGS | METH_KEYWORDS, |
| 10842 | posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10843 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10844 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10845 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10846 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10847 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10848 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10849 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10850 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10851 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10852 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10853 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10854 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10855 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10856 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10857 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10858 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10859 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10860 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10861 | {"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] | 10862 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10863 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10864 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10865 | #endif |
| 10866 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10867 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10868 | #endif |
| 10869 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10870 | {"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] | 10871 | #endif |
| 10872 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10873 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10874 | #endif |
| 10875 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10876 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10877 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10878 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10879 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10880 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10881 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10882 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10883 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10884 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10885 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10886 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10887 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10888 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10889 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10890 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10891 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 10892 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10893 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10894 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 10895 | #endif /* HAVE_GETEUID */ |
| 10896 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10897 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10898 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10899 | #ifdef HAVE_GETGROUPLIST |
| 10900 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10901 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10902 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10903 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10904 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10905 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10906 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10907 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10908 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10909 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10910 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10911 | #endif /* HAVE_GETPPID */ |
| 10912 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10913 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10914 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10915 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10916 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10917 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10918 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10919 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10920 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10921 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10922 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10923 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10924 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10925 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10926 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10927 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10928 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10929 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10930 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10931 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10932 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10933 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10934 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10935 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10936 | #endif /* HAVE_SETEUID */ |
| 10937 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10938 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10939 | #endif /* HAVE_SETEGID */ |
| 10940 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10941 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10942 | #endif /* HAVE_SETREUID */ |
| 10943 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10944 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10945 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10946 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10947 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10948 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10949 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10950 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10951 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10952 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10953 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10954 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10955 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10956 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10957 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10958 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10959 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10960 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10961 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10962 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10963 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10964 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10965 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10966 | #endif /* HAVE_WAIT3 */ |
| 10967 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10968 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10969 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10970 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10971 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10972 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10973 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10974 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10975 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10976 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10977 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10978 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10979 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10981 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10982 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10983 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10984 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10985 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10986 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10987 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10988 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10989 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10990 | #endif /* HAVE_TCSETPGRP */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10991 | {"open", (PyCFunction)posix_open,\ |
| 10992 | METH_VARARGS | METH_KEYWORDS, |
| 10993 | posix_open__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10994 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10995 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10996 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10997 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10998 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10999 | #ifdef HAVE_LOCKF |
| 11000 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 11001 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11002 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 11003 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11004 | #ifdef HAVE_READV |
| 11005 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 11006 | #endif |
| 11007 | #ifdef HAVE_PREAD |
| 11008 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 11009 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11010 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11011 | #ifdef HAVE_WRITEV |
| 11012 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 11013 | #endif |
| 11014 | #ifdef HAVE_PWRITE |
| 11015 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 11016 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11017 | #ifdef HAVE_SENDFILE |
| 11018 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 11019 | posix_sendfile__doc__}, |
| 11020 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11021 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11022 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11023 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11024 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11025 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11026 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 11027 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11028 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11029 | #ifdef HAVE_MKFIFO |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11030 | {"mkfifo", (PyCFunction)posix_mkfifo, |
| 11031 | METH_VARARGS | METH_KEYWORDS, |
| 11032 | posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11033 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 11034 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11035 | {"mknod", (PyCFunction)posix_mknod, |
| 11036 | METH_VARARGS | METH_KEYWORDS, |
| 11037 | posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 11038 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11039 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11040 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 11041 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 11042 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11043 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11044 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11045 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11046 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11047 | #ifdef HAVE_TRUNCATE |
| 11048 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 11049 | #endif |
| 11050 | #ifdef HAVE_POSIX_FALLOCATE |
| 11051 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 11052 | #endif |
| 11053 | #ifdef HAVE_POSIX_FADVISE |
| 11054 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 11055 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11056 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11057 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11058 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11059 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11060 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11061 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11062 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11063 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11064 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11065 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11066 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11068 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11069 | #ifdef HAVE_SYNC |
| 11070 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 11071 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11072 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11074 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11075 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11076 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11077 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11078 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11079 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11080 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11081 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11082 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11083 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11084 | #endif /* WIFSTOPPED */ |
| 11085 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11086 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11087 | #endif /* WIFSIGNALED */ |
| 11088 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11090 | #endif /* WIFEXITED */ |
| 11091 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11092 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11093 | #endif /* WEXITSTATUS */ |
| 11094 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11096 | #endif /* WTERMSIG */ |
| 11097 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11098 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11099 | #endif /* WSTOPSIG */ |
| 11100 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11101 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11102 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11103 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11104 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11105 | {"statvfs", (PyCFunction)posix_statvfs, |
| 11106 | METH_VARARGS | METH_KEYWORDS, |
| 11107 | posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11108 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11109 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11111 | #endif |
| 11112 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11114 | #endif |
| 11115 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11116 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11117 | #endif |
| 11118 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11119 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11120 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11121 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11122 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11123 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11124 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 11125 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 11126 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11127 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 11128 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11129 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11131 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 11132 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11133 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11134 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11135 | #endif |
| 11136 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11137 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11138 | #endif |
| 11139 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11140 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11141 | #endif |
| 11142 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11143 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11144 | #endif |
| 11145 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11146 | #ifdef USE_XATTRS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11147 | {"setxattr", (PyCFunction)posix_setxattr, |
| 11148 | METH_VARARGS | METH_KEYWORDS, |
| 11149 | posix_setxattr__doc__}, |
| 11150 | {"getxattr", (PyCFunction)posix_getxattr, |
| 11151 | METH_VARARGS | METH_KEYWORDS, |
| 11152 | posix_getxattr__doc__}, |
| 11153 | {"removexattr", (PyCFunction)posix_removexattr, |
| 11154 | METH_VARARGS | METH_KEYWORDS, |
| 11155 | posix_removexattr__doc__}, |
| 11156 | {"listxattr", (PyCFunction)posix_listxattr, |
| 11157 | METH_VARARGS | METH_KEYWORDS, |
| 11158 | posix_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11159 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11160 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 11161 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 11162 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11163 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11164 | }; |
| 11165 | |
| 11166 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11167 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11168 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11169 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11170 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11171 | } |
| 11172 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11173 | #if defined(PYOS_OS2) |
| 11174 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11175 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11176 | { |
| 11177 | APIRET rc; |
| 11178 | ULONG values[QSV_MAX+1]; |
| 11179 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 11180 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11181 | |
| 11182 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 11183 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11184 | Py_END_ALLOW_THREADS |
| 11185 | |
| 11186 | if (rc != NO_ERROR) { |
| 11187 | os2_error(rc); |
| 11188 | return -1; |
| 11189 | } |
| 11190 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11191 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 11192 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 11193 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 11194 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 11195 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 11196 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 11197 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11198 | |
| 11199 | switch (values[QSV_VERSION_MINOR]) { |
| 11200 | case 0: ver = "2.00"; break; |
| 11201 | case 10: ver = "2.10"; break; |
| 11202 | case 11: ver = "2.11"; break; |
| 11203 | case 30: ver = "3.00"; break; |
| 11204 | case 40: ver = "4.00"; break; |
| 11205 | case 50: ver = "5.00"; break; |
| 11206 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11207 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11208 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11209 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11210 | ver = &tmp[0]; |
| 11211 | } |
| 11212 | |
| 11213 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11214 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11215 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11216 | |
| 11217 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 11218 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 11219 | tmp[1] = ':'; |
| 11220 | tmp[2] = '\0'; |
| 11221 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11222 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11223 | } |
| 11224 | #endif |
| 11225 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11226 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11227 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11228 | enable_symlink() |
| 11229 | { |
| 11230 | HANDLE tok; |
| 11231 | TOKEN_PRIVILEGES tok_priv; |
| 11232 | LUID luid; |
| 11233 | int meth_idx = 0; |
| 11234 | |
| 11235 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11236 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11237 | |
| 11238 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11239 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11240 | |
| 11241 | tok_priv.PrivilegeCount = 1; |
| 11242 | tok_priv.Privileges[0].Luid = luid; |
| 11243 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11244 | |
| 11245 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11246 | sizeof(TOKEN_PRIVILEGES), |
| 11247 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11248 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11249 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11250 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11251 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11252 | } |
| 11253 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11254 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11255 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11256 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11257 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11258 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11259 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11260 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11261 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11263 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11264 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11265 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11266 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11267 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11268 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11269 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11270 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11271 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11272 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11273 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11274 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11275 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11276 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11277 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11278 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11279 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11280 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11281 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11282 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11283 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11284 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11285 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11286 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11287 | #endif |
| 11288 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11289 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11290 | #endif |
| 11291 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11292 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11293 | #endif |
| 11294 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11295 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11296 | #endif |
| 11297 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11298 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11299 | #endif |
| 11300 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11301 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11302 | #endif |
| 11303 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11304 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11305 | #endif |
| 11306 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11307 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11308 | #endif |
| 11309 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11310 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11311 | #endif |
| 11312 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11313 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11314 | #endif |
| 11315 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11316 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11317 | #endif |
| 11318 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11319 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11320 | #endif |
| 11321 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11322 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11323 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11324 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11325 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11326 | #endif |
| 11327 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11328 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11329 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11330 | #ifdef O_XATTR |
| 11331 | if (ins(d, "O_XATTR", (long)O_XATTR)) return -1; |
| 11332 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11333 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11334 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11335 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11336 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11337 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11338 | #endif |
| 11339 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11340 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11341 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11342 | #ifdef O_EXEC |
| 11343 | if (ins(d, "O_EXEC", (long)O_EXEC)) return -1; |
| 11344 | #endif |
| 11345 | #ifdef O_SEARCH |
| 11346 | if (ins(d, "O_SEARCH", (long)O_SEARCH)) return -1; |
| 11347 | #endif |
| 11348 | #ifdef O_TTY_INIT |
| 11349 | if (ins(d, "O_TTY_INIT", (long)O_TTY_INIT)) return -1; |
| 11350 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11351 | #ifdef PRIO_PROCESS |
| 11352 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11353 | #endif |
| 11354 | #ifdef PRIO_PGRP |
| 11355 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11356 | #endif |
| 11357 | #ifdef PRIO_USER |
| 11358 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11359 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11360 | #ifdef O_CLOEXEC |
| 11361 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11362 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11363 | #ifdef O_ACCMODE |
| 11364 | if (ins(d, "O_ACCMODE", (long)O_ACCMODE)) return -1; |
| 11365 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11366 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11367 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 11368 | #ifdef SEEK_HOLE |
| 11369 | if (ins(d, "SEEK_HOLE", (long)SEEK_HOLE)) return -1; |
| 11370 | #endif |
| 11371 | #ifdef SEEK_DATA |
| 11372 | if (ins(d, "SEEK_DATA", (long)SEEK_DATA)) return -1; |
| 11373 | #endif |
| 11374 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11375 | /* MS Windows */ |
| 11376 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11377 | /* Don't inherit in child processes. */ |
| 11378 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11379 | #endif |
| 11380 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11381 | /* Optimize for short life (keep in memory). */ |
| 11382 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11383 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11384 | #endif |
| 11385 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11386 | /* Automatically delete when last handle is closed. */ |
| 11387 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11388 | #endif |
| 11389 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11390 | /* Optimize for random access. */ |
| 11391 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11392 | #endif |
| 11393 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11394 | /* Optimize for sequential access. */ |
| 11395 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11396 | #endif |
| 11397 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11398 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11399 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11400 | /* Send a SIGIO signal whenever input or output |
| 11401 | becomes available on file descriptor */ |
| 11402 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11403 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11404 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11405 | /* Direct disk access. */ |
| 11406 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11407 | #endif |
| 11408 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11409 | /* Must be a directory. */ |
| 11410 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11411 | #endif |
| 11412 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11413 | /* Do not follow links. */ |
| 11414 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11415 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11416 | #ifdef O_NOLINKS |
| 11417 | /* Fails if link count of the named file is greater than 1 */ |
| 11418 | if (ins(d, "O_NOLINKS", (long)O_NOLINKS)) return -1; |
| 11419 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11420 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11421 | /* Do not update the access time. */ |
| 11422 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11423 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11424 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11425 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11426 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11427 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11428 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11429 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11430 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11431 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11432 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11433 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11434 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11435 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11436 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11437 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11438 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11439 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11440 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11441 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11442 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11443 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11444 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11445 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11446 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11447 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11448 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11449 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11450 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11451 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11452 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11453 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11454 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11455 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11456 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11457 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11458 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11459 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11460 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11461 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11462 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11463 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11464 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11465 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11466 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11467 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11468 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11469 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11470 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11471 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11472 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11473 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11474 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11475 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11476 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11477 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11478 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11479 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11480 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11481 | #endif /* ST_RDONLY */ |
| 11482 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11483 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11484 | #endif /* ST_NOSUID */ |
| 11485 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11486 | /* FreeBSD sendfile() constants */ |
| 11487 | #ifdef SF_NODISKIO |
| 11488 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11489 | #endif |
| 11490 | #ifdef SF_MNOWAIT |
| 11491 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11492 | #endif |
| 11493 | #ifdef SF_SYNC |
| 11494 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11495 | #endif |
| 11496 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11497 | /* constants for posix_fadvise */ |
| 11498 | #ifdef POSIX_FADV_NORMAL |
| 11499 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11500 | #endif |
| 11501 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11502 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11503 | #endif |
| 11504 | #ifdef POSIX_FADV_RANDOM |
| 11505 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11506 | #endif |
| 11507 | #ifdef POSIX_FADV_NOREUSE |
| 11508 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11509 | #endif |
| 11510 | #ifdef POSIX_FADV_WILLNEED |
| 11511 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11512 | #endif |
| 11513 | #ifdef POSIX_FADV_DONTNEED |
| 11514 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11515 | #endif |
| 11516 | |
| 11517 | /* constants for waitid */ |
| 11518 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11519 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11520 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11521 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11522 | #endif |
| 11523 | #ifdef WEXITED |
| 11524 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11525 | #endif |
| 11526 | #ifdef WNOWAIT |
| 11527 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11528 | #endif |
| 11529 | #ifdef WSTOPPED |
| 11530 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11531 | #endif |
| 11532 | #ifdef CLD_EXITED |
| 11533 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11534 | #endif |
| 11535 | #ifdef CLD_DUMPED |
| 11536 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11537 | #endif |
| 11538 | #ifdef CLD_TRAPPED |
| 11539 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11540 | #endif |
| 11541 | #ifdef CLD_CONTINUED |
| 11542 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11543 | #endif |
| 11544 | |
| 11545 | /* constants for lockf */ |
| 11546 | #ifdef F_LOCK |
| 11547 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11548 | #endif |
| 11549 | #ifdef F_TLOCK |
| 11550 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11551 | #endif |
| 11552 | #ifdef F_ULOCK |
| 11553 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11554 | #endif |
| 11555 | #ifdef F_TEST |
| 11556 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11557 | #endif |
| 11558 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11559 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11560 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11561 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11562 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11563 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11564 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11565 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11566 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11567 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11568 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11569 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11570 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11571 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11572 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11573 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11574 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11575 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11576 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11577 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11578 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11579 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11580 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11581 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11582 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11583 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11584 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11585 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11586 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11587 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11588 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11589 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11590 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11591 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11592 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11593 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11594 | #ifdef SCHED_SPORADIC |
| 11595 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11596 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11597 | #ifdef SCHED_BATCH |
| 11598 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11599 | #endif |
| 11600 | #ifdef SCHED_IDLE |
| 11601 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11602 | #endif |
| 11603 | #ifdef SCHED_RESET_ON_FORK |
| 11604 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11605 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11606 | #ifdef SCHED_SYS |
| 11607 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11608 | #endif |
| 11609 | #ifdef SCHED_IA |
| 11610 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11611 | #endif |
| 11612 | #ifdef SCHED_FSS |
| 11613 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11614 | #endif |
| 11615 | #ifdef SCHED_FX |
| 11616 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11617 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11618 | #endif |
| 11619 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11620 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11621 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11622 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11623 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11624 | #endif |
| 11625 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11626 | #ifdef RTLD_LAZY |
| 11627 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11628 | #endif |
| 11629 | #ifdef RTLD_NOW |
| 11630 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11631 | #endif |
| 11632 | #ifdef RTLD_GLOBAL |
| 11633 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11634 | #endif |
| 11635 | #ifdef RTLD_LOCAL |
| 11636 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11637 | #endif |
| 11638 | #ifdef RTLD_NODELETE |
| 11639 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11640 | #endif |
| 11641 | #ifdef RTLD_NOLOAD |
| 11642 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11643 | #endif |
| 11644 | #ifdef RTLD_DEEPBIND |
| 11645 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11646 | #endif |
| 11647 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11648 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11649 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11650 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11651 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11652 | } |
| 11653 | |
| 11654 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11655 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11656 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11657 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11658 | |
| 11659 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11660 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11661 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11662 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11663 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11664 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11665 | #define MODNAME "posix" |
| 11666 | #endif |
| 11667 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11668 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11669 | PyModuleDef_HEAD_INIT, |
| 11670 | MODNAME, |
| 11671 | posix__doc__, |
| 11672 | -1, |
| 11673 | posix_methods, |
| 11674 | NULL, |
| 11675 | NULL, |
| 11676 | NULL, |
| 11677 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11678 | }; |
| 11679 | |
| 11680 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11681 | static char *have_functions[] = { |
| 11682 | |
| 11683 | #ifdef HAVE_FACCESSAT |
| 11684 | "HAVE_FACCESSAT", |
| 11685 | #endif |
| 11686 | |
| 11687 | #ifdef HAVE_FCHDIR |
| 11688 | "HAVE_FCHDIR", |
| 11689 | #endif |
| 11690 | |
| 11691 | #ifdef HAVE_FCHMOD |
| 11692 | "HAVE_FCHMOD", |
| 11693 | #endif |
| 11694 | |
| 11695 | #ifdef HAVE_FCHMODAT |
| 11696 | "HAVE_FCHMODAT", |
| 11697 | #endif |
| 11698 | |
| 11699 | #ifdef HAVE_FCHOWN |
| 11700 | "HAVE_FCHOWN", |
| 11701 | #endif |
| 11702 | |
| 11703 | #ifdef HAVE_FEXECVE |
| 11704 | "HAVE_FEXECVE", |
| 11705 | #endif |
| 11706 | |
| 11707 | #ifdef HAVE_FDOPENDIR |
| 11708 | "HAVE_FDOPENDIR", |
| 11709 | #endif |
| 11710 | |
| 11711 | #ifdef HAVE_FSTATAT |
| 11712 | "HAVE_FSTATAT", |
| 11713 | #endif |
| 11714 | |
| 11715 | #ifdef HAVE_FSTATVFS |
| 11716 | "HAVE_FSTATVFS", |
| 11717 | #endif |
| 11718 | |
| 11719 | #ifdef HAVE_FUTIMENS |
| 11720 | "HAVE_FUTIMENS", |
| 11721 | #endif |
| 11722 | |
| 11723 | #ifdef HAVE_FUTIMES |
| 11724 | "HAVE_FUTIMES", |
| 11725 | #endif |
| 11726 | |
| 11727 | #ifdef HAVE_FUTIMESAT |
| 11728 | "HAVE_FUTIMESAT", |
| 11729 | #endif |
| 11730 | |
| 11731 | #ifdef HAVE_LINKAT |
| 11732 | "HAVE_LINKAT", |
| 11733 | #endif |
| 11734 | |
| 11735 | #ifdef HAVE_LCHFLAGS |
| 11736 | "HAVE_LCHFLAGS", |
| 11737 | #endif |
| 11738 | |
| 11739 | #ifdef HAVE_LCHMOD |
| 11740 | "HAVE_LCHMOD", |
| 11741 | #endif |
| 11742 | |
| 11743 | #ifdef HAVE_LCHOWN |
| 11744 | "HAVE_LCHOWN", |
| 11745 | #endif |
| 11746 | |
| 11747 | #ifdef HAVE_LSTAT |
| 11748 | "HAVE_LSTAT", |
| 11749 | #endif |
| 11750 | |
| 11751 | #ifdef HAVE_LUTIMES |
| 11752 | "HAVE_LUTIMES", |
| 11753 | #endif |
| 11754 | |
| 11755 | #ifdef HAVE_MKDIRAT |
| 11756 | "HAVE_MKDIRAT", |
| 11757 | #endif |
| 11758 | |
| 11759 | #ifdef HAVE_MKFIFOAT |
| 11760 | "HAVE_MKFIFOAT", |
| 11761 | #endif |
| 11762 | |
| 11763 | #ifdef HAVE_MKNODAT |
| 11764 | "HAVE_MKNODAT", |
| 11765 | #endif |
| 11766 | |
| 11767 | #ifdef HAVE_OPENAT |
| 11768 | "HAVE_OPENAT", |
| 11769 | #endif |
| 11770 | |
| 11771 | #ifdef HAVE_READLINKAT |
| 11772 | "HAVE_READLINKAT", |
| 11773 | #endif |
| 11774 | |
| 11775 | #ifdef HAVE_RENAMEAT |
| 11776 | "HAVE_RENAMEAT", |
| 11777 | #endif |
| 11778 | |
| 11779 | #ifdef HAVE_SYMLINKAT |
| 11780 | "HAVE_SYMLINKAT", |
| 11781 | #endif |
| 11782 | |
| 11783 | #ifdef HAVE_UNLINKAT |
| 11784 | "HAVE_UNLINKAT", |
| 11785 | #endif |
| 11786 | |
| 11787 | #ifdef HAVE_UTIMENSAT |
| 11788 | "HAVE_UTIMENSAT", |
| 11789 | #endif |
| 11790 | |
| 11791 | #ifdef MS_WINDOWS |
| 11792 | "MS_WINDOWS", |
| 11793 | #endif |
| 11794 | |
| 11795 | NULL |
| 11796 | }; |
| 11797 | |
| 11798 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11799 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11800 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11801 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11802 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11803 | PyObject *list; |
| 11804 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11805 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11806 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11807 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11808 | #endif |
| 11809 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11810 | m = PyModule_Create(&posixmodule); |
| 11811 | if (m == NULL) |
| 11812 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11813 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11814 | /* Initialize environ dictionary */ |
| 11815 | v = convertenviron(); |
| 11816 | Py_XINCREF(v); |
| 11817 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11818 | return NULL; |
| 11819 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11821 | if (all_ins(m)) |
| 11822 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11823 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11824 | if (setup_confname_tables(m)) |
| 11825 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11826 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11827 | Py_INCREF(PyExc_OSError); |
| 11828 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11829 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11830 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11831 | if (PyType_Ready(&cpu_set_type) < 0) |
| 11832 | return NULL; |
| 11833 | Py_INCREF(&cpu_set_type); |
| 11834 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11835 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11836 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11837 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11838 | if (posix_putenv_garbage == NULL) |
| 11839 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11840 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11841 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11842 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11843 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11844 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11845 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11846 | #endif |
| 11847 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11848 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11849 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11850 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11851 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11852 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11853 | structseq_new = StatResultType.tp_new; |
| 11854 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11855 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11856 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11857 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11858 | #ifdef NEED_TICKS_PER_SECOND |
| 11859 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11860 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11861 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11862 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11863 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11864 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11865 | # endif |
| 11866 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11867 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11868 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11869 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11870 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11871 | SchedParamType.tp_new = sched_param_new; |
| 11872 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11873 | |
| 11874 | /* initialize TerminalSize_info */ |
| 11875 | PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc); |
| 11876 | Py_INCREF(&TerminalSizeType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11877 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11878 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11879 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11880 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11881 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11882 | Py_INCREF((PyObject*) &StatResultType); |
| 11883 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11884 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11885 | PyModule_AddObject(m, "statvfs_result", |
| 11886 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11887 | |
| 11888 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11889 | Py_INCREF(&SchedParamType); |
| 11890 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11891 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11892 | |
| 11893 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11894 | /* |
| 11895 | * Step 2 of weak-linking support on Mac OS X. |
| 11896 | * |
| 11897 | * The code below removes functions that are not available on the |
| 11898 | * currently active platform. |
| 11899 | * |
| 11900 | * 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] | 11901 | * 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] | 11902 | * OSX 10.4. |
| 11903 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11904 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11905 | if (fstatvfs == NULL) { |
| 11906 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11907 | return NULL; |
| 11908 | } |
| 11909 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11910 | #endif /* HAVE_FSTATVFS */ |
| 11911 | |
| 11912 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11913 | if (statvfs == NULL) { |
| 11914 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11915 | return NULL; |
| 11916 | } |
| 11917 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11918 | #endif /* HAVE_STATVFS */ |
| 11919 | |
| 11920 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11921 | if (lchown == NULL) { |
| 11922 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11923 | return NULL; |
| 11924 | } |
| 11925 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11926 | #endif /* HAVE_LCHOWN */ |
| 11927 | |
| 11928 | |
| 11929 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11930 | |
| 11931 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 11932 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 11933 | billion = PyLong_FromLong(1000000000); |
| 11934 | if (!billion) |
| 11935 | return NULL; |
| 11936 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11937 | /* suppress "function not used" warnings */ |
| 11938 | { |
| 11939 | int ignored; |
| 11940 | fd_specified("", -1); |
| 11941 | follow_symlinks_specified("", 1); |
| 11942 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 11943 | dir_fd_converter(Py_None, &ignored); |
| 11944 | dir_fd_unavailable(Py_None, &ignored); |
| 11945 | } |
| 11946 | |
| 11947 | /* |
| 11948 | * provide list of locally available functions |
| 11949 | * so os.py can populate support_* lists |
| 11950 | */ |
| 11951 | list = PyList_New(0); |
| 11952 | if (!list) |
| 11953 | return NULL; |
| 11954 | for (trace = have_functions; *trace; trace++) { |
| 11955 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 11956 | if (!unicode) |
| 11957 | return NULL; |
| 11958 | if (PyList_Append(list, unicode)) |
| 11959 | return NULL; |
| 11960 | Py_DECREF(unicode); |
| 11961 | } |
| 11962 | PyModule_AddObject(m, "_have_functions", list); |
| 11963 | |
| 11964 | initialized = 1; |
| 11965 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11966 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11967 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11968 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11969 | |
| 11970 | #ifdef __cplusplus |
| 11971 | } |
| 11972 | #endif |