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 |
| 382 | # define FSTAT win32_fstat |
| 383 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 384 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 385 | # define STAT stat |
| 386 | # define FSTAT fstat |
| 387 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 388 | #endif |
| 389 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 390 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 391 | #include <sys/mkdev.h> |
| 392 | #else |
| 393 | #if defined(MAJOR_IN_SYSMACROS) |
| 394 | #include <sys/sysmacros.h> |
| 395 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 396 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 397 | #include <sys/mkdev.h> |
| 398 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 399 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 400 | |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 401 | /* |
| 402 | * De-vararg'd PyArg_ParseTupleAndKeywords() |
| 403 | */ |
| 404 | typedef struct argument_path_s { |
| 405 | struct argument_path_s *next; |
| 406 | PyObject *object; |
| 407 | char *format; |
| 408 | wchar_t **wide; |
| 409 | char **narrow; |
| 410 | Py_ssize_t *length; |
| 411 | } argument_path_t; |
| 412 | |
| 413 | typedef struct { |
| 414 | PyObject *args; |
| 415 | PyObject *kwargs; |
| 416 | char format[32]; |
| 417 | char *format_trace; |
| 418 | char *keywords[8]; |
| 419 | int keyword_count; |
| 420 | PyObject **varargs[16]; |
| 421 | int vararg_count; |
| 422 | const char *function_name; |
| 423 | argument_path_t *path_head; |
| 424 | } argument_parser_table_t; |
| 425 | |
| 426 | #define APT_DECLARE(apt, fname) \ |
| 427 | argument_parser_table_t apt; \ |
| 428 | memset(&apt, 0, sizeof(apt)); \ |
| 429 | apt.function_name = fname; \ |
| 430 | apt.args = args; \ |
| 431 | apt.kwargs = kwargs; \ |
| 432 | apt.format_trace = apt.format |
| 433 | |
| 434 | #define _APT_ARGUMENT_OBJECT(apt, name, variable, format) \ |
| 435 | *apt.format_trace++ = (format); \ |
| 436 | apt.keywords[apt.keyword_count++] = (name); \ |
| 437 | apt.varargs[apt.vararg_count++] = (PyObject **)(variable); \ |
| 438 | |
| 439 | #define APT_ARGUMENT_OBJECT(apt, name, variable) \ |
| 440 | _APT_ARGUMENT_OBJECT(apt, name, variable, 'O') |
| 441 | |
| 442 | #define APT_ARGUMENT_OBJECT_WITH_CONVERTER(apt, name, variable, converter) \ |
| 443 | apt.varargs[apt.vararg_count++] = (PyObject **)converter; \ |
| 444 | APT_ARGUMENT_OBJECT(apt, name, variable); \ |
| 445 | *apt.format_trace++ = '&'; \ |
| 446 | |
| 447 | #define APT_ARGUMENT_UNICODE(apt, name, variable) \ |
| 448 | _APT_ARGUMENT_OBJECT(apt, name, variable, 'U') |
| 449 | |
| 450 | #define APT_ARGUMENT_BYTES(apt, name, variable) \ |
| 451 | _APT_ARGUMENT_OBJECT(apt, name, variable, 'y') |
| 452 | |
| 453 | #define APT_ARGUMENT_INT(apt, name, variable) \ |
| 454 | _APT_ARGUMENT_OBJECT(apt, name, variable, 'i') |
| 455 | |
| 456 | static int |
| 457 | bool_converter(PyObject *object, void *address) { |
| 458 | int *output = (int *)address; |
| 459 | int value = PyObject_IsTrue(object); |
| 460 | if (value == -1) |
| 461 | return 0; |
| 462 | *output = value; |
| 463 | return 1; |
| 464 | } |
| 465 | |
| 466 | #define APT_ARGUMENT_BOOL(apt, name, variable) \ |
| 467 | _APT_ARGUMENT_OBJECT(apt, name, variable, 'p') |
| 468 | |
| 469 | #if !MS_WINDOWS |
| 470 | #define _APT_DEFAULT_PATH_TYPE 'U' |
| 471 | #define _APT_PATH_BEFORE ; |
| 472 | #define _APT_PATH_AFTER ; |
| 473 | #else |
| 474 | #define _APT_DEFAULT_PATH_TYPE 'O' |
| 475 | #define _APT_PATH_BEFORE apt.varargs[apt.vararg_count++] = (PyObject **)PyUnicode_FSConverter; |
| 476 | #define _APT_PATH_AFTER *apt.format_trace++ = '&'; |
| 477 | #endif |
| 478 | |
| 479 | #define APT_ARGUMENT_PATH(apt, name, w, n, l) \ |
| 480 | { \ |
| 481 | argument_path_t *_path_ = (argument_path_t *)alloca(sizeof(argument_path_t)); \ |
| 482 | _APT_PATH_BEFORE; \ |
| 483 | memset(_path_, 0, sizeof(*_path_)); \ |
| 484 | _path_->wide = w; \ |
| 485 | _path_->narrow = n; \ |
| 486 | _path_->length = l; \ |
| 487 | _path_->next = apt.path_head; \ |
| 488 | apt.path_head = _path_; \ |
| 489 | _APT_ARGUMENT_OBJECT(apt, name, &_path_->object, _APT_DEFAULT_PATH_TYPE); \ |
| 490 | _APT_PATH_AFTER; \ |
| 491 | } \ |
| 492 | |
| 493 | #define APT_OPTIONAL(apt) \ |
| 494 | *apt.format_trace++ = '|' \ |
| 495 | |
| 496 | #define APT_KEYWORD_ONLY(apt) \ |
| 497 | *apt.format_trace++ = '$' \ |
| 498 | |
| 499 | |
| 500 | static int apt_parse(argument_parser_table_t *apt) { |
| 501 | argument_path_t *trace; |
| 502 | |
| 503 | *apt->format_trace++ = ':'; |
| 504 | strcpy(apt->format_trace, apt->function_name); |
| 505 | apt->keywords[apt->keyword_count] = NULL; |
| 506 | |
| 507 | for (;;) { |
| 508 | int result; |
| 509 | |
| 510 | switch (apt->vararg_count) { |
| 511 | case 0: |
| 512 | PyErr_SetString(PyExc_RuntimeError, "suspiciously too few arguments for apt_parse"); |
| 513 | return 0; |
| 514 | |
| 515 | #define APT_PREAMBLE \ |
| 516 | result = PyArg_ParseTupleAndKeywords(apt->args, apt->kwargs, apt->format, apt->keywords, \ |
| 517 | |
| 518 | #define APT_POSTSCRIPT \ |
| 519 | ); \ |
| 520 | break; \ |
| 521 | |
| 522 | case 1: |
| 523 | APT_PREAMBLE |
| 524 | apt->varargs[0] |
| 525 | APT_POSTSCRIPT |
| 526 | case 2: |
| 527 | APT_PREAMBLE |
| 528 | apt->varargs[0], |
| 529 | apt->varargs[1] |
| 530 | APT_POSTSCRIPT |
| 531 | case 3: |
| 532 | APT_PREAMBLE |
| 533 | apt->varargs[0], |
| 534 | apt->varargs[1], |
| 535 | apt->varargs[2] |
| 536 | APT_POSTSCRIPT |
| 537 | case 4: |
| 538 | APT_PREAMBLE |
| 539 | apt->varargs[0], |
| 540 | apt->varargs[1], |
| 541 | apt->varargs[2], |
| 542 | apt->varargs[3] |
| 543 | APT_POSTSCRIPT |
| 544 | case 5: |
| 545 | APT_PREAMBLE |
| 546 | apt->varargs[0], |
| 547 | apt->varargs[1], |
| 548 | apt->varargs[2], |
| 549 | apt->varargs[3], |
| 550 | apt->varargs[4] |
| 551 | APT_POSTSCRIPT |
| 552 | case 6: |
| 553 | APT_PREAMBLE |
| 554 | apt->varargs[0], |
| 555 | apt->varargs[1], |
| 556 | apt->varargs[2], |
| 557 | apt->varargs[3], |
| 558 | apt->varargs[4], |
| 559 | apt->varargs[5] |
| 560 | APT_POSTSCRIPT |
| 561 | case 7: |
| 562 | APT_PREAMBLE |
| 563 | apt->varargs[0], |
| 564 | apt->varargs[1], |
| 565 | apt->varargs[2], |
| 566 | apt->varargs[3], |
| 567 | apt->varargs[4], |
| 568 | apt->varargs[5], |
| 569 | apt->varargs[6] |
| 570 | APT_POSTSCRIPT |
| 571 | case 8: |
| 572 | APT_PREAMBLE |
| 573 | apt->varargs[0], |
| 574 | apt->varargs[1], |
| 575 | apt->varargs[2], |
| 576 | apt->varargs[3], |
| 577 | apt->varargs[4], |
| 578 | apt->varargs[5], |
| 579 | apt->varargs[6], |
| 580 | apt->varargs[7] |
| 581 | APT_POSTSCRIPT |
| 582 | default: |
| 583 | PyErr_SetString(PyExc_RuntimeError, "too many arguments for apt_parse"); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | if (result) { |
| 588 | for (trace = apt->path_head; trace != NULL; trace = trace->next) { |
| 589 | #if MS_WINDOWS |
| 590 | switch (*trace->format) { |
| 591 | case 'U': |
| 592 | *trace->wide = PyUnicode_AsUnicode(trace->object); |
| 593 | if (trace->length) |
| 594 | *trace->length = PyUnicode_GET_SIZE(trace->object); |
| 595 | break; |
| 596 | case 'y' |
| 597 | if (win32_warn_bytes_api()) |
| 598 | return 0; |
| 599 | *trace->narrow = trace->object; |
| 600 | if (trace->length) |
| 601 | *trace->length = strlen((char *)trace->object); |
| 602 | break; |
| 603 | default: |
| 604 | PyErr_SetString(PyExc_RuntimeError, "unexpected format character in apt_parse"); |
| 605 | return 0; |
| 606 | } |
| 607 | #else |
| 608 | assert(*trace->format == 'O'); |
| 609 | *trace->narrow = PyBytes_AsString(trace->object); |
| 610 | if (trace->length) |
| 611 | *trace->length = PyBytes_GET_SIZE(trace->object); |
| 612 | #endif |
| 613 | } |
| 614 | return result; |
| 615 | } |
| 616 | |
| 617 | #if !MS_WINDOWS |
| 618 | break; |
| 619 | #else |
| 620 | for (trace = apt->path_head; trace != NULL; trace = trace->next) { |
| 621 | /* |
| 622 | * try flipping paths between wide and narrow. |
| 623 | * |
| 624 | * each element always started with wide. |
| 625 | * so: |
| 626 | * * if we see a wide, flip it to narrow and stop. |
| 627 | * * if we see a narrow, flip it to wide and move on to the next field. |
| 628 | * * if we run out of fields, we have exhausted all possibilities. fail. |
| 629 | * |
| 630 | * (conceptually it helps to think of the fields as a binary number |
| 631 | * with as many digits as there are entries in the path list. |
| 632 | * wide is 0, narrow is 1. we keep incrementing the number |
| 633 | * until we wrap around to 0 again.) |
| 634 | */ |
| 635 | if (*trace->format == 'U') { |
| 636 | *trace->format = 'y'; |
| 637 | break; |
| 638 | } |
| 639 | if (*trace->format == 'y') { |
| 640 | *trace->format = 'U'; |
| 641 | continue; |
| 642 | } |
| 643 | PyErr_SetString(PyExc_RuntimeError, "unexpected format character in apt_parse"); |
| 644 | return 0; |
| 645 | } |
| 646 | if (!trace) |
| 647 | break; |
| 648 | #endif |
| 649 | } |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static void apt_cleanup(argument_parser_table_t *apt) { |
| 655 | #if !MS_WINDOWS |
| 656 | argument_path_t *trace; |
| 657 | for (trace = apt->path_head; trace != NULL; trace = trace->next) { |
| 658 | Py_XDECREF(trace->object); |
| 659 | } |
| 660 | #endif |
| 661 | } |
| 662 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 663 | /* A helper used by a number of POSIX-only functions */ |
| 664 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 665 | static int |
| 666 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 667 | { |
| 668 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 669 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 670 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 671 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 672 | #endif |
| 673 | if (PyErr_Occurred()) |
| 674 | return 0; |
| 675 | return 1; |
| 676 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 677 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 678 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 679 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 680 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 681 | * valid and throw an assertion if it isn't. |
| 682 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 683 | * an assertion can be useful, but it does contradict the POSIX standard |
| 684 | * which for write(2) states: |
| 685 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 686 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 687 | * writing." |
| 688 | * Furthermore, python allows the user to enter any old integer |
| 689 | * as a fd and should merely raise a python exception on error. |
| 690 | * The Microsoft CRT doesn't provide an official way to check for the |
| 691 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 692 | * 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] | 693 | * internal structures involved. |
| 694 | * The structures below must be updated for each version of visual studio |
| 695 | * according to the file internal.h in the CRT source, until MS comes |
| 696 | * up with a less hacky way to do this. |
| 697 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 698 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 699 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 700 | /* The actual size of the structure is determined at runtime. |
| 701 | * Only the first items must be present. |
| 702 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 703 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 704 | intptr_t osfhnd; |
| 705 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 706 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 707 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 708 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 709 | #define IOINFO_L2E 5 |
| 710 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 711 | #define IOINFO_ARRAYS 64 |
| 712 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 713 | #define FOPEN 0x01 |
| 714 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 715 | |
| 716 | /* This function emulates what the windows CRT does to validate file handles */ |
| 717 | int |
| 718 | _PyVerify_fd(int fd) |
| 719 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 720 | const int i1 = fd >> IOINFO_L2E; |
| 721 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 722 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 723 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 724 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 725 | /* Determine the actual size of the ioinfo structure, |
| 726 | * as used by the CRT loaded in memory |
| 727 | */ |
| 728 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 729 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 730 | } |
| 731 | if (sizeof_ioinfo == 0) { |
| 732 | /* This should not happen... */ |
| 733 | goto fail; |
| 734 | } |
| 735 | |
| 736 | /* See that it isn't a special CLEAR fileno */ |
| 737 | if (fd != _NO_CONSOLE_FILENO) { |
| 738 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 739 | * we check pointer validity and other info |
| 740 | */ |
| 741 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 742 | /* finally, check that the file is open */ |
| 743 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 744 | if (info->osfile & FOPEN) { |
| 745 | return 1; |
| 746 | } |
| 747 | } |
| 748 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 749 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 750 | errno = EBADF; |
| 751 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 755 | static int |
| 756 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 757 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 758 | if (!_PyVerify_fd(fd1)) |
| 759 | return 0; |
| 760 | if (fd2 == _NO_CONSOLE_FILENO) |
| 761 | return 0; |
| 762 | if ((unsigned)fd2 < _NHANDLE_) |
| 763 | return 1; |
| 764 | else |
| 765 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 766 | } |
| 767 | #else |
| 768 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 769 | #define _PyVerify_fd_dup2(A, B) (1) |
| 770 | #endif |
| 771 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 772 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 773 | /* The following structure was copied from |
| 774 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 775 | include doesn't seem to be present in the Windows SDK (at least as included |
| 776 | with Visual Studio Express). */ |
| 777 | typedef struct _REPARSE_DATA_BUFFER { |
| 778 | ULONG ReparseTag; |
| 779 | USHORT ReparseDataLength; |
| 780 | USHORT Reserved; |
| 781 | union { |
| 782 | struct { |
| 783 | USHORT SubstituteNameOffset; |
| 784 | USHORT SubstituteNameLength; |
| 785 | USHORT PrintNameOffset; |
| 786 | USHORT PrintNameLength; |
| 787 | ULONG Flags; |
| 788 | WCHAR PathBuffer[1]; |
| 789 | } SymbolicLinkReparseBuffer; |
| 790 | |
| 791 | struct { |
| 792 | USHORT SubstituteNameOffset; |
| 793 | USHORT SubstituteNameLength; |
| 794 | USHORT PrintNameOffset; |
| 795 | USHORT PrintNameLength; |
| 796 | WCHAR PathBuffer[1]; |
| 797 | } MountPointReparseBuffer; |
| 798 | |
| 799 | struct { |
| 800 | UCHAR DataBuffer[1]; |
| 801 | } GenericReparseBuffer; |
| 802 | }; |
| 803 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 804 | |
| 805 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 806 | GenericReparseBuffer) |
| 807 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 808 | |
| 809 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 810 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 811 | { |
| 812 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 813 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 814 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 815 | |
| 816 | if (0 == DeviceIoControl( |
| 817 | reparse_point_handle, |
| 818 | FSCTL_GET_REPARSE_POINT, |
| 819 | NULL, 0, /* in buffer */ |
| 820 | target_buffer, sizeof(target_buffer), |
| 821 | &n_bytes_returned, |
| 822 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 823 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 824 | |
| 825 | if (reparse_tag) |
| 826 | *reparse_tag = rdb->ReparseTag; |
| 827 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 828 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 829 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 830 | |
| 831 | static int |
| 832 | win32_warn_bytes_api() |
| 833 | { |
| 834 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 835 | "The Windows bytes API has been deprecated, " |
| 836 | "use Unicode filenames instead", |
| 837 | 1); |
| 838 | } |
| 839 | |
| 840 | static PyObject* |
| 841 | win32_decode_filename(PyObject *obj) |
| 842 | { |
| 843 | PyObject *unicode; |
| 844 | if (PyUnicode_Check(obj)) { |
| 845 | if (PyUnicode_READY(obj)) |
| 846 | return NULL; |
| 847 | Py_INCREF(obj); |
| 848 | return obj; |
| 849 | } |
| 850 | if (!PyUnicode_FSDecoder(obj, &unicode)) |
| 851 | return NULL; |
| 852 | if (win32_warn_bytes_api()) { |
| 853 | Py_DECREF(unicode); |
| 854 | return NULL; |
| 855 | } |
| 856 | return unicode; |
| 857 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 858 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 859 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 860 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 861 | #ifdef WITH_NEXT_FRAMEWORK |
| 862 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 863 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 864 | */ |
| 865 | #include <crt_externs.h> |
| 866 | static char **environ; |
| 867 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 868 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 869 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 870 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 871 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 872 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 873 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 874 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 875 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 876 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 877 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 878 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 879 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 880 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 881 | APIRET rc; |
| 882 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 883 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 884 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 885 | d = PyDict_New(); |
| 886 | if (d == NULL) |
| 887 | return NULL; |
| 888 | #ifdef WITH_NEXT_FRAMEWORK |
| 889 | if (environ == NULL) |
| 890 | environ = *_NSGetEnviron(); |
| 891 | #endif |
| 892 | #ifdef MS_WINDOWS |
| 893 | /* _wenviron must be initialized in this way if the program is started |
| 894 | through main() instead of wmain(). */ |
| 895 | _wgetenv(L""); |
| 896 | if (_wenviron == NULL) |
| 897 | return d; |
| 898 | /* This part ignores errors */ |
| 899 | for (e = _wenviron; *e != NULL; e++) { |
| 900 | PyObject *k; |
| 901 | PyObject *v; |
| 902 | wchar_t *p = wcschr(*e, L'='); |
| 903 | if (p == NULL) |
| 904 | continue; |
| 905 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 906 | if (k == NULL) { |
| 907 | PyErr_Clear(); |
| 908 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 909 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 910 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 911 | if (v == NULL) { |
| 912 | PyErr_Clear(); |
| 913 | Py_DECREF(k); |
| 914 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 915 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 916 | if (PyDict_GetItem(d, k) == NULL) { |
| 917 | if (PyDict_SetItem(d, k, v) != 0) |
| 918 | PyErr_Clear(); |
| 919 | } |
| 920 | Py_DECREF(k); |
| 921 | Py_DECREF(v); |
| 922 | } |
| 923 | #else |
| 924 | if (environ == NULL) |
| 925 | return d; |
| 926 | /* This part ignores errors */ |
| 927 | for (e = environ; *e != NULL; e++) { |
| 928 | PyObject *k; |
| 929 | PyObject *v; |
| 930 | char *p = strchr(*e, '='); |
| 931 | if (p == NULL) |
| 932 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 933 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 934 | if (k == NULL) { |
| 935 | PyErr_Clear(); |
| 936 | continue; |
| 937 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 938 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 939 | if (v == NULL) { |
| 940 | PyErr_Clear(); |
| 941 | Py_DECREF(k); |
| 942 | continue; |
| 943 | } |
| 944 | if (PyDict_GetItem(d, k) == NULL) { |
| 945 | if (PyDict_SetItem(d, k, v) != 0) |
| 946 | PyErr_Clear(); |
| 947 | } |
| 948 | Py_DECREF(k); |
| 949 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 950 | } |
| 951 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 952 | #if defined(PYOS_OS2) |
| 953 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 954 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 955 | PyObject *v = PyBytes_FromString(buffer); |
| 956 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 957 | Py_DECREF(v); |
| 958 | } |
| 959 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 960 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 961 | PyObject *v = PyBytes_FromString(buffer); |
| 962 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 963 | Py_DECREF(v); |
| 964 | } |
| 965 | #endif |
| 966 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 969 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 970 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 971 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 972 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 973 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 974 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 975 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 976 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 977 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 978 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 979 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 982 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 983 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 984 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 985 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 986 | PyObject *name_str, *rc; |
| 987 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 988 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 989 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 990 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 991 | name_str); |
| 992 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 993 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 996 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 997 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 998 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 999 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1000 | /* XXX We should pass the function name along in the future. |
| 1001 | (winreg.c also wants to pass the function name.) |
| 1002 | This would however require an additional param to the |
| 1003 | Windows error object, which is non-trivial. |
| 1004 | */ |
| 1005 | errno = GetLastError(); |
| 1006 | if (filename) |
| 1007 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1008 | else |
| 1009 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1010 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1011 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1012 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1013 | win32_error_unicode(char* function, wchar_t* filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1014 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1015 | /* XXX - see win32_error for comments on 'function' */ |
| 1016 | errno = GetLastError(); |
| 1017 | if (filename) |
| 1018 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 1019 | else |
| 1020 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1023 | static PyObject * |
| 1024 | win32_error_object(char* function, PyObject* filename) |
| 1025 | { |
| 1026 | /* XXX - see win32_error for comments on 'function' */ |
| 1027 | errno = GetLastError(); |
| 1028 | if (filename) |
| 1029 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1030 | PyExc_WindowsError, |
| 1031 | errno, |
| 1032 | filename); |
| 1033 | else |
| 1034 | return PyErr_SetFromWindowsErr(errno); |
| 1035 | } |
| 1036 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1037 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1038 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1039 | #if defined(PYOS_OS2) |
| 1040 | /********************************************************************** |
| 1041 | * Helper Function to Trim and Format OS/2 Messages |
| 1042 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1043 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1044 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 1045 | { |
| 1046 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 1047 | |
| 1048 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 1049 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 1050 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 1051 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1052 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 1053 | } |
| 1054 | |
| 1055 | /* Add Optional Reason Text */ |
| 1056 | if (reason) { |
| 1057 | strcat(msgbuf, " : "); |
| 1058 | strcat(msgbuf, reason); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | /********************************************************************** |
| 1063 | * Decode an OS/2 Operating System Error Code |
| 1064 | * |
| 1065 | * A convenience function to lookup an OS/2 error code and return a |
| 1066 | * text message we can use to raise a Python exception. |
| 1067 | * |
| 1068 | * Notes: |
| 1069 | * The messages for errors returned from the OS/2 kernel reside in |
| 1070 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 1071 | * |
| 1072 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1073 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1074 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 1075 | { |
| 1076 | APIRET rc; |
| 1077 | ULONG msglen; |
| 1078 | |
| 1079 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 1080 | Py_BEGIN_ALLOW_THREADS |
| 1081 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 1082 | errorcode, "oso001.msg", &msglen); |
| 1083 | Py_END_ALLOW_THREADS |
| 1084 | |
| 1085 | if (rc == NO_ERROR) |
| 1086 | os2_formatmsg(msgbuf, msglen, reason); |
| 1087 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 1088 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1089 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1090 | |
| 1091 | return msgbuf; |
| 1092 | } |
| 1093 | |
| 1094 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 1095 | errors are not in a global variable e.g. 'errno' nor are |
| 1096 | they congruent with posix error numbers. */ |
| 1097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1098 | static PyObject * |
| 1099 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1100 | { |
| 1101 | char text[1024]; |
| 1102 | PyObject *v; |
| 1103 | |
| 1104 | os2_strerror(text, sizeof(text), code, ""); |
| 1105 | |
| 1106 | v = Py_BuildValue("(is)", code, text); |
| 1107 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 1108 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1109 | Py_DECREF(v); |
| 1110 | } |
| 1111 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 1112 | } |
| 1113 | |
| 1114 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1115 | |
| 1116 | /* POSIX generic methods */ |
| 1117 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1118 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1119 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 1120 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1121 | int fd; |
| 1122 | int res; |
| 1123 | fd = PyObject_AsFileDescriptor(fdobj); |
| 1124 | if (fd < 0) |
| 1125 | return NULL; |
| 1126 | if (!_PyVerify_fd(fd)) |
| 1127 | return posix_error(); |
| 1128 | Py_BEGIN_ALLOW_THREADS |
| 1129 | res = (*func)(fd); |
| 1130 | Py_END_ALLOW_THREADS |
| 1131 | if (res < 0) |
| 1132 | return posix_error(); |
| 1133 | Py_INCREF(Py_None); |
| 1134 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1135 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1136 | |
| 1137 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1138 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1139 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1140 | PyObject *opath1 = NULL; |
| 1141 | char *path1; |
| 1142 | int res; |
| 1143 | if (!PyArg_ParseTuple(args, format, |
| 1144 | PyUnicode_FSConverter, &opath1)) |
| 1145 | return NULL; |
| 1146 | path1 = PyBytes_AsString(opath1); |
| 1147 | Py_BEGIN_ALLOW_THREADS |
| 1148 | res = (*func)(path1); |
| 1149 | Py_END_ALLOW_THREADS |
| 1150 | if (res < 0) |
| 1151 | return posix_error_with_allocated_filename(opath1); |
| 1152 | Py_DECREF(opath1); |
| 1153 | Py_INCREF(Py_None); |
| 1154 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1157 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1158 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1159 | char *format, |
| 1160 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1161 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1162 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 1163 | char *path1, *path2; |
| 1164 | int res; |
| 1165 | if (!PyArg_ParseTuple(args, format, |
| 1166 | PyUnicode_FSConverter, &opath1, |
| 1167 | PyUnicode_FSConverter, &opath2)) { |
| 1168 | return NULL; |
| 1169 | } |
| 1170 | path1 = PyBytes_AsString(opath1); |
| 1171 | path2 = PyBytes_AsString(opath2); |
| 1172 | Py_BEGIN_ALLOW_THREADS |
| 1173 | res = (*func)(path1, path2); |
| 1174 | Py_END_ALLOW_THREADS |
| 1175 | Py_DECREF(opath1); |
| 1176 | Py_DECREF(opath2); |
| 1177 | if (res != 0) |
| 1178 | /* XXX how to report both path1 and path2??? */ |
| 1179 | return posix_error(); |
| 1180 | Py_INCREF(Py_None); |
| 1181 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1184 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1185 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1186 | win32_1str(PyObject* args, char* func, |
| 1187 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 1188 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1189 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1190 | PyObject *uni; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1191 | const char *ansi; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1192 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1193 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1194 | if (PyArg_ParseTuple(args, wformat, &uni)) |
| 1195 | { |
| 1196 | wchar_t *wstr = PyUnicode_AsUnicode(uni); |
| 1197 | if (wstr == NULL) |
| 1198 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1199 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1200 | result = funcW(wstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1201 | Py_END_ALLOW_THREADS |
| 1202 | if (!result) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1203 | return win32_error_object(func, uni); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1204 | Py_INCREF(Py_None); |
| 1205 | return Py_None; |
| 1206 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1207 | PyErr_Clear(); |
| 1208 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1209 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 1210 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1211 | if (win32_warn_bytes_api()) |
| 1212 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1213 | Py_BEGIN_ALLOW_THREADS |
| 1214 | result = funcA(ansi); |
| 1215 | Py_END_ALLOW_THREADS |
| 1216 | if (!result) |
| 1217 | return win32_error(func, ansi); |
| 1218 | Py_INCREF(Py_None); |
| 1219 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1220 | |
| 1221 | } |
| 1222 | |
| 1223 | /* This is a reimplementation of the C library's chdir function, |
| 1224 | but one that produces Win32 errors instead of DOS error codes. |
| 1225 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1226 | it also needs to set "magic" environment variables indicating |
| 1227 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1228 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1229 | win32_chdir(LPCSTR path) |
| 1230 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1231 | char new_path[MAX_PATH+1]; |
| 1232 | int result; |
| 1233 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1234 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1235 | if(!SetCurrentDirectoryA(path)) |
| 1236 | return FALSE; |
| 1237 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 1238 | if (!result) |
| 1239 | return FALSE; |
| 1240 | /* In the ANSI API, there should not be any paths longer |
| 1241 | than MAX_PATH. */ |
| 1242 | assert(result <= MAX_PATH+1); |
| 1243 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 1244 | strncmp(new_path, "//", 2) == 0) |
| 1245 | /* UNC path, nothing to do. */ |
| 1246 | return TRUE; |
| 1247 | env[1] = new_path[0]; |
| 1248 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | /* The Unicode version differs from the ANSI version |
| 1252 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1253 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1254 | win32_wchdir(LPCWSTR path) |
| 1255 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1256 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 1257 | int result; |
| 1258 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1259 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1260 | if(!SetCurrentDirectoryW(path)) |
| 1261 | return FALSE; |
| 1262 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 1263 | if (!result) |
| 1264 | return FALSE; |
| 1265 | if (result > MAX_PATH+1) { |
| 1266 | new_path = malloc(result * sizeof(wchar_t)); |
| 1267 | if (!new_path) { |
| 1268 | SetLastError(ERROR_OUTOFMEMORY); |
| 1269 | return FALSE; |
| 1270 | } |
| 1271 | result = GetCurrentDirectoryW(result, new_path); |
| 1272 | if (!result) { |
| 1273 | free(new_path); |
| 1274 | return FALSE; |
| 1275 | } |
| 1276 | } |
| 1277 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1278 | wcsncmp(new_path, L"//", 2) == 0) |
| 1279 | /* UNC path, nothing to do. */ |
| 1280 | return TRUE; |
| 1281 | env[1] = new_path[0]; |
| 1282 | result = SetEnvironmentVariableW(env, new_path); |
| 1283 | if (new_path != _new_path) |
| 1284 | free(new_path); |
| 1285 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1286 | } |
| 1287 | #endif |
| 1288 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1289 | #ifdef MS_WINDOWS |
| 1290 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1291 | - time stamps are restricted to second resolution |
| 1292 | - file modification times suffer from forth-and-back conversions between |
| 1293 | UTC and local time |
| 1294 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1295 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1296 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1297 | |
| 1298 | struct win32_stat{ |
| 1299 | int st_dev; |
| 1300 | __int64 st_ino; |
| 1301 | unsigned short st_mode; |
| 1302 | int st_nlink; |
| 1303 | int st_uid; |
| 1304 | int st_gid; |
| 1305 | int st_rdev; |
| 1306 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1307 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1308 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1309 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1310 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1311 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1312 | int st_ctime_nsec; |
| 1313 | }; |
| 1314 | |
| 1315 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1316 | |
| 1317 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1318 | 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] | 1319 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1320 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1321 | /* Cannot simply cast and dereference in_ptr, |
| 1322 | since it might not be aligned properly */ |
| 1323 | __int64 in; |
| 1324 | memcpy(&in, in_ptr, sizeof(in)); |
| 1325 | *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] | 1326 | *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] | 1327 | } |
| 1328 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1329 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1330 | 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] | 1331 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1332 | /* XXX endianness */ |
| 1333 | __int64 out; |
| 1334 | out = time_in + secs_between_epochs; |
| 1335 | out = out * 10000000 + nsec_in / 100; |
| 1336 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1339 | /* Below, we *know* that ugo+r is 0444 */ |
| 1340 | #if _S_IREAD != 0400 |
| 1341 | #error Unsupported C library |
| 1342 | #endif |
| 1343 | static int |
| 1344 | attributes_to_mode(DWORD attr) |
| 1345 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1346 | int m = 0; |
| 1347 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1348 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1349 | else |
| 1350 | m |= _S_IFREG; |
| 1351 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1352 | m |= 0444; |
| 1353 | else |
| 1354 | m |= 0666; |
| 1355 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1359 | 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] | 1360 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1361 | memset(result, 0, sizeof(*result)); |
| 1362 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1363 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1364 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1365 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1366 | 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] | 1367 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1368 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1369 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1370 | /* first clear the S_IFMT bits */ |
| 1371 | result->st_mode ^= (result->st_mode & 0170000); |
| 1372 | /* now set the bits that make this a symlink */ |
| 1373 | result->st_mode |= 0120000; |
| 1374 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1375 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1376 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1379 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1380 | 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] | 1381 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1382 | HANDLE hFindFile; |
| 1383 | WIN32_FIND_DATAA FileData; |
| 1384 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1385 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1386 | return FALSE; |
| 1387 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1388 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1389 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1390 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1391 | info->ftCreationTime = FileData.ftCreationTime; |
| 1392 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1393 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1394 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1395 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1396 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1397 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1398 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1399 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1403 | 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] | 1404 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1405 | HANDLE hFindFile; |
| 1406 | WIN32_FIND_DATAW FileData; |
| 1407 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1408 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1409 | return FALSE; |
| 1410 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1411 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1412 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1413 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1414 | info->ftCreationTime = FileData.ftCreationTime; |
| 1415 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1416 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1417 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1418 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1419 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1420 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1421 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1422 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1425 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1426 | static int has_GetFinalPathNameByHandle = 0; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1427 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1428 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1429 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1430 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1431 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1432 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1433 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1434 | DWORD); |
| 1435 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1436 | /* only recheck */ |
| 1437 | if (!has_GetFinalPathNameByHandle) |
| 1438 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1439 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1440 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1441 | "GetFinalPathNameByHandleA"); |
| 1442 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1443 | "GetFinalPathNameByHandleW"); |
| 1444 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1445 | Py_GetFinalPathNameByHandleW; |
| 1446 | } |
| 1447 | return has_GetFinalPathNameByHandle; |
| 1448 | } |
| 1449 | |
| 1450 | static BOOL |
| 1451 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1452 | { |
| 1453 | int buf_size, result_length; |
| 1454 | wchar_t *buf; |
| 1455 | |
| 1456 | /* We have a good handle to the target, use it to determine |
| 1457 | the target path name (then we'll call lstat on it). */ |
| 1458 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1459 | VOLUME_NAME_DOS); |
| 1460 | if(!buf_size) |
| 1461 | return FALSE; |
| 1462 | |
| 1463 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1464 | if (!buf) { |
| 1465 | SetLastError(ERROR_OUTOFMEMORY); |
| 1466 | return FALSE; |
| 1467 | } |
| 1468 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1469 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1470 | buf, buf_size, VOLUME_NAME_DOS); |
| 1471 | |
| 1472 | if(!result_length) { |
| 1473 | free(buf); |
| 1474 | return FALSE; |
| 1475 | } |
| 1476 | |
| 1477 | if(!CloseHandle(hdl)) { |
| 1478 | free(buf); |
| 1479 | return FALSE; |
| 1480 | } |
| 1481 | |
| 1482 | buf[result_length] = 0; |
| 1483 | |
| 1484 | *target_path = buf; |
| 1485 | return TRUE; |
| 1486 | } |
| 1487 | |
| 1488 | static int |
| 1489 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1490 | BOOL traverse); |
| 1491 | static int |
| 1492 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1493 | BOOL traverse) |
| 1494 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1495 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1496 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1497 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1498 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1499 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1500 | const char *dot; |
| 1501 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1502 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1503 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1504 | traverse reparse point. */ |
| 1505 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1508 | hFile = CreateFileA( |
| 1509 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1510 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1511 | 0, /* share mode */ |
| 1512 | NULL, /* security attributes */ |
| 1513 | OPEN_EXISTING, |
| 1514 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1515 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1516 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1517 | the symlink path agin and not the actual final path. */ |
| 1518 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1519 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1520 | NULL); |
| 1521 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1522 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1523 | /* Either the target doesn't exist, or we don't have access to |
| 1524 | get a handle to it. If the former, we need to return an error. |
| 1525 | If the latter, we can use attributes_from_dir. */ |
| 1526 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1527 | return -1; |
| 1528 | /* Could not get attributes on open file. Fall back to |
| 1529 | reading the directory. */ |
| 1530 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1531 | /* Very strange. This should not fail now */ |
| 1532 | return -1; |
| 1533 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1534 | if (traverse) { |
| 1535 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1536 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1537 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1538 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1539 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1540 | } else { |
| 1541 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1542 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1543 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1544 | } |
| 1545 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1546 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1547 | return -1; |
| 1548 | |
| 1549 | /* Close the outer open file handle now that we're about to |
| 1550 | reopen it with different flags. */ |
| 1551 | if (!CloseHandle(hFile)) |
| 1552 | return -1; |
| 1553 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1554 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1555 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1556 | the file without the reparse handling flag set. */ |
| 1557 | hFile2 = CreateFileA( |
| 1558 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1559 | NULL, OPEN_EXISTING, |
| 1560 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1561 | NULL); |
| 1562 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1563 | return -1; |
| 1564 | |
| 1565 | if (!get_target_path(hFile2, &target_path)) |
| 1566 | return -1; |
| 1567 | |
| 1568 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1569 | free(target_path); |
| 1570 | return code; |
| 1571 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1572 | } else |
| 1573 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1574 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1575 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1576 | |
| 1577 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1578 | dot = strrchr(path, '.'); |
| 1579 | if (dot) { |
| 1580 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1581 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1582 | result->st_mode |= 0111; |
| 1583 | } |
| 1584 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1588 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1589 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1590 | { |
| 1591 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1592 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1593 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1594 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1595 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1596 | const wchar_t *dot; |
| 1597 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1598 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1599 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1600 | traverse reparse point. */ |
| 1601 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1604 | hFile = CreateFileW( |
| 1605 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1606 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1607 | 0, /* share mode */ |
| 1608 | NULL, /* security attributes */ |
| 1609 | OPEN_EXISTING, |
| 1610 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1611 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1612 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1613 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1614 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1615 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1616 | NULL); |
| 1617 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1618 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1619 | /* Either the target doesn't exist, or we don't have access to |
| 1620 | get a handle to it. If the former, we need to return an error. |
| 1621 | If the latter, we can use attributes_from_dir. */ |
| 1622 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1623 | return -1; |
| 1624 | /* Could not get attributes on open file. Fall back to |
| 1625 | reading the directory. */ |
| 1626 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1627 | /* Very strange. This should not fail now */ |
| 1628 | return -1; |
| 1629 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1630 | if (traverse) { |
| 1631 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1632 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1633 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1634 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1635 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1636 | } else { |
| 1637 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1638 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1639 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1640 | } |
| 1641 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1642 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1643 | return -1; |
| 1644 | |
| 1645 | /* Close the outer open file handle now that we're about to |
| 1646 | reopen it with different flags. */ |
| 1647 | if (!CloseHandle(hFile)) |
| 1648 | return -1; |
| 1649 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1650 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1651 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1652 | the file without the reparse handling flag set. */ |
| 1653 | hFile2 = CreateFileW( |
| 1654 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1655 | NULL, OPEN_EXISTING, |
| 1656 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1657 | NULL); |
| 1658 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1659 | return -1; |
| 1660 | |
| 1661 | if (!get_target_path(hFile2, &target_path)) |
| 1662 | return -1; |
| 1663 | |
| 1664 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1665 | free(target_path); |
| 1666 | return code; |
| 1667 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1668 | } else |
| 1669 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1670 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1671 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1672 | |
| 1673 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1674 | dot = wcsrchr(path, '.'); |
| 1675 | if (dot) { |
| 1676 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1677 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1678 | result->st_mode |= 0111; |
| 1679 | } |
| 1680 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1684 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1685 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1686 | /* Protocol violation: we explicitly clear errno, instead of |
| 1687 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1688 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1689 | errno = 0; |
| 1690 | return code; |
| 1691 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1692 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1693 | static int |
| 1694 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1695 | { |
| 1696 | /* Protocol violation: we explicitly clear errno, instead of |
| 1697 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1698 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1699 | errno = 0; |
| 1700 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1701 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1702 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1703 | |
| 1704 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1705 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1706 | default does not traverse symlinks and instead returns attributes for |
| 1707 | the symlink. |
| 1708 | |
| 1709 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1710 | win32_stat will first explicitly resolve the symlink target and then will |
| 1711 | call win32_lstat on that result. |
| 1712 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1713 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1714 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1715 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1716 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1717 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1718 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1721 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1722 | 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] | 1723 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1724 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | static int |
| 1728 | win32_stat(const char* path, struct win32_stat *result) |
| 1729 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1730 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1733 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1734 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1735 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1736 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | static int |
| 1740 | win32_fstat(int file_number, struct win32_stat *result) |
| 1741 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1742 | BY_HANDLE_FILE_INFORMATION info; |
| 1743 | HANDLE h; |
| 1744 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1745 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1746 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1747 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1748 | /* Protocol violation: we explicitly clear errno, instead of |
| 1749 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1750 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1751 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1752 | if (h == INVALID_HANDLE_VALUE) { |
| 1753 | /* This is really a C library error (invalid file handle). |
| 1754 | We set the Win32 error to the closes one matching. */ |
| 1755 | SetLastError(ERROR_INVALID_HANDLE); |
| 1756 | return -1; |
| 1757 | } |
| 1758 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1759 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1760 | type = GetFileType(h); |
| 1761 | if (type == FILE_TYPE_UNKNOWN) { |
| 1762 | DWORD error = GetLastError(); |
| 1763 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1764 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1765 | } |
| 1766 | /* else: valid but unknown file */ |
| 1767 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1768 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1769 | if (type != FILE_TYPE_DISK) { |
| 1770 | if (type == FILE_TYPE_CHAR) |
| 1771 | result->st_mode = _S_IFCHR; |
| 1772 | else if (type == FILE_TYPE_PIPE) |
| 1773 | result->st_mode = _S_IFIFO; |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
| 1777 | if (!GetFileInformationByHandle(h, &info)) { |
| 1778 | return -1; |
| 1779 | } |
| 1780 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1781 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1782 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1783 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1784 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | #endif /* MS_WINDOWS */ |
| 1788 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1789 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1790 | "stat_result: Result from stat or lstat.\n\n\ |
| 1791 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1792 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1793 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1794 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1795 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1796 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1797 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1798 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1799 | |
| 1800 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1801 | {"st_mode", "protection bits"}, |
| 1802 | {"st_ino", "inode"}, |
| 1803 | {"st_dev", "device"}, |
| 1804 | {"st_nlink", "number of hard links"}, |
| 1805 | {"st_uid", "user ID of owner"}, |
| 1806 | {"st_gid", "group ID of owner"}, |
| 1807 | {"st_size", "total size, in bytes"}, |
| 1808 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1809 | {NULL, "integer time of last access"}, |
| 1810 | {NULL, "integer time of last modification"}, |
| 1811 | {NULL, "integer time of last change"}, |
| 1812 | {"st_atime", "time of last access"}, |
| 1813 | {"st_mtime", "time of last modification"}, |
| 1814 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1815 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1816 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1817 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1818 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1819 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1820 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1821 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1822 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1823 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1824 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1825 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1826 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1827 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1828 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1829 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1830 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1831 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1832 | #endif |
| 1833 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1834 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1835 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1836 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1837 | }; |
| 1838 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1839 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1840 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1841 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1842 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1843 | #endif |
| 1844 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1845 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1846 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1847 | #else |
| 1848 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1849 | #endif |
| 1850 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1851 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1852 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1853 | #else |
| 1854 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1855 | #endif |
| 1856 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1857 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1858 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1859 | #else |
| 1860 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1861 | #endif |
| 1862 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1863 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1864 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1865 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1866 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1867 | #endif |
| 1868 | |
| 1869 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1870 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1871 | #else |
| 1872 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1873 | #endif |
| 1874 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1875 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1876 | "stat_result", /* name */ |
| 1877 | stat_result__doc__, /* doc */ |
| 1878 | stat_result_fields, |
| 1879 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1880 | }; |
| 1881 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1882 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1883 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1884 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1885 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1886 | 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] | 1887 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1888 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1889 | |
| 1890 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1891 | {"f_bsize", }, |
| 1892 | {"f_frsize", }, |
| 1893 | {"f_blocks", }, |
| 1894 | {"f_bfree", }, |
| 1895 | {"f_bavail", }, |
| 1896 | {"f_files", }, |
| 1897 | {"f_ffree", }, |
| 1898 | {"f_favail", }, |
| 1899 | {"f_flag", }, |
| 1900 | {"f_namemax",}, |
| 1901 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1902 | }; |
| 1903 | |
| 1904 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1905 | "statvfs_result", /* name */ |
| 1906 | statvfs_result__doc__, /* doc */ |
| 1907 | statvfs_result_fields, |
| 1908 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1909 | }; |
| 1910 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1911 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1912 | PyDoc_STRVAR(waitid_result__doc__, |
| 1913 | "waitid_result: Result from waitid.\n\n\ |
| 1914 | This object may be accessed either as a tuple of\n\ |
| 1915 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1916 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1917 | \n\ |
| 1918 | See os.waitid for more information."); |
| 1919 | |
| 1920 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1921 | {"si_pid", }, |
| 1922 | {"si_uid", }, |
| 1923 | {"si_signo", }, |
| 1924 | {"si_status", }, |
| 1925 | {"si_code", }, |
| 1926 | {0} |
| 1927 | }; |
| 1928 | |
| 1929 | static PyStructSequence_Desc waitid_result_desc = { |
| 1930 | "waitid_result", /* name */ |
| 1931 | waitid_result__doc__, /* doc */ |
| 1932 | waitid_result_fields, |
| 1933 | 5 |
| 1934 | }; |
| 1935 | static PyTypeObject WaitidResultType; |
| 1936 | #endif |
| 1937 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1938 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1939 | static PyTypeObject StatResultType; |
| 1940 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1941 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1942 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1943 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1944 | static newfunc structseq_new; |
| 1945 | |
| 1946 | static PyObject * |
| 1947 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1948 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1949 | PyStructSequence *result; |
| 1950 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1951 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1952 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1953 | if (!result) |
| 1954 | return NULL; |
| 1955 | /* If we have been initialized from a tuple, |
| 1956 | st_?time might be set to None. Initialize it |
| 1957 | from the int slots. */ |
| 1958 | for (i = 7; i <= 9; i++) { |
| 1959 | if (result->ob_item[i+3] == Py_None) { |
| 1960 | Py_DECREF(Py_None); |
| 1961 | Py_INCREF(result->ob_item[i]); |
| 1962 | result->ob_item[i+3] = result->ob_item[i]; |
| 1963 | } |
| 1964 | } |
| 1965 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | |
| 1969 | |
| 1970 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1971 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1972 | |
| 1973 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1974 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1975 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1976 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1977 | future calls return ints. \n\ |
| 1978 | If newval is omitted, return the current setting.\n"); |
| 1979 | |
| 1980 | static PyObject* |
| 1981 | stat_float_times(PyObject* self, PyObject *args) |
| 1982 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1983 | int newval = -1; |
| 1984 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1985 | return NULL; |
| 1986 | if (newval == -1) |
| 1987 | /* Return old value */ |
| 1988 | return PyBool_FromLong(_stat_float_times); |
| 1989 | _stat_float_times = newval; |
| 1990 | Py_INCREF(Py_None); |
| 1991 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1992 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1993 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1994 | static PyObject *billion = NULL; |
| 1995 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1996 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1997 | 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] | 1998 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1999 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2000 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2001 | PyObject *s_in_ns = NULL; |
| 2002 | PyObject *ns_total = NULL; |
| 2003 | PyObject *float_s = NULL; |
| 2004 | |
| 2005 | if (!(s && ns_fractional)) |
| 2006 | goto exit; |
| 2007 | |
| 2008 | s_in_ns = PyNumber_Multiply(s, billion); |
| 2009 | if (!s_in_ns) |
| 2010 | goto exit; |
| 2011 | |
| 2012 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2013 | if (!ns_total) |
| 2014 | goto exit; |
| 2015 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2016 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2017 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2018 | if (!float_s) |
| 2019 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2020 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2021 | else { |
| 2022 | float_s = s; |
| 2023 | Py_INCREF(float_s); |
| 2024 | } |
| 2025 | |
| 2026 | PyStructSequence_SET_ITEM(v, index, s); |
| 2027 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2028 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2029 | s = NULL; |
| 2030 | float_s = NULL; |
| 2031 | ns_total = NULL; |
| 2032 | exit: |
| 2033 | Py_XDECREF(s); |
| 2034 | Py_XDECREF(ns_fractional); |
| 2035 | Py_XDECREF(s_in_ns); |
| 2036 | Py_XDECREF(ns_total); |
| 2037 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2040 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2041 | (used by posix_stat() and posix_fstat()) */ |
| 2042 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2043 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2044 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2045 | unsigned long ansec, mnsec, cnsec; |
| 2046 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 2047 | if (v == NULL) |
| 2048 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2049 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2050 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2051 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2052 | PyStructSequence_SET_ITEM(v, 1, |
| 2053 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2054 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2055 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2056 | #endif |
| 2057 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2058 | PyStructSequence_SET_ITEM(v, 2, |
| 2059 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2060 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2061 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2062 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2063 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 2064 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 2065 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2066 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2067 | PyStructSequence_SET_ITEM(v, 6, |
| 2068 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2069 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2070 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2071 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2072 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2073 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2074 | ansec = st->st_atim.tv_nsec; |
| 2075 | mnsec = st->st_mtim.tv_nsec; |
| 2076 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2077 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2078 | ansec = st->st_atimespec.tv_nsec; |
| 2079 | mnsec = st->st_mtimespec.tv_nsec; |
| 2080 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2081 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2082 | ansec = st->st_atime_nsec; |
| 2083 | mnsec = st->st_mtime_nsec; |
| 2084 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2085 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2086 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2087 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2088 | fill_time(v, 7, st->st_atime, ansec); |
| 2089 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2090 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2091 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2092 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2093 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2094 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2095 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2096 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2097 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2098 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2099 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2100 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2101 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2102 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2103 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2104 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2105 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2106 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2107 | #endif |
| 2108 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2109 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2110 | PyObject *val; |
| 2111 | unsigned long bsec,bnsec; |
| 2112 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2113 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2114 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2115 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2116 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2117 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2118 | if (_stat_float_times) { |
| 2119 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 2120 | } else { |
| 2121 | val = PyLong_FromLong((long)bsec); |
| 2122 | } |
| 2123 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2124 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2125 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2126 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2127 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2128 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2129 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2130 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2131 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2132 | if (PyErr_Occurred()) { |
| 2133 | Py_DECREF(v); |
| 2134 | return NULL; |
| 2135 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2136 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2137 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2140 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2141 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2142 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2143 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2144 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2145 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2146 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2147 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2148 | char *wformat, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2149 | int (*wstatfunc)(const wchar_t *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2150 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2151 | STRUCT_STAT st; |
| 2152 | PyObject *opath; |
| 2153 | char *path; |
| 2154 | int res; |
| 2155 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2156 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2157 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2158 | PyObject *po; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2159 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2160 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 2161 | if (wpath == NULL) |
| 2162 | return NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2163 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2164 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2165 | res = wstatfunc(wpath, &st); |
| 2166 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2167 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2168 | if (res != 0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2169 | return win32_error_object("stat", po); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2170 | return _pystat_fromstructstat(&st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2171 | } |
| 2172 | /* Drop the argument parsing error as narrow strings |
| 2173 | are also valid. */ |
| 2174 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2175 | #endif |
| 2176 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2177 | if (!PyArg_ParseTuple(args, format, |
| 2178 | PyUnicode_FSConverter, &opath)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2179 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2180 | #ifdef MS_WINDOWS |
| 2181 | if (win32_warn_bytes_api()) { |
| 2182 | Py_DECREF(opath); |
| 2183 | return NULL; |
| 2184 | } |
| 2185 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2186 | path = PyBytes_AsString(opath); |
| 2187 | Py_BEGIN_ALLOW_THREADS |
| 2188 | res = (*statfunc)(path, &st); |
| 2189 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2190 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2191 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2192 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2193 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2194 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2195 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2196 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2197 | } |
| 2198 | else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2199 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2200 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2201 | Py_DECREF(opath); |
| 2202 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2205 | /* POSIX methods */ |
| 2206 | |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 2207 | #ifdef AT_FDCWD |
| 2208 | #define DEFAULT_DIR_FD AT_FDCWD |
| 2209 | #else |
| 2210 | #define DEFAULT_DIR_FD (-100) |
| 2211 | #endif |
| 2212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2213 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 2214 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 2215 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 2216 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 2217 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 2218 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 2219 | existence, or the inclusive-OR of R_OK, W_OK, and X_OK."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2220 | |
| 2221 | static PyObject * |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 2222 | posix_access(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2223 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2224 | int mode; |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 2225 | int dir_fd = DEFAULT_DIR_FD; |
| 2226 | wchar_t *wide_path; |
| 2227 | char *path; |
| 2228 | int follow_symlinks = 1; |
| 2229 | int effective_ids = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2230 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2231 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2232 | DWORD attr; |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 2233 | #else |
| 2234 | int res; |
| 2235 | #endif |
| 2236 | |
| 2237 | APT_DECLARE(apt, "access"); |
| 2238 | APT_ARGUMENT_PATH(apt, "path", &wide_path, &path, NULL); |
| 2239 | APT_ARGUMENT_INT(apt, "mode", &mode); |
| 2240 | APT_OPTIONAL(apt); |
| 2241 | APT_KEYWORD_ONLY(apt); |
| 2242 | APT_ARGUMENT_INT(apt, "dir_fd", &dir_fd); |
| 2243 | APT_ARGUMENT_BOOL(apt, "follow_symlinks", &follow_symlinks); |
| 2244 | APT_ARGUMENT_BOOL(apt, "effective_ids", &effective_ids); |
| 2245 | |
| 2246 | #define ACCESS_FAIL_IF_KEYWORD_USED \ |
| 2247 | if (dir_fd != DEFAULT_DIR_FD) \ |
| 2248 | PyErr_SetString(PyExc_NotImplementedError, "access: dir_fd unavailable on this platform"); \ |
| 2249 | if (!follow_symlinks) \ |
| 2250 | PyErr_SetString(PyExc_NotImplementedError, "access: follow_symlinks unavailable on this platform"); \ |
| 2251 | if (effective_ids) \ |
| 2252 | PyErr_SetString(PyExc_NotImplementedError, "access: effective_ids unavailable on this platform") |
| 2253 | |
| 2254 | if (!apt_parse(&apt)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2255 | return NULL; |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 2256 | Py_RETURN_NONE; |
| 2257 | |
| 2258 | #ifdef MS_WINDOWS |
| 2259 | ACCESS_FAIL_IF_KEYWORD_USED; |
| 2260 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2261 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 2262 | if (wide != NULL) |
| 2263 | attr = GetFileAttributesW(wide_path); |
| 2264 | else |
| 2265 | attr = GetFileAttributesA(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2266 | Py_END_ALLOW_THREADS |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 2267 | |
| 2268 | apt_cleanup(&apt); |
| 2269 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2270 | if (attr == 0xFFFFFFFF) |
| 2271 | /* File does not exist, or cannot read attributes */ |
| 2272 | return PyBool_FromLong(0); |
| 2273 | /* Access is possible if either write access wasn't requested, or |
| 2274 | the file isn't read-only, or if it's a directory, as there are |
| 2275 | no read-only directories on Windows. */ |
| 2276 | return PyBool_FromLong(!(mode & 2) |
| 2277 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 2278 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2279 | #else |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 2280 | |
| 2281 | if ((dir_fd != DEFAULT_DIR_FD) |
| 2282 | || effective_ids || !follow_symlinks) { |
| 2283 | #ifdef HAVE_FACCESSAT |
| 2284 | int flags = 0; |
| 2285 | if (!follow_symlinks) |
| 2286 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2287 | if (effective_ids) |
| 2288 | flags |= AT_EACCESS; |
| 2289 | Py_BEGIN_ALLOW_THREADS |
| 2290 | res = faccessat(dir_fd, path, mode, flags); |
| 2291 | Py_END_ALLOW_THREADS |
| 2292 | #else |
| 2293 | ACCESS_FAIL_IF_KEYWORD_USED; |
| 2294 | #endif |
| 2295 | } else { |
| 2296 | Py_BEGIN_ALLOW_THREADS |
| 2297 | res = access(path, mode); |
| 2298 | Py_END_ALLOW_THREADS |
| 2299 | } |
| 2300 | |
| 2301 | apt_cleanup(&apt); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2302 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2303 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2306 | #ifndef F_OK |
| 2307 | #define F_OK 0 |
| 2308 | #endif |
| 2309 | #ifndef R_OK |
| 2310 | #define R_OK 4 |
| 2311 | #endif |
| 2312 | #ifndef W_OK |
| 2313 | #define W_OK 2 |
| 2314 | #endif |
| 2315 | #ifndef X_OK |
| 2316 | #define X_OK 1 |
| 2317 | #endif |
| 2318 | |
| 2319 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2320 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2321 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2322 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2323 | |
| 2324 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2325 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2326 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2327 | int id; |
| 2328 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2329 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2330 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 2331 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2332 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2333 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2334 | /* file descriptor 0 only, the default input device (stdin) */ |
| 2335 | if (id == 0) { |
| 2336 | ret = ttyname(); |
| 2337 | } |
| 2338 | else { |
| 2339 | ret = NULL; |
| 2340 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2341 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2342 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2343 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2344 | if (ret == NULL) |
| 2345 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2346 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2347 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2348 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2349 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2350 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2351 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2352 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2353 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2354 | |
| 2355 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2356 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2357 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2358 | char *ret; |
| 2359 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2360 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2361 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2362 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2363 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2364 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2365 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2366 | if (ret == NULL) |
| 2367 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2368 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2369 | } |
| 2370 | #endif |
| 2371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2372 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2373 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2374 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2375 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2376 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2377 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2378 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2379 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2380 | return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2381 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2382 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 2383 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2384 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2385 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2386 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2387 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2390 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2391 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2392 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2393 | Change to the directory of the given file descriptor. fildes must be\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2394 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2395 | |
| 2396 | static PyObject * |
| 2397 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2398 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2399 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2400 | } |
| 2401 | #endif /* HAVE_FCHDIR */ |
| 2402 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2404 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2405 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2406 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2407 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2408 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2409 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2410 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2411 | PyObject *opath = NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2412 | const char *path = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2413 | int i; |
| 2414 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2415 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2416 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2417 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2418 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2419 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 2420 | if (wpath == NULL) |
| 2421 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2422 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2423 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2424 | if (attr != 0xFFFFFFFF) { |
| 2425 | if (i & _S_IWRITE) |
| 2426 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2427 | else |
| 2428 | attr |= FILE_ATTRIBUTE_READONLY; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2429 | res = SetFileAttributesW(wpath, attr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2430 | } |
| 2431 | else |
| 2432 | res = 0; |
| 2433 | Py_END_ALLOW_THREADS |
| 2434 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2435 | return win32_error_object("chmod", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2436 | Py_INCREF(Py_None); |
| 2437 | return Py_None; |
| 2438 | } |
| 2439 | /* Drop the argument parsing error as narrow strings |
| 2440 | are also valid. */ |
| 2441 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2442 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2443 | if (!PyArg_ParseTuple(args, "yi:chmod", &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2444 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2445 | if (win32_warn_bytes_api()) |
| 2446 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2447 | Py_BEGIN_ALLOW_THREADS |
| 2448 | attr = GetFileAttributesA(path); |
| 2449 | if (attr != 0xFFFFFFFF) { |
| 2450 | if (i & _S_IWRITE) |
| 2451 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2452 | else |
| 2453 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2454 | res = SetFileAttributesA(path, attr); |
| 2455 | } |
| 2456 | else |
| 2457 | res = 0; |
| 2458 | Py_END_ALLOW_THREADS |
| 2459 | if (!res) { |
| 2460 | win32_error("chmod", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2461 | return NULL; |
| 2462 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2463 | Py_INCREF(Py_None); |
| 2464 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2465 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2466 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2467 | &opath, &i)) |
| 2468 | return NULL; |
| 2469 | path = PyBytes_AsString(opath); |
| 2470 | Py_BEGIN_ALLOW_THREADS |
| 2471 | res = chmod(path, i); |
| 2472 | Py_END_ALLOW_THREADS |
| 2473 | if (res < 0) |
| 2474 | return posix_error_with_allocated_filename(opath); |
| 2475 | Py_DECREF(opath); |
| 2476 | Py_INCREF(Py_None); |
| 2477 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2478 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2481 | #ifdef HAVE_FCHMOD |
| 2482 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2483 | "fchmod(fd, mode)\n\n\ |
| 2484 | Change the access permissions of the file given by file\n\ |
| 2485 | descriptor fd."); |
| 2486 | |
| 2487 | static PyObject * |
| 2488 | posix_fchmod(PyObject *self, PyObject *args) |
| 2489 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2490 | int fd, mode, res; |
| 2491 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2492 | return NULL; |
| 2493 | Py_BEGIN_ALLOW_THREADS |
| 2494 | res = fchmod(fd, mode); |
| 2495 | Py_END_ALLOW_THREADS |
| 2496 | if (res < 0) |
| 2497 | return posix_error(); |
| 2498 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2499 | } |
| 2500 | #endif /* HAVE_FCHMOD */ |
| 2501 | |
| 2502 | #ifdef HAVE_LCHMOD |
| 2503 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2504 | "lchmod(path, mode)\n\n\ |
| 2505 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2506 | affects the link itself rather than the target."); |
| 2507 | |
| 2508 | static PyObject * |
| 2509 | posix_lchmod(PyObject *self, PyObject *args) |
| 2510 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2511 | PyObject *opath; |
| 2512 | char *path; |
| 2513 | int i; |
| 2514 | int res; |
| 2515 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2516 | &opath, &i)) |
| 2517 | return NULL; |
| 2518 | path = PyBytes_AsString(opath); |
| 2519 | Py_BEGIN_ALLOW_THREADS |
| 2520 | res = lchmod(path, i); |
| 2521 | Py_END_ALLOW_THREADS |
| 2522 | if (res < 0) |
| 2523 | return posix_error_with_allocated_filename(opath); |
| 2524 | Py_DECREF(opath); |
| 2525 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2526 | } |
| 2527 | #endif /* HAVE_LCHMOD */ |
| 2528 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2529 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2530 | #ifdef HAVE_CHFLAGS |
| 2531 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2532 | "chflags(path, flags)\n\n\ |
| 2533 | Set file flags."); |
| 2534 | |
| 2535 | static PyObject * |
| 2536 | posix_chflags(PyObject *self, PyObject *args) |
| 2537 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2538 | PyObject *opath; |
| 2539 | char *path; |
| 2540 | unsigned long flags; |
| 2541 | int res; |
| 2542 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2543 | PyUnicode_FSConverter, &opath, &flags)) |
| 2544 | return NULL; |
| 2545 | path = PyBytes_AsString(opath); |
| 2546 | Py_BEGIN_ALLOW_THREADS |
| 2547 | res = chflags(path, flags); |
| 2548 | Py_END_ALLOW_THREADS |
| 2549 | if (res < 0) |
| 2550 | return posix_error_with_allocated_filename(opath); |
| 2551 | Py_DECREF(opath); |
| 2552 | Py_INCREF(Py_None); |
| 2553 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2554 | } |
| 2555 | #endif /* HAVE_CHFLAGS */ |
| 2556 | |
| 2557 | #ifdef HAVE_LCHFLAGS |
| 2558 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2559 | "lchflags(path, flags)\n\n\ |
| 2560 | Set file flags.\n\ |
| 2561 | This function will not follow symbolic links."); |
| 2562 | |
| 2563 | static PyObject * |
| 2564 | posix_lchflags(PyObject *self, PyObject *args) |
| 2565 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2566 | PyObject *opath; |
| 2567 | char *path; |
| 2568 | unsigned long flags; |
| 2569 | int res; |
| 2570 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2571 | PyUnicode_FSConverter, &opath, &flags)) |
| 2572 | return NULL; |
| 2573 | path = PyBytes_AsString(opath); |
| 2574 | Py_BEGIN_ALLOW_THREADS |
| 2575 | res = lchflags(path, flags); |
| 2576 | Py_END_ALLOW_THREADS |
| 2577 | if (res < 0) |
| 2578 | return posix_error_with_allocated_filename(opath); |
| 2579 | Py_DECREF(opath); |
| 2580 | Py_INCREF(Py_None); |
| 2581 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2582 | } |
| 2583 | #endif /* HAVE_LCHFLAGS */ |
| 2584 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2585 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2586 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2587 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2588 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2589 | |
| 2590 | static PyObject * |
| 2591 | posix_chroot(PyObject *self, PyObject *args) |
| 2592 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2593 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2594 | } |
| 2595 | #endif |
| 2596 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2597 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2598 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2599 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2600 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2601 | |
| 2602 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2603 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2604 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2605 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2606 | } |
| 2607 | #endif /* HAVE_FSYNC */ |
| 2608 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2609 | #ifdef HAVE_SYNC |
| 2610 | PyDoc_STRVAR(posix_sync__doc__, |
| 2611 | "sync()\n\n\ |
| 2612 | Force write of everything to disk."); |
| 2613 | |
| 2614 | static PyObject * |
| 2615 | posix_sync(PyObject *self, PyObject *noargs) |
| 2616 | { |
| 2617 | Py_BEGIN_ALLOW_THREADS |
| 2618 | sync(); |
| 2619 | Py_END_ALLOW_THREADS |
| 2620 | Py_RETURN_NONE; |
| 2621 | } |
| 2622 | #endif |
| 2623 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2624 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2625 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2626 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2627 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2628 | #endif |
| 2629 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2630 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2631 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2632 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2633 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2634 | |
| 2635 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2636 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2637 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2638 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2639 | } |
| 2640 | #endif /* HAVE_FDATASYNC */ |
| 2641 | |
| 2642 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2643 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2644 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2645 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2646 | Change the owner and group id of path to the numeric uid and gid."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2647 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2648 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2649 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2650 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2651 | PyObject *opath; |
| 2652 | char *path; |
| 2653 | long uid, gid; |
| 2654 | int res; |
| 2655 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2656 | PyUnicode_FSConverter, &opath, |
| 2657 | &uid, &gid)) |
| 2658 | return NULL; |
| 2659 | path = PyBytes_AsString(opath); |
| 2660 | Py_BEGIN_ALLOW_THREADS |
| 2661 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2662 | Py_END_ALLOW_THREADS |
| 2663 | if (res < 0) |
| 2664 | return posix_error_with_allocated_filename(opath); |
| 2665 | Py_DECREF(opath); |
| 2666 | Py_INCREF(Py_None); |
| 2667 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2668 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2669 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2670 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2671 | #ifdef HAVE_FCHOWN |
| 2672 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2673 | "fchown(fd, uid, gid)\n\n\ |
| 2674 | Change the owner and group id of the file given by file descriptor\n\ |
| 2675 | fd to the numeric uid and gid."); |
| 2676 | |
| 2677 | static PyObject * |
| 2678 | posix_fchown(PyObject *self, PyObject *args) |
| 2679 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2680 | int fd; |
| 2681 | long uid, gid; |
| 2682 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2683 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2684 | return NULL; |
| 2685 | Py_BEGIN_ALLOW_THREADS |
| 2686 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2687 | Py_END_ALLOW_THREADS |
| 2688 | if (res < 0) |
| 2689 | return posix_error(); |
| 2690 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2691 | } |
| 2692 | #endif /* HAVE_FCHOWN */ |
| 2693 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2694 | #ifdef HAVE_LCHOWN |
| 2695 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2696 | "lchown(path, uid, gid)\n\n\ |
| 2697 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2698 | This function will not follow symbolic links."); |
| 2699 | |
| 2700 | static PyObject * |
| 2701 | posix_lchown(PyObject *self, PyObject *args) |
| 2702 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2703 | PyObject *opath; |
| 2704 | char *path; |
| 2705 | long uid, gid; |
| 2706 | int res; |
| 2707 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2708 | PyUnicode_FSConverter, &opath, |
| 2709 | &uid, &gid)) |
| 2710 | return NULL; |
| 2711 | path = PyBytes_AsString(opath); |
| 2712 | Py_BEGIN_ALLOW_THREADS |
| 2713 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2714 | Py_END_ALLOW_THREADS |
| 2715 | if (res < 0) |
| 2716 | return posix_error_with_allocated_filename(opath); |
| 2717 | Py_DECREF(opath); |
| 2718 | Py_INCREF(Py_None); |
| 2719 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2720 | } |
| 2721 | #endif /* HAVE_LCHOWN */ |
| 2722 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2723 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2724 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2725 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2726 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2727 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2728 | char buf[1026]; |
| 2729 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2730 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2731 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2732 | if (!use_bytes) { |
| 2733 | wchar_t wbuf[1026]; |
| 2734 | wchar_t *wbuf2 = wbuf; |
| 2735 | PyObject *resobj; |
| 2736 | DWORD len; |
| 2737 | Py_BEGIN_ALLOW_THREADS |
| 2738 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2739 | /* If the buffer is large enough, len does not include the |
| 2740 | terminating \0. If the buffer is too small, len includes |
| 2741 | the space needed for the terminator. */ |
| 2742 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2743 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2744 | if (wbuf2) |
| 2745 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2746 | } |
| 2747 | Py_END_ALLOW_THREADS |
| 2748 | if (!wbuf2) { |
| 2749 | PyErr_NoMemory(); |
| 2750 | return NULL; |
| 2751 | } |
| 2752 | if (!len) { |
| 2753 | if (wbuf2 != wbuf) free(wbuf2); |
| 2754 | return win32_error("getcwdu", NULL); |
| 2755 | } |
| 2756 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2757 | if (wbuf2 != wbuf) free(wbuf2); |
| 2758 | return resobj; |
| 2759 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 2760 | |
| 2761 | if (win32_warn_bytes_api()) |
| 2762 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2763 | #endif |
| 2764 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2765 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2766 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2767 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2768 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2769 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2770 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2771 | Py_END_ALLOW_THREADS |
| 2772 | if (res == NULL) |
| 2773 | return posix_error(); |
| 2774 | if (use_bytes) |
| 2775 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2776 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2777 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2778 | |
| 2779 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2780 | "getcwd() -> path\n\n\ |
| 2781 | Return a unicode string representing the current working directory."); |
| 2782 | |
| 2783 | static PyObject * |
| 2784 | posix_getcwd_unicode(PyObject *self) |
| 2785 | { |
| 2786 | return posix_getcwd(0); |
| 2787 | } |
| 2788 | |
| 2789 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2790 | "getcwdb() -> path\n\n\ |
| 2791 | Return a bytes string representing the current working directory."); |
| 2792 | |
| 2793 | static PyObject * |
| 2794 | posix_getcwd_bytes(PyObject *self) |
| 2795 | { |
| 2796 | return posix_getcwd(1); |
| 2797 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2798 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2799 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2800 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2801 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2802 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2803 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2804 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2805 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2806 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2807 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2808 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2809 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2810 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2811 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2812 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2813 | #ifdef MS_WINDOWS |
| 2814 | PyDoc_STRVAR(win32_link__doc__, |
| 2815 | "link(src, dst)\n\n\ |
| 2816 | Create a hard link to a file."); |
| 2817 | |
| 2818 | static PyObject * |
| 2819 | win32_link(PyObject *self, PyObject *args) |
| 2820 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2821 | PyObject *src, *dst; |
| 2822 | BOOL ok; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2823 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2824 | if (PyArg_ParseTuple(args, "UU:link", &src, &dst)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2825 | { |
| 2826 | wchar_t *wsrc, *wdst; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2827 | |
| 2828 | wsrc = PyUnicode_AsUnicode(src); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2829 | if (wsrc == NULL) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2830 | goto error; |
| 2831 | wdst = PyUnicode_AsUnicode(dst); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2832 | if (wdst == NULL) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2833 | goto error; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2834 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2835 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2836 | ok = CreateHardLinkW(wdst, wsrc, NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2837 | Py_END_ALLOW_THREADS |
| 2838 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2839 | if (!ok) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2840 | return win32_error("link", NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2841 | Py_RETURN_NONE; |
| 2842 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2843 | else { |
| 2844 | PyErr_Clear(); |
| 2845 | if (!PyArg_ParseTuple(args, "O&O&:link", |
| 2846 | PyUnicode_FSConverter, &src, |
| 2847 | PyUnicode_FSConverter, &dst)) |
| 2848 | return NULL; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2849 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2850 | if (win32_warn_bytes_api()) |
| 2851 | goto error; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2852 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2853 | Py_BEGIN_ALLOW_THREADS |
| 2854 | ok = CreateHardLinkA(PyBytes_AS_STRING(dst), |
| 2855 | PyBytes_AS_STRING(src), |
| 2856 | NULL); |
| 2857 | Py_END_ALLOW_THREADS |
| 2858 | |
| 2859 | Py_XDECREF(src); |
| 2860 | Py_XDECREF(dst); |
| 2861 | |
| 2862 | if (!ok) |
| 2863 | return win32_error("link", NULL); |
| 2864 | Py_RETURN_NONE; |
| 2865 | |
| 2866 | error: |
| 2867 | Py_XDECREF(src); |
| 2868 | Py_XDECREF(dst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2869 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2870 | } |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2871 | } |
| 2872 | #endif /* MS_WINDOWS */ |
| 2873 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2874 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2875 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2876 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2877 | Return a list containing the names of the entries in the directory.\n\ |
| 2878 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2879 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2880 | \n\ |
| 2881 | The list is in arbitrary order. It does not include the special\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2882 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2883 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2884 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2885 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2886 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2887 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2888 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2889 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2890 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2891 | PyObject *d, *v; |
| 2892 | HANDLE hFindFile; |
| 2893 | BOOL result; |
| 2894 | WIN32_FIND_DATA FileData; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2895 | const char *path; |
| 2896 | Py_ssize_t pathlen; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2897 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2898 | char *bufptr = namebuf; |
| 2899 | Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2900 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2901 | PyObject *po = NULL; |
| 2902 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2903 | WIN32_FIND_DATAW wFileData; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2904 | wchar_t *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2905 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2906 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2907 | po_wchars = L"."; |
| 2908 | len = 1; |
| 2909 | } else { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 2910 | po_wchars = PyUnicode_AsUnicodeAndSize(po, &len); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2911 | if (po_wchars == NULL) |
| 2912 | return NULL; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2913 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2914 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2915 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2916 | if (!wnamebuf) { |
| 2917 | PyErr_NoMemory(); |
| 2918 | return NULL; |
| 2919 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2920 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2921 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2922 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2923 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2924 | wnamebuf[len++] = L'\\'; |
| 2925 | wcscpy(wnamebuf + len, L"*.*"); |
| 2926 | } |
| 2927 | if ((d = PyList_New(0)) == NULL) { |
| 2928 | free(wnamebuf); |
| 2929 | return NULL; |
| 2930 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2931 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2932 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2933 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2934 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2935 | int error = GetLastError(); |
| 2936 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2937 | free(wnamebuf); |
| 2938 | return d; |
| 2939 | } |
| 2940 | Py_DECREF(d); |
| 2941 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2942 | free(wnamebuf); |
| 2943 | return NULL; |
| 2944 | } |
| 2945 | do { |
| 2946 | /* Skip over . and .. */ |
| 2947 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2948 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 2949 | v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2950 | if (v == NULL) { |
| 2951 | Py_DECREF(d); |
| 2952 | d = NULL; |
| 2953 | break; |
| 2954 | } |
| 2955 | if (PyList_Append(d, v) != 0) { |
| 2956 | Py_DECREF(v); |
| 2957 | Py_DECREF(d); |
| 2958 | d = NULL; |
| 2959 | break; |
| 2960 | } |
| 2961 | Py_DECREF(v); |
| 2962 | } |
| 2963 | Py_BEGIN_ALLOW_THREADS |
| 2964 | result = FindNextFileW(hFindFile, &wFileData); |
| 2965 | Py_END_ALLOW_THREADS |
| 2966 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2967 | it got to the end of the directory. */ |
| 2968 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2969 | Py_DECREF(d); |
| 2970 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2971 | FindClose(hFindFile); |
| 2972 | free(wnamebuf); |
| 2973 | return NULL; |
| 2974 | } |
| 2975 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2976 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2977 | if (FindClose(hFindFile) == FALSE) { |
| 2978 | Py_DECREF(d); |
| 2979 | win32_error_unicode("FindClose", wnamebuf); |
| 2980 | free(wnamebuf); |
| 2981 | return NULL; |
| 2982 | } |
| 2983 | free(wnamebuf); |
| 2984 | return d; |
| 2985 | } |
| 2986 | /* Drop the argument parsing error as narrow strings |
| 2987 | are also valid. */ |
| 2988 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2989 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2990 | if (!PyArg_ParseTuple(args, "y#:listdir", &path, &pathlen)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2991 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2992 | if (win32_warn_bytes_api()) |
| 2993 | return NULL; |
| 2994 | if (pathlen+1 > MAX_PATH) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2995 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2996 | return NULL; |
| 2997 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2998 | strcpy(namebuf, path); |
| 2999 | len = pathlen; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3000 | if (len > 0) { |
| 3001 | char ch = namebuf[len-1]; |
| 3002 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 3003 | namebuf[len++] = '/'; |
| 3004 | strcpy(namebuf + len, "*.*"); |
| 3005 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3006 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3007 | if ((d = PyList_New(0)) == NULL) |
| 3008 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3009 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3010 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3011 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3012 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3013 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3014 | int error = GetLastError(); |
| 3015 | if (error == ERROR_FILE_NOT_FOUND) |
| 3016 | return d; |
| 3017 | Py_DECREF(d); |
| 3018 | return win32_error("FindFirstFile", namebuf); |
| 3019 | } |
| 3020 | do { |
| 3021 | /* Skip over . and .. */ |
| 3022 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 3023 | strcmp(FileData.cFileName, "..") != 0) { |
| 3024 | v = PyBytes_FromString(FileData.cFileName); |
| 3025 | if (v == NULL) { |
| 3026 | Py_DECREF(d); |
| 3027 | d = NULL; |
| 3028 | break; |
| 3029 | } |
| 3030 | if (PyList_Append(d, v) != 0) { |
| 3031 | Py_DECREF(v); |
| 3032 | Py_DECREF(d); |
| 3033 | d = NULL; |
| 3034 | break; |
| 3035 | } |
| 3036 | Py_DECREF(v); |
| 3037 | } |
| 3038 | Py_BEGIN_ALLOW_THREADS |
| 3039 | result = FindNextFile(hFindFile, &FileData); |
| 3040 | Py_END_ALLOW_THREADS |
| 3041 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3042 | it got to the end of the directory. */ |
| 3043 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 3044 | Py_DECREF(d); |
| 3045 | win32_error("FindNextFile", namebuf); |
| 3046 | FindClose(hFindFile); |
| 3047 | return NULL; |
| 3048 | } |
| 3049 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3051 | if (FindClose(hFindFile) == FALSE) { |
| 3052 | Py_DECREF(d); |
| 3053 | return win32_error("FindClose", namebuf); |
| 3054 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3055 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3056 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3057 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 3058 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3059 | |
| 3060 | #ifndef MAX_PATH |
| 3061 | #define MAX_PATH CCHMAXPATH |
| 3062 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3063 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3064 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 3065 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3066 | PyObject *d, *v; |
| 3067 | char namebuf[MAX_PATH+5]; |
| 3068 | HDIR hdir = 1; |
| 3069 | ULONG srchcnt = 1; |
| 3070 | FILEFINDBUF3 ep; |
| 3071 | APIRET rc; |
| 3072 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3073 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3074 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3075 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3076 | name = PyBytes_AsString(oname); |
| 3077 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3078 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3079 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 3080 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3081 | return NULL; |
| 3082 | } |
| 3083 | strcpy(namebuf, name); |
| 3084 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3085 | if (*pt == ALTSEP) |
| 3086 | *pt = SEP; |
| 3087 | if (namebuf[len-1] != SEP) |
| 3088 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3089 | strcpy(namebuf + len, "*.*"); |
| 3090 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 3091 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3092 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3093 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 3094 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3095 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3096 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 3097 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3098 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3099 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 3100 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 3101 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3102 | |
| 3103 | if (rc != NO_ERROR) { |
| 3104 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3105 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3108 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3109 | do { |
| 3110 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3111 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3112 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3113 | |
| 3114 | strcpy(namebuf, ep.achName); |
| 3115 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3116 | /* Leave Case of Name Alone -- In Native Form */ |
| 3117 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3118 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3119 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3120 | if (v == NULL) { |
| 3121 | Py_DECREF(d); |
| 3122 | d = NULL; |
| 3123 | break; |
| 3124 | } |
| 3125 | if (PyList_Append(d, v) != 0) { |
| 3126 | Py_DECREF(v); |
| 3127 | Py_DECREF(d); |
| 3128 | d = NULL; |
| 3129 | break; |
| 3130 | } |
| 3131 | Py_DECREF(v); |
| 3132 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 3133 | } |
| 3134 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3135 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3136 | return d; |
| 3137 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3138 | PyObject *oname; |
| 3139 | char *name; |
| 3140 | PyObject *d, *v; |
| 3141 | DIR *dirp; |
| 3142 | struct dirent *ep; |
| 3143 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 3144 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3145 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3146 | /* v is never read, so it does not need to be initialized yet. */ |
| 3147 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3148 | arg_is_unicode = 0; |
| 3149 | PyErr_Clear(); |
| 3150 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3151 | oname = NULL; |
| 3152 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3153 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 3154 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3155 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 3156 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3157 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3158 | Py_BEGIN_ALLOW_THREADS |
| 3159 | dirp = opendir(name); |
| 3160 | Py_END_ALLOW_THREADS |
| 3161 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3162 | return posix_error_with_allocated_filename(oname); |
| 3163 | } |
| 3164 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3165 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3166 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3167 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3168 | Py_DECREF(oname); |
| 3169 | return NULL; |
| 3170 | } |
| 3171 | for (;;) { |
| 3172 | errno = 0; |
| 3173 | Py_BEGIN_ALLOW_THREADS |
| 3174 | ep = readdir(dirp); |
| 3175 | Py_END_ALLOW_THREADS |
| 3176 | if (ep == NULL) { |
| 3177 | if (errno == 0) { |
| 3178 | break; |
| 3179 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3180 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3181 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3182 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3183 | Py_DECREF(d); |
| 3184 | return posix_error_with_allocated_filename(oname); |
| 3185 | } |
| 3186 | } |
| 3187 | if (ep->d_name[0] == '.' && |
| 3188 | (NAMLEN(ep) == 1 || |
| 3189 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3190 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3191 | if (arg_is_unicode) |
| 3192 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3193 | else |
| 3194 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3195 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3196 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3197 | break; |
| 3198 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3199 | if (PyList_Append(d, v) != 0) { |
| 3200 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3201 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3202 | break; |
| 3203 | } |
| 3204 | Py_DECREF(v); |
| 3205 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3206 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3207 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 3208 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3209 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3210 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3211 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3212 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 3213 | #endif /* which OS */ |
| 3214 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3215 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 3216 | #ifdef HAVE_FDOPENDIR |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 3217 | PyDoc_STRVAR(posix_flistdir__doc__, |
| 3218 | "flistdir(fd) -> list_of_strings\n\n\ |
Charles-François Natali | 76961fa | 2012-01-10 20:25:09 +0100 | [diff] [blame] | 3219 | Like listdir(), but uses a file descriptor instead."); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 3220 | |
| 3221 | static PyObject * |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 3222 | posix_flistdir(PyObject *self, PyObject *args) |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 3223 | { |
| 3224 | PyObject *d, *v; |
| 3225 | DIR *dirp; |
| 3226 | struct dirent *ep; |
| 3227 | int fd; |
| 3228 | |
| 3229 | errno = 0; |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 3230 | if (!PyArg_ParseTuple(args, "i:flistdir", &fd)) |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 3231 | return NULL; |
Charles-François Natali | 76961fa | 2012-01-10 20:25:09 +0100 | [diff] [blame] | 3232 | /* closedir() closes the FD, so we duplicate it */ |
| 3233 | fd = dup(fd); |
| 3234 | if (fd < 0) |
| 3235 | return posix_error(); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 3236 | Py_BEGIN_ALLOW_THREADS |
| 3237 | dirp = fdopendir(fd); |
| 3238 | Py_END_ALLOW_THREADS |
| 3239 | if (dirp == NULL) { |
| 3240 | close(fd); |
| 3241 | return posix_error(); |
| 3242 | } |
| 3243 | if ((d = PyList_New(0)) == NULL) { |
| 3244 | Py_BEGIN_ALLOW_THREADS |
| 3245 | closedir(dirp); |
| 3246 | Py_END_ALLOW_THREADS |
| 3247 | return NULL; |
| 3248 | } |
| 3249 | for (;;) { |
| 3250 | errno = 0; |
| 3251 | Py_BEGIN_ALLOW_THREADS |
| 3252 | ep = readdir(dirp); |
| 3253 | Py_END_ALLOW_THREADS |
| 3254 | if (ep == NULL) { |
| 3255 | if (errno == 0) { |
| 3256 | break; |
| 3257 | } else { |
| 3258 | Py_BEGIN_ALLOW_THREADS |
Charles-François Natali | f2840a8 | 2012-01-08 20:30:47 +0100 | [diff] [blame] | 3259 | rewinddir(dirp); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 3260 | closedir(dirp); |
| 3261 | Py_END_ALLOW_THREADS |
| 3262 | Py_DECREF(d); |
| 3263 | return posix_error(); |
| 3264 | } |
| 3265 | } |
| 3266 | if (ep->d_name[0] == '.' && |
| 3267 | (NAMLEN(ep) == 1 || |
| 3268 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3269 | continue; |
| 3270 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3271 | if (v == NULL) { |
| 3272 | Py_CLEAR(d); |
| 3273 | break; |
| 3274 | } |
| 3275 | if (PyList_Append(d, v) != 0) { |
| 3276 | Py_DECREF(v); |
| 3277 | Py_CLEAR(d); |
| 3278 | break; |
| 3279 | } |
| 3280 | Py_DECREF(v); |
| 3281 | } |
| 3282 | Py_BEGIN_ALLOW_THREADS |
Charles-François Natali | f2840a8 | 2012-01-08 20:30:47 +0100 | [diff] [blame] | 3283 | rewinddir(dirp); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 3284 | closedir(dirp); |
| 3285 | Py_END_ALLOW_THREADS |
| 3286 | |
| 3287 | return d; |
| 3288 | } |
| 3289 | #endif |
| 3290 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3291 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3292 | /* A helper function for abspath on win32 */ |
| 3293 | static PyObject * |
| 3294 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 3295 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3296 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3297 | char outbuf[MAX_PATH*2]; |
| 3298 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3299 | PyObject *po; |
| 3300 | |
| 3301 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3302 | { |
| 3303 | wchar_t *wpath; |
| 3304 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 3305 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3306 | DWORD result; |
| 3307 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3308 | |
| 3309 | wpath = PyUnicode_AsUnicode(po); |
| 3310 | if (wpath == NULL) |
| 3311 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3312 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3313 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3314 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3315 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3316 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3317 | if (!woutbufp) |
| 3318 | return PyErr_NoMemory(); |
| 3319 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3320 | } |
| 3321 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3322 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3323 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3324 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3325 | if (woutbufp != woutbuf) |
| 3326 | free(woutbufp); |
| 3327 | return v; |
| 3328 | } |
| 3329 | /* Drop the argument parsing error as narrow strings |
| 3330 | are also valid. */ |
| 3331 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3332 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3333 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3334 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3335 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3336 | if (win32_warn_bytes_api()) |
| 3337 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3338 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3339 | outbuf, &temp)) { |
| 3340 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3341 | return NULL; |
| 3342 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3343 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3344 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3345 | Py_FileSystemDefaultEncoding, NULL); |
| 3346 | } |
| 3347 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3348 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3349 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3350 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3351 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3352 | /* A helper function for samepath on windows */ |
| 3353 | static PyObject * |
| 3354 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3355 | { |
| 3356 | HANDLE hFile; |
| 3357 | int buf_size; |
| 3358 | wchar_t *target_path; |
| 3359 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3360 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3361 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3362 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3363 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3364 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3365 | path = PyUnicode_AsUnicode(po); |
| 3366 | if (path == NULL) |
| 3367 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3368 | |
| 3369 | if(!check_GetFinalPathNameByHandle()) { |
| 3370 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3371 | NotImplementedError. */ |
| 3372 | return PyErr_Format(PyExc_NotImplementedError, |
| 3373 | "GetFinalPathNameByHandle not available on this platform"); |
| 3374 | } |
| 3375 | |
| 3376 | hFile = CreateFileW( |
| 3377 | path, |
| 3378 | 0, /* desired access */ |
| 3379 | 0, /* share mode */ |
| 3380 | NULL, /* security attributes */ |
| 3381 | OPEN_EXISTING, |
| 3382 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3383 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3384 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3385 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3386 | if(hFile == INVALID_HANDLE_VALUE) |
| 3387 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3388 | |
| 3389 | /* We have a good handle to the target, use it to determine the |
| 3390 | target path name. */ |
| 3391 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3392 | |
| 3393 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3394 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3395 | |
| 3396 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3397 | if(!target_path) |
| 3398 | return PyErr_NoMemory(); |
| 3399 | |
| 3400 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3401 | buf_size, VOLUME_NAME_DOS); |
| 3402 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3403 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3404 | |
| 3405 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3406 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3407 | |
| 3408 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3409 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3410 | free(target_path); |
| 3411 | return result; |
| 3412 | |
| 3413 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3414 | |
| 3415 | static PyObject * |
| 3416 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3417 | { |
| 3418 | HANDLE hFile; |
| 3419 | BY_HANDLE_FILE_INFORMATION info; |
| 3420 | int fd; |
| 3421 | |
| 3422 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3423 | return NULL; |
| 3424 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3425 | if (!_PyVerify_fd(fd)) |
| 3426 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3427 | |
| 3428 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3429 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3430 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3431 | |
| 3432 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3433 | return win32_error("_getfileinformation", NULL); |
| 3434 | |
| 3435 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3436 | info.nFileIndexHigh, |
| 3437 | info.nFileIndexLow); |
| 3438 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3439 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3440 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3441 | "Return true if the pathname refers to an existing directory."); |
| 3442 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3443 | static PyObject * |
| 3444 | posix__isdir(PyObject *self, PyObject *args) |
| 3445 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3446 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3447 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3448 | DWORD attributes; |
| 3449 | |
| 3450 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3451 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3452 | if (wpath == NULL) |
| 3453 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3454 | |
| 3455 | attributes = GetFileAttributesW(wpath); |
| 3456 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3457 | Py_RETURN_FALSE; |
| 3458 | goto check; |
| 3459 | } |
| 3460 | /* Drop the argument parsing error as narrow strings |
| 3461 | are also valid. */ |
| 3462 | PyErr_Clear(); |
| 3463 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3464 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3465 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3466 | if (win32_warn_bytes_api()) |
| 3467 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3468 | attributes = GetFileAttributesA(path); |
| 3469 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3470 | Py_RETURN_FALSE; |
| 3471 | |
| 3472 | check: |
| 3473 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3474 | Py_RETURN_TRUE; |
| 3475 | else |
| 3476 | Py_RETURN_FALSE; |
| 3477 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3478 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3479 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3480 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3481 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3482 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3483 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3484 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3485 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3486 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3487 | int res; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3488 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3489 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3490 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3491 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3492 | PyObject *po; |
| 3493 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) |
| 3494 | { |
| 3495 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3496 | if (wpath == NULL) |
| 3497 | return NULL; |
| 3498 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3499 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3500 | res = CreateDirectoryW(wpath, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3501 | Py_END_ALLOW_THREADS |
| 3502 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3503 | return win32_error_object("mkdir", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3504 | Py_INCREF(Py_None); |
| 3505 | return Py_None; |
| 3506 | } |
| 3507 | /* Drop the argument parsing error as narrow strings |
| 3508 | are also valid. */ |
| 3509 | PyErr_Clear(); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3510 | if (!PyArg_ParseTuple(args, "y|i:mkdir", &path, &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3511 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3512 | if (win32_warn_bytes_api()) |
| 3513 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3514 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3515 | res = CreateDirectoryA(path, NULL); |
| 3516 | Py_END_ALLOW_THREADS |
| 3517 | if (!res) { |
| 3518 | win32_error("mkdir", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3519 | return NULL; |
| 3520 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3521 | Py_INCREF(Py_None); |
| 3522 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3523 | #else |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3524 | PyObject *opath; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3525 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3526 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3527 | PyUnicode_FSConverter, &opath, &mode)) |
| 3528 | return NULL; |
| 3529 | path = PyBytes_AsString(opath); |
| 3530 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3531 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3532 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3533 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3534 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3535 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3536 | Py_END_ALLOW_THREADS |
| 3537 | if (res < 0) |
| 3538 | return posix_error_with_allocated_filename(opath); |
| 3539 | Py_DECREF(opath); |
| 3540 | Py_INCREF(Py_None); |
| 3541 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3542 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3545 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3546 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3547 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3548 | #include <sys/resource.h> |
| 3549 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3550 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3551 | |
| 3552 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3553 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3554 | "nice(inc) -> new_priority\n\n\ |
| 3555 | 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] | 3556 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3557 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3558 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3559 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3560 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3561 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3562 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3563 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3564 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3565 | /* There are two flavours of 'nice': one that returns the new |
| 3566 | priority (as required by almost all standards out there) and the |
| 3567 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3568 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3569 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3570 | If we are of the nice family that returns the new priority, we |
| 3571 | need to clear errno before the call, and check if errno is filled |
| 3572 | before calling posix_error() on a returnvalue of -1, because the |
| 3573 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3574 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3575 | errno = 0; |
| 3576 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3577 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3578 | if (value == 0) |
| 3579 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3580 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3581 | if (value == -1 && errno != 0) |
| 3582 | /* either nice() or getpriority() returned an error */ |
| 3583 | return posix_error(); |
| 3584 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3585 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3586 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3587 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3588 | |
| 3589 | #ifdef HAVE_GETPRIORITY |
| 3590 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3591 | "getpriority(which, who) -> current_priority\n\n\ |
| 3592 | Get program scheduling priority."); |
| 3593 | |
| 3594 | static PyObject * |
| 3595 | posix_getpriority(PyObject *self, PyObject *args) |
| 3596 | { |
| 3597 | int which, who, retval; |
| 3598 | |
| 3599 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3600 | return NULL; |
| 3601 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3602 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3603 | if (errno != 0) |
| 3604 | return posix_error(); |
| 3605 | return PyLong_FromLong((long)retval); |
| 3606 | } |
| 3607 | #endif /* HAVE_GETPRIORITY */ |
| 3608 | |
| 3609 | |
| 3610 | #ifdef HAVE_SETPRIORITY |
| 3611 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3612 | "setpriority(which, who, prio) -> None\n\n\ |
| 3613 | Set program scheduling priority."); |
| 3614 | |
| 3615 | static PyObject * |
| 3616 | posix_setpriority(PyObject *self, PyObject *args) |
| 3617 | { |
| 3618 | int which, who, prio, retval; |
| 3619 | |
| 3620 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3621 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3622 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3623 | if (retval == -1) |
| 3624 | return posix_error(); |
| 3625 | Py_RETURN_NONE; |
| 3626 | } |
| 3627 | #endif /* HAVE_SETPRIORITY */ |
| 3628 | |
| 3629 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3630 | static PyObject * |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3631 | internal_rename(PyObject *self, PyObject *args, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3632 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3633 | #ifdef MS_WINDOWS |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3634 | PyObject *src, *dst; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3635 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3636 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
| 3637 | if (PyArg_ParseTuple(args, |
| 3638 | is_replace ? "UU:replace" : "UU:rename", |
| 3639 | &src, &dst)) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3640 | { |
| 3641 | wchar_t *wsrc, *wdst; |
| 3642 | |
| 3643 | wsrc = PyUnicode_AsUnicode(src); |
| 3644 | if (wsrc == NULL) |
| 3645 | return NULL; |
| 3646 | wdst = PyUnicode_AsUnicode(dst); |
| 3647 | if (wdst == NULL) |
| 3648 | return NULL; |
| 3649 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3650 | result = MoveFileExW(wsrc, wdst, flags); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3651 | Py_END_ALLOW_THREADS |
| 3652 | if (!result) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3653 | return win32_error(is_replace ? "replace" : "rename", NULL); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3654 | Py_INCREF(Py_None); |
| 3655 | return Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3656 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3657 | else { |
| 3658 | PyErr_Clear(); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3659 | if (!PyArg_ParseTuple(args, |
| 3660 | is_replace ? "O&O&:replace" : "O&O&:rename", |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3661 | PyUnicode_FSConverter, &src, |
| 3662 | PyUnicode_FSConverter, &dst)) |
| 3663 | return NULL; |
| 3664 | |
| 3665 | if (win32_warn_bytes_api()) |
| 3666 | goto error; |
| 3667 | |
| 3668 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3669 | result = MoveFileExA(PyBytes_AS_STRING(src), |
| 3670 | PyBytes_AS_STRING(dst), flags); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3671 | Py_END_ALLOW_THREADS |
| 3672 | |
| 3673 | Py_XDECREF(src); |
| 3674 | Py_XDECREF(dst); |
| 3675 | |
| 3676 | if (!result) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3677 | return win32_error(is_replace ? "replace" : "rename", NULL); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3678 | Py_INCREF(Py_None); |
| 3679 | return Py_None; |
| 3680 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3681 | error: |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3682 | Py_XDECREF(src); |
| 3683 | Py_XDECREF(dst); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3684 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3685 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3686 | #else |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3687 | return posix_2str(args, |
| 3688 | is_replace ? "O&O&:replace" : "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3689 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3692 | PyDoc_STRVAR(posix_rename__doc__, |
| 3693 | "rename(old, new)\n\n\ |
| 3694 | Rename a file or directory."); |
| 3695 | |
| 3696 | static PyObject * |
| 3697 | posix_rename(PyObject *self, PyObject *args) |
| 3698 | { |
| 3699 | return internal_rename(self, args, 0); |
| 3700 | } |
| 3701 | |
| 3702 | PyDoc_STRVAR(posix_replace__doc__, |
| 3703 | "replace(old, new)\n\n\ |
| 3704 | Rename a file or directory, overwriting the destination."); |
| 3705 | |
| 3706 | static PyObject * |
| 3707 | posix_replace(PyObject *self, PyObject *args) |
| 3708 | { |
| 3709 | return internal_rename(self, args, 1); |
| 3710 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3712 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3713 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3714 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3715 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3716 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3717 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3718 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3719 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3720 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3721 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3722 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3723 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3726 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3727 | PyDoc_STRVAR(posix_stat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 3728 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3729 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3730 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3731 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 3732 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3733 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3734 | #ifdef MS_WINDOWS |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 3735 | return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3736 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 3737 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3738 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3741 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3742 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3743 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3744 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3745 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3746 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3747 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3748 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3749 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3750 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3751 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3752 | wchar_t *command; |
| 3753 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3754 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3755 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3756 | Py_BEGIN_ALLOW_THREADS |
| 3757 | sts = _wsystem(command); |
| 3758 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3759 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3760 | PyObject *command_obj; |
| 3761 | char *command; |
| 3762 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3763 | PyUnicode_FSConverter, &command_obj)) |
| 3764 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3765 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3766 | command = PyBytes_AsString(command_obj); |
| 3767 | Py_BEGIN_ALLOW_THREADS |
| 3768 | sts = system(command); |
| 3769 | Py_END_ALLOW_THREADS |
| 3770 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3771 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3772 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3773 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3774 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3775 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3776 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3777 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3778 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3779 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3780 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3781 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3782 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3783 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3784 | int i; |
| 3785 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3786 | return NULL; |
| 3787 | i = (int)umask(i); |
| 3788 | if (i < 0) |
| 3789 | return posix_error(); |
| 3790 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3793 | #ifdef MS_WINDOWS |
| 3794 | |
| 3795 | /* override the default DeleteFileW behavior so that directory |
| 3796 | symlinks can be removed with this function, the same as with |
| 3797 | Unix symlinks */ |
| 3798 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3799 | { |
| 3800 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3801 | WIN32_FIND_DATAW find_data; |
| 3802 | HANDLE find_data_handle; |
| 3803 | int is_directory = 0; |
| 3804 | int is_link = 0; |
| 3805 | |
| 3806 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3807 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3808 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3809 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3810 | it is a symlink */ |
| 3811 | if(is_directory && |
| 3812 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3813 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3814 | |
| 3815 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3816 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3817 | FindClose(find_data_handle); |
| 3818 | } |
| 3819 | } |
| 3820 | } |
| 3821 | |
| 3822 | if (is_directory && is_link) |
| 3823 | return RemoveDirectoryW(lpFileName); |
| 3824 | |
| 3825 | return DeleteFileW(lpFileName); |
| 3826 | } |
| 3827 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3829 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3830 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3831 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3833 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3834 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3835 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3836 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3837 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3838 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3839 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3840 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3841 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3842 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3843 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3844 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3845 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3848 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3849 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3850 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3851 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3852 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3853 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3854 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3855 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3856 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3857 | struct utsname u; |
| 3858 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3859 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3860 | Py_BEGIN_ALLOW_THREADS |
| 3861 | res = uname(&u); |
| 3862 | Py_END_ALLOW_THREADS |
| 3863 | if (res < 0) |
| 3864 | return posix_error(); |
| 3865 | return Py_BuildValue("(sssss)", |
| 3866 | u.sysname, |
| 3867 | u.nodename, |
| 3868 | u.release, |
| 3869 | u.version, |
| 3870 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3871 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3872 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3873 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3874 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3875 | static int |
| 3876 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 3877 | { |
| 3878 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 3879 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3880 | divmod = PyNumber_Divmod(py_long, billion); |
| 3881 | if (!divmod) |
| 3882 | goto exit; |
| 3883 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 3884 | if ((*s == -1) && PyErr_Occurred()) |
| 3885 | goto exit; |
| 3886 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 3887 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3888 | goto exit; |
| 3889 | |
| 3890 | result = 1; |
| 3891 | exit: |
| 3892 | Py_XDECREF(divmod); |
| 3893 | return result; |
| 3894 | } |
| 3895 | |
| 3896 | |
| 3897 | typedef int (*parameter_converter_t)(PyObject *, void *); |
| 3898 | |
| 3899 | typedef struct { |
| 3900 | /* input only */ |
| 3901 | char path_format; |
| 3902 | parameter_converter_t converter; |
| 3903 | char *function_name; |
| 3904 | char *first_argument_name; |
| 3905 | PyObject *args; |
| 3906 | PyObject *kwargs; |
| 3907 | |
| 3908 | /* input/output */ |
| 3909 | PyObject **path; |
| 3910 | |
| 3911 | /* output only */ |
| 3912 | int now; |
| 3913 | time_t atime_s; |
| 3914 | long atime_ns; |
| 3915 | time_t mtime_s; |
| 3916 | long mtime_ns; |
| 3917 | } utime_arguments; |
| 3918 | |
| 3919 | #define DECLARE_UA(ua, fname) \ |
| 3920 | utime_arguments ua; \ |
| 3921 | memset(&ua, 0, sizeof(ua)); \ |
| 3922 | ua.function_name = fname; \ |
| 3923 | ua.args = args; \ |
| 3924 | ua.kwargs = kwargs; \ |
| 3925 | ua.first_argument_name = "path"; \ |
| 3926 | |
| 3927 | /* UA_TO_FILETIME doesn't declare atime and mtime for you */ |
| 3928 | #define UA_TO_FILETIME(ua, atime, mtime) \ |
| 3929 | time_t_to_FILE_TIME(ua.atime_s, ua.atime_ns, &atime); \ |
| 3930 | time_t_to_FILE_TIME(ua.mtime_s, ua.mtime_ns, &mtime) |
| 3931 | |
| 3932 | /* the rest of these macros declare the output variable for you */ |
| 3933 | #define UA_TO_TIMESPEC(ua, ts) \ |
| 3934 | struct timespec ts[2]; \ |
| 3935 | ts[0].tv_sec = ua.atime_s; \ |
| 3936 | ts[0].tv_nsec = ua.atime_ns; \ |
| 3937 | ts[1].tv_sec = ua.mtime_s; \ |
| 3938 | ts[1].tv_nsec = ua.mtime_ns |
| 3939 | |
| 3940 | #define UA_TO_TIMEVAL(ua, tv) \ |
| 3941 | struct timeval tv[2]; \ |
| 3942 | tv[0].tv_sec = ua.atime_s; \ |
| 3943 | tv[0].tv_usec = ua.atime_ns / 1000; \ |
| 3944 | tv[1].tv_sec = ua.mtime_s; \ |
| 3945 | tv[1].tv_usec = ua.mtime_ns / 1000 |
| 3946 | |
| 3947 | #define UA_TO_UTIMBUF(ua, u) \ |
| 3948 | struct utimbuf u; \ |
| 3949 | utimbuf.actime = ua.atime_s; \ |
| 3950 | utimbuf.modtime = ua.mtime_s |
| 3951 | |
| 3952 | #define UA_TO_TIME_T(ua, timet) \ |
| 3953 | time_t timet[2]; \ |
| 3954 | timet[0] = ua.atime_s; \ |
| 3955 | timet[1] = ua.mtime_s |
| 3956 | |
| 3957 | |
| 3958 | /* |
| 3959 | * utime_read_time_arguments() processes arguments for the utime |
| 3960 | * family of functions. |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3961 | */ |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3962 | |
| 3963 | typedef enum { |
| 3964 | utime_success = 0, |
| 3965 | utime_parse_failure = 1, |
| 3966 | utime_times_and_ns_collision = 2, |
| 3967 | utime_times_conversion_failure = 3, |
| 3968 | utime_ns_conversion_failure = 4, |
| 3969 | } utime_status; |
| 3970 | |
| 3971 | static utime_status |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3972 | utime_read_time_arguments(utime_arguments *ua) |
| 3973 | { |
| 3974 | PyObject *times = NULL; |
| 3975 | PyObject *ns = NULL; |
| 3976 | char format[24]; |
| 3977 | char *kwlist[4]; |
| 3978 | char **kw = kwlist; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3979 | utime_status return_value; |
| 3980 | int parse_result; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3981 | |
| 3982 | *kw++ = ua->first_argument_name; |
| 3983 | *kw++ = "times"; |
| 3984 | *kw++ = "ns"; |
| 3985 | *kw = NULL; |
| 3986 | |
| 3987 | sprintf(format, "%c%s|O$O:%s", |
| 3988 | ua->path_format, |
| 3989 | ua->converter ? "&" : "", |
| 3990 | ua->function_name); |
| 3991 | |
| 3992 | if (ua->converter) |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3993 | parse_result = PyArg_ParseTupleAndKeywords(ua->args, ua->kwargs, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3994 | format, kwlist, ua->converter, ua->path, ×, &ns); |
| 3995 | else |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3996 | parse_result = PyArg_ParseTupleAndKeywords(ua->args, ua->kwargs, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3997 | format, kwlist, ua->path, ×, &ns); |
| 3998 | |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3999 | if (!parse_result) |
| 4000 | return utime_parse_failure; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4001 | |
| 4002 | if (times && ns) { |
| 4003 | PyErr_Format(PyExc_RuntimeError, |
| 4004 | "%s: you may specify either 'times'" |
| 4005 | " or 'ns' but not both", |
| 4006 | ua->function_name); |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4007 | return_value = utime_times_and_ns_collision; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4008 | goto fail; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | if (times && (times != Py_None)) { |
| 4012 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
| 4013 | PyErr_Format(PyExc_TypeError, |
Stefan Krah | 6b03f2c | 2012-05-05 22:37:05 +0200 | [diff] [blame] | 4014 | "%s: 'times' must be either" |
Benjamin Peterson | 9bd9d74 | 2012-05-04 01:42:41 -0400 | [diff] [blame] | 4015 | " a tuple of two ints or None", |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4016 | ua->function_name); |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4017 | return_value = utime_times_conversion_failure; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4018 | goto fail; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4019 | } |
| 4020 | ua->now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4021 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
| 4022 | &ua->atime_s, &ua->atime_ns) == -1 || |
| 4023 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4024 | &ua->mtime_s, &ua->mtime_ns) == -1) { |
| 4025 | return_value = utime_times_conversion_failure; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4026 | goto fail; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4027 | } |
| 4028 | return utime_success; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | if (ns) { |
| 4032 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
| 4033 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 9bd9d74 | 2012-05-04 01:42:41 -0400 | [diff] [blame] | 4034 | "%s: 'ns' must be a tuple of two ints", |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4035 | ua->function_name); |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4036 | return_value = utime_ns_conversion_failure; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4037 | goto fail; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4038 | } |
| 4039 | ua->now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4040 | if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0), |
| 4041 | &ua->atime_s, &ua->atime_ns) || |
| 4042 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4043 | &ua->mtime_s, &ua->mtime_ns)) { |
| 4044 | return_value = utime_ns_conversion_failure; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4045 | goto fail; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4046 | } |
| 4047 | return utime_success; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | /* either times=None, or neither times nor ns was specified. use "now". */ |
| 4051 | ua->now = 1; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4052 | return utime_success; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4053 | |
| 4054 | fail: |
| 4055 | if (ua->converter) |
Richard Oudkerk | f072b45 | 2012-05-04 12:01:31 +0100 | [diff] [blame] | 4056 | Py_DECREF(*ua->path); |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4057 | return return_value; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4061 | PyDoc_STRVAR(posix_utime__doc__, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4062 | "utime(path[, times=(atime, mtime), *, ns=(atime_ns, mtime_ns)])\n\ |
| 4063 | Set the access and modified time of the file.\n\ |
| 4064 | If the second argument ('times') is specified,\n\ |
| 4065 | the values should be expressed as float seconds since the epoch.\n\ |
| 4066 | If the keyword argument 'ns' is specified,\n\ |
| 4067 | the values should be expressed as integer nanoseconds since the epoch.\n\ |
| 4068 | If neither the second nor the 'ns' argument is specified,\n\ |
| 4069 | utime uses the current time.\n\ |
| 4070 | Specifying both 'times' and 'ns' is an error."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4071 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4072 | static PyObject * |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4073 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4074 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4075 | #ifdef MS_WINDOWS |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4076 | PyObject *upath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4077 | HANDLE hFile; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4078 | PyObject *result = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4079 | FILETIME atime, mtime; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4080 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4081 | DECLARE_UA(ua, "utime"); |
| 4082 | |
| 4083 | ua.path_format = 'U'; |
| 4084 | ua.path = &upath; |
| 4085 | |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4086 | switch (utime_read_time_arguments(&ua)) { |
| 4087 | default: |
| 4088 | return NULL; |
| 4089 | case utime_success: { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4090 | wchar_t *wpath = PyUnicode_AsUnicode(upath); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4091 | if (wpath == NULL) |
| 4092 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4093 | Py_BEGIN_ALLOW_THREADS |
| 4094 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 4095 | NULL, OPEN_EXISTING, |
| 4096 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 4097 | Py_END_ALLOW_THREADS |
| 4098 | if (hFile == INVALID_HANDLE_VALUE) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4099 | return win32_error_object("utime", upath); |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4100 | break; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4101 | } |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4102 | case utime_parse_failure: { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4103 | const char *apath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4104 | /* Drop the argument parsing error as narrow strings |
| 4105 | are also valid. */ |
| 4106 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 4107 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4108 | ua.path_format = 'y'; |
| 4109 | ua.path = (PyObject **)&apath; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4110 | if (utime_read_time_arguments(&ua) != utime_success) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4111 | return NULL; |
| 4112 | if (win32_warn_bytes_api()) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4113 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4114 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4115 | Py_BEGIN_ALLOW_THREADS |
| 4116 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 4117 | NULL, OPEN_EXISTING, |
| 4118 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 4119 | Py_END_ALLOW_THREADS |
| 4120 | if (hFile == INVALID_HANDLE_VALUE) { |
| 4121 | win32_error("utime", apath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4122 | return NULL; |
| 4123 | } |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4124 | break; |
| 4125 | } |
| 4126 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4129 | if (ua.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4130 | SYSTEMTIME now; |
| 4131 | GetSystemTime(&now); |
| 4132 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 4133 | !SystemTimeToFileTime(&now, &atime)) { |
| 4134 | win32_error("utime", NULL); |
| 4135 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 4136 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4137 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4138 | else { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4139 | UA_TO_FILETIME(ua, atime, mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4140 | } |
| 4141 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4142 | /* Avoid putting the file name into the error here, |
| 4143 | as that may confuse the user into believing that |
| 4144 | something is wrong with the file, when it also |
| 4145 | could be the time stamp that gives a problem. */ |
| 4146 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 4147 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4148 | } |
| 4149 | Py_INCREF(Py_None); |
| 4150 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4151 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4152 | CloseHandle(hFile); |
| 4153 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4154 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4155 | PyObject *opath; |
| 4156 | char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4157 | int res; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4158 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4159 | DECLARE_UA(ua, "utime"); |
| 4160 | |
| 4161 | ua.path_format = 'O'; |
| 4162 | ua.path = &opath; |
| 4163 | ua.converter = PyUnicode_FSConverter; |
| 4164 | |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4165 | if (utime_read_time_arguments(&ua) != utime_success) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4166 | return NULL; |
| 4167 | path = PyBytes_AsString(opath); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4168 | if (ua.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4169 | Py_BEGIN_ALLOW_THREADS |
| 4170 | res = utime(path, NULL); |
| 4171 | Py_END_ALLOW_THREADS |
| 4172 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4173 | else { |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4174 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4175 | #ifdef HAVE_UTIMENSAT |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4176 | UA_TO_TIMESPEC(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4177 | res = utimensat(AT_FDCWD, path, buf, 0); |
| 4178 | #elif defined(HAVE_UTIMES) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4179 | UA_TO_TIMEVAL(ua, buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4180 | res = utimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4181 | #elif defined(HAVE_UTIME_H) |
| 4182 | /* XXX should define struct utimbuf instead, above */ |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4183 | UA_TO_UTIMBUF(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4184 | res = utime(path, &buf); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 4185 | #else |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4186 | UA_TO_TIME_T(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4187 | res = utime(path, buf); |
| 4188 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4189 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4190 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4192 | if (res < 0) { |
| 4193 | return posix_error_with_allocated_filename(opath); |
| 4194 | } |
| 4195 | Py_DECREF(opath); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4196 | Py_RETURN_NONE; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4197 | #undef UTIME_EXTRACT |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4198 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4201 | #ifdef HAVE_FUTIMES |
| 4202 | PyDoc_STRVAR(posix_futimes__doc__, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4203 | "futimes(fd[, times=(atime, mtime), *, ns=(atime_ns, mtime_ns)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4204 | Set the access and modified time of the file specified by the file\n\ |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4205 | descriptor fd. See utime for the semantics of the times and ns parameters."); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4206 | |
| 4207 | static PyObject * |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4208 | posix_futimes(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4209 | { |
| 4210 | int res, fd; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4211 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4212 | DECLARE_UA(ua, "futimes"); |
| 4213 | |
| 4214 | ua.path_format = 'i'; |
| 4215 | ua.path = (PyObject **)&fd; |
| 4216 | ua.first_argument_name = "fd"; |
| 4217 | |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4218 | if (utime_read_time_arguments(&ua) != utime_success) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4219 | return NULL; |
| 4220 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4221 | if (ua.now) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4222 | Py_BEGIN_ALLOW_THREADS |
| 4223 | res = futimes(fd, NULL); |
| 4224 | Py_END_ALLOW_THREADS |
| 4225 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4226 | else { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4227 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4228 | { |
| 4229 | #ifdef HAVE_FUTIMENS |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4230 | UA_TO_TIMESPEC(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4231 | res = futimens(fd, buf); |
| 4232 | #else |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4233 | UA_TO_TIMEVAL(ua, buf); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4234 | res = futimes(fd, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4235 | #endif |
| 4236 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4237 | Py_END_ALLOW_THREADS |
| 4238 | } |
| 4239 | if (res < 0) |
| 4240 | return posix_error(); |
| 4241 | Py_RETURN_NONE; |
| 4242 | } |
| 4243 | #endif |
| 4244 | |
| 4245 | #ifdef HAVE_LUTIMES |
| 4246 | PyDoc_STRVAR(posix_lutimes__doc__, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4247 | "lutimes(path[, times=(atime, mtime), *, ns=(atime_ns, mtime_ns)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4248 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 4249 | |
| 4250 | static PyObject * |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4251 | posix_lutimes(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4252 | { |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 4253 | PyObject *opath; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4254 | const char *path; |
| 4255 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4256 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4257 | DECLARE_UA(ua, "lutimes"); |
| 4258 | |
| 4259 | ua.path_format = 'O'; |
| 4260 | ua.path = &opath; |
| 4261 | ua.converter = PyUnicode_FSConverter; |
| 4262 | |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4263 | if (utime_read_time_arguments(&ua) != utime_success) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4264 | return NULL; |
| 4265 | path = PyBytes_AsString(opath); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4266 | |
| 4267 | if (ua.now) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4268 | /* optional time values not given */ |
| 4269 | Py_BEGIN_ALLOW_THREADS |
| 4270 | res = lutimes(path, NULL); |
| 4271 | Py_END_ALLOW_THREADS |
| 4272 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4273 | else { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4274 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4275 | { |
| 4276 | #ifdef HAVE_UTIMENSAT |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4277 | UA_TO_TIMESPEC(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4278 | res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); |
| 4279 | #else |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4280 | UA_TO_TIMEVAL(ua, buf); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4281 | res = lutimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4282 | #endif |
| 4283 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4284 | Py_END_ALLOW_THREADS |
| 4285 | } |
| 4286 | Py_DECREF(opath); |
| 4287 | if (res < 0) |
| 4288 | return posix_error(); |
| 4289 | Py_RETURN_NONE; |
| 4290 | } |
| 4291 | #endif |
| 4292 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4293 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4294 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4295 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4296 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4297 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4298 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4299 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4300 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4301 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4302 | int sts; |
| 4303 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 4304 | return NULL; |
| 4305 | _exit(sts); |
| 4306 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4307 | } |
| 4308 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4309 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4310 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4311 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4312 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4313 | Py_ssize_t i; |
| 4314 | for (i = 0; i < count; i++) |
| 4315 | PyMem_Free(array[i]); |
| 4316 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4317 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4318 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4319 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4320 | int fsconvert_strdup(PyObject *o, char**out) |
| 4321 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4322 | PyObject *bytes; |
| 4323 | Py_ssize_t size; |
| 4324 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4325 | return 0; |
| 4326 | size = PyBytes_GET_SIZE(bytes); |
| 4327 | *out = PyMem_Malloc(size+1); |
| 4328 | if (!*out) |
| 4329 | return 0; |
| 4330 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4331 | Py_DECREF(bytes); |
| 4332 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4333 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4334 | #endif |
| 4335 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4336 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4337 | static char** |
| 4338 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4339 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4340 | char **envlist; |
| 4341 | Py_ssize_t i, pos, envc; |
| 4342 | PyObject *keys=NULL, *vals=NULL; |
| 4343 | PyObject *key, *val, *key2, *val2; |
| 4344 | char *p, *k, *v; |
| 4345 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4347 | i = PyMapping_Size(env); |
| 4348 | if (i < 0) |
| 4349 | return NULL; |
| 4350 | envlist = PyMem_NEW(char *, i + 1); |
| 4351 | if (envlist == NULL) { |
| 4352 | PyErr_NoMemory(); |
| 4353 | return NULL; |
| 4354 | } |
| 4355 | envc = 0; |
| 4356 | keys = PyMapping_Keys(env); |
| 4357 | vals = PyMapping_Values(env); |
| 4358 | if (!keys || !vals) |
| 4359 | goto error; |
| 4360 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4361 | PyErr_Format(PyExc_TypeError, |
| 4362 | "env.keys() or env.values() is not a list"); |
| 4363 | goto error; |
| 4364 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4365 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4366 | for (pos = 0; pos < i; pos++) { |
| 4367 | key = PyList_GetItem(keys, pos); |
| 4368 | val = PyList_GetItem(vals, pos); |
| 4369 | if (!key || !val) |
| 4370 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4371 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4372 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4373 | goto error; |
| 4374 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4375 | Py_DECREF(key2); |
| 4376 | goto error; |
| 4377 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4378 | |
| 4379 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4380 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 4381 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4382 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4383 | k = PyBytes_AsString(key2); |
| 4384 | v = PyBytes_AsString(val2); |
| 4385 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4387 | p = PyMem_NEW(char, len); |
| 4388 | if (p == NULL) { |
| 4389 | PyErr_NoMemory(); |
| 4390 | Py_DECREF(key2); |
| 4391 | Py_DECREF(val2); |
| 4392 | goto error; |
| 4393 | } |
| 4394 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4395 | envlist[envc++] = p; |
| 4396 | Py_DECREF(key2); |
| 4397 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4398 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4399 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4400 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4401 | } |
| 4402 | Py_DECREF(vals); |
| 4403 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4404 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4405 | envlist[envc] = 0; |
| 4406 | *envc_ptr = envc; |
| 4407 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4408 | |
| 4409 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4410 | Py_XDECREF(keys); |
| 4411 | Py_XDECREF(vals); |
| 4412 | while (--envc >= 0) |
| 4413 | PyMem_DEL(envlist[envc]); |
| 4414 | PyMem_DEL(envlist); |
| 4415 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4416 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4417 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4418 | static char** |
| 4419 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4420 | { |
| 4421 | int i; |
| 4422 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4423 | if (argvlist == NULL) { |
| 4424 | PyErr_NoMemory(); |
| 4425 | return NULL; |
| 4426 | } |
| 4427 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4428 | PyObject* item = PySequence_ITEM(argv, i); |
| 4429 | if (item == NULL) |
| 4430 | goto fail; |
| 4431 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4432 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4433 | goto fail; |
| 4434 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4435 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4436 | } |
| 4437 | argvlist[*argc] = NULL; |
| 4438 | return argvlist; |
| 4439 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4440 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4441 | free_string_array(argvlist, *argc); |
| 4442 | return NULL; |
| 4443 | } |
| 4444 | #endif |
| 4445 | |
| 4446 | #ifdef HAVE_EXECV |
| 4447 | PyDoc_STRVAR(posix_execv__doc__, |
| 4448 | "execv(path, args)\n\n\ |
| 4449 | Execute an executable path with arguments, replacing current process.\n\ |
| 4450 | \n\ |
| 4451 | path: path of executable file\n\ |
| 4452 | args: tuple or list of strings"); |
| 4453 | |
| 4454 | static PyObject * |
| 4455 | posix_execv(PyObject *self, PyObject *args) |
| 4456 | { |
| 4457 | PyObject *opath; |
| 4458 | char *path; |
| 4459 | PyObject *argv; |
| 4460 | char **argvlist; |
| 4461 | Py_ssize_t argc; |
| 4462 | |
| 4463 | /* execv has two arguments: (path, argv), where |
| 4464 | argv is a list or tuple of strings. */ |
| 4465 | |
| 4466 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4467 | PyUnicode_FSConverter, |
| 4468 | &opath, &argv)) |
| 4469 | return NULL; |
| 4470 | path = PyBytes_AsString(opath); |
| 4471 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4472 | PyErr_SetString(PyExc_TypeError, |
| 4473 | "execv() arg 2 must be a tuple or list"); |
| 4474 | Py_DECREF(opath); |
| 4475 | return NULL; |
| 4476 | } |
| 4477 | argc = PySequence_Size(argv); |
| 4478 | if (argc < 1) { |
| 4479 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4480 | Py_DECREF(opath); |
| 4481 | return NULL; |
| 4482 | } |
| 4483 | |
| 4484 | argvlist = parse_arglist(argv, &argc); |
| 4485 | if (argvlist == NULL) { |
| 4486 | Py_DECREF(opath); |
| 4487 | return NULL; |
| 4488 | } |
| 4489 | |
| 4490 | execv(path, argvlist); |
| 4491 | |
| 4492 | /* If we get here it's definitely an error */ |
| 4493 | |
| 4494 | free_string_array(argvlist, argc); |
| 4495 | Py_DECREF(opath); |
| 4496 | return posix_error(); |
| 4497 | } |
| 4498 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4499 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4500 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4501 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4502 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4503 | path: path of executable file\n\ |
| 4504 | args: tuple or list of arguments\n\ |
| 4505 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4507 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4508 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4509 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4510 | PyObject *opath; |
| 4511 | char *path; |
| 4512 | PyObject *argv, *env; |
| 4513 | char **argvlist; |
| 4514 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4515 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4516 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4517 | /* execve has three arguments: (path, argv, env), where |
| 4518 | argv is a list or tuple of strings and env is a dictionary |
| 4519 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4520 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4521 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 4522 | PyUnicode_FSConverter, |
| 4523 | &opath, &argv, &env)) |
| 4524 | return NULL; |
| 4525 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4526 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4527 | PyErr_SetString(PyExc_TypeError, |
| 4528 | "execve() arg 2 must be a tuple or list"); |
| 4529 | goto fail_0; |
| 4530 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4531 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4532 | if (!PyMapping_Check(env)) { |
| 4533 | PyErr_SetString(PyExc_TypeError, |
| 4534 | "execve() arg 3 must be a mapping object"); |
| 4535 | goto fail_0; |
| 4536 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4537 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4538 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4539 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4540 | goto fail_0; |
| 4541 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4542 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4543 | envlist = parse_envlist(env, &envc); |
| 4544 | if (envlist == NULL) |
| 4545 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4546 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4547 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4548 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4549 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4550 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4551 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4552 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4553 | while (--envc >= 0) |
| 4554 | PyMem_DEL(envlist[envc]); |
| 4555 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4556 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4557 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4558 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4559 | Py_DECREF(opath); |
| 4560 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4561 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4562 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4563 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4564 | #ifdef HAVE_FEXECVE |
| 4565 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4566 | "fexecve(fd, args, env)\n\n\ |
| 4567 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4568 | environment, replacing the current process.\n\ |
| 4569 | \n\ |
| 4570 | fd: file descriptor of executable\n\ |
| 4571 | args: tuple or list of arguments\n\ |
| 4572 | env: dictionary of strings mapping to strings"); |
| 4573 | |
| 4574 | static PyObject * |
| 4575 | posix_fexecve(PyObject *self, PyObject *args) |
| 4576 | { |
| 4577 | int fd; |
| 4578 | PyObject *argv, *env; |
| 4579 | char **argvlist; |
| 4580 | char **envlist; |
| 4581 | Py_ssize_t argc, envc; |
| 4582 | |
| 4583 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4584 | &fd, &argv, &env)) |
| 4585 | return NULL; |
| 4586 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4587 | PyErr_SetString(PyExc_TypeError, |
| 4588 | "fexecve() arg 2 must be a tuple or list"); |
| 4589 | return NULL; |
| 4590 | } |
| 4591 | argc = PySequence_Size(argv); |
| 4592 | if (!PyMapping_Check(env)) { |
| 4593 | PyErr_SetString(PyExc_TypeError, |
| 4594 | "fexecve() arg 3 must be a mapping object"); |
| 4595 | return NULL; |
| 4596 | } |
| 4597 | |
| 4598 | argvlist = parse_arglist(argv, &argc); |
| 4599 | if (argvlist == NULL) |
| 4600 | return NULL; |
| 4601 | |
| 4602 | envlist = parse_envlist(env, &envc); |
| 4603 | if (envlist == NULL) |
| 4604 | goto fail; |
| 4605 | |
| 4606 | fexecve(fd, argvlist, envlist); |
| 4607 | |
| 4608 | /* If we get here it's definitely an error */ |
| 4609 | |
| 4610 | (void) posix_error(); |
| 4611 | |
| 4612 | while (--envc >= 0) |
| 4613 | PyMem_DEL(envlist[envc]); |
| 4614 | PyMem_DEL(envlist); |
| 4615 | fail: |
| 4616 | free_string_array(argvlist, argc); |
| 4617 | return NULL; |
| 4618 | } |
| 4619 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4620 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4621 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4622 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4623 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4624 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4625 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4626 | mode: mode of process creation\n\ |
| 4627 | path: path of executable file\n\ |
| 4628 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4629 | |
| 4630 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4631 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4632 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4633 | PyObject *opath; |
| 4634 | char *path; |
| 4635 | PyObject *argv; |
| 4636 | char **argvlist; |
| 4637 | int mode, i; |
| 4638 | Py_ssize_t argc; |
| 4639 | Py_intptr_t spawnval; |
| 4640 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4641 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4642 | /* spawnv has three arguments: (mode, path, argv), where |
| 4643 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4644 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4645 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4646 | PyUnicode_FSConverter, |
| 4647 | &opath, &argv)) |
| 4648 | return NULL; |
| 4649 | path = PyBytes_AsString(opath); |
| 4650 | if (PyList_Check(argv)) { |
| 4651 | argc = PyList_Size(argv); |
| 4652 | getitem = PyList_GetItem; |
| 4653 | } |
| 4654 | else if (PyTuple_Check(argv)) { |
| 4655 | argc = PyTuple_Size(argv); |
| 4656 | getitem = PyTuple_GetItem; |
| 4657 | } |
| 4658 | else { |
| 4659 | PyErr_SetString(PyExc_TypeError, |
| 4660 | "spawnv() arg 2 must be a tuple or list"); |
| 4661 | Py_DECREF(opath); |
| 4662 | return NULL; |
| 4663 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4664 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4665 | argvlist = PyMem_NEW(char *, argc+1); |
| 4666 | if (argvlist == NULL) { |
| 4667 | Py_DECREF(opath); |
| 4668 | return PyErr_NoMemory(); |
| 4669 | } |
| 4670 | for (i = 0; i < argc; i++) { |
| 4671 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4672 | &argvlist[i])) { |
| 4673 | free_string_array(argvlist, i); |
| 4674 | PyErr_SetString( |
| 4675 | PyExc_TypeError, |
| 4676 | "spawnv() arg 2 must contain only strings"); |
| 4677 | Py_DECREF(opath); |
| 4678 | return NULL; |
| 4679 | } |
| 4680 | } |
| 4681 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4682 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4683 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4684 | Py_BEGIN_ALLOW_THREADS |
| 4685 | spawnval = spawnv(mode, path, argvlist); |
| 4686 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4687 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4688 | if (mode == _OLD_P_OVERLAY) |
| 4689 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4690 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4691 | Py_BEGIN_ALLOW_THREADS |
| 4692 | spawnval = _spawnv(mode, path, argvlist); |
| 4693 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4694 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4695 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4696 | free_string_array(argvlist, argc); |
| 4697 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4698 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4699 | if (spawnval == -1) |
| 4700 | return posix_error(); |
| 4701 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4702 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4703 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4704 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4705 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4706 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
| 4709 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4710 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4711 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4712 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4713 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4714 | mode: mode of process creation\n\ |
| 4715 | path: path of executable file\n\ |
| 4716 | args: tuple or list of arguments\n\ |
| 4717 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4718 | |
| 4719 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4720 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4721 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4722 | PyObject *opath; |
| 4723 | char *path; |
| 4724 | PyObject *argv, *env; |
| 4725 | char **argvlist; |
| 4726 | char **envlist; |
| 4727 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4728 | int mode; |
| 4729 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4730 | Py_intptr_t spawnval; |
| 4731 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4732 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4733 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4734 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4735 | argv is a list or tuple of strings and env is a dictionary |
| 4736 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4737 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4738 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4739 | PyUnicode_FSConverter, |
| 4740 | &opath, &argv, &env)) |
| 4741 | return NULL; |
| 4742 | path = PyBytes_AsString(opath); |
| 4743 | if (PyList_Check(argv)) { |
| 4744 | argc = PyList_Size(argv); |
| 4745 | getitem = PyList_GetItem; |
| 4746 | } |
| 4747 | else if (PyTuple_Check(argv)) { |
| 4748 | argc = PyTuple_Size(argv); |
| 4749 | getitem = PyTuple_GetItem; |
| 4750 | } |
| 4751 | else { |
| 4752 | PyErr_SetString(PyExc_TypeError, |
| 4753 | "spawnve() arg 2 must be a tuple or list"); |
| 4754 | goto fail_0; |
| 4755 | } |
| 4756 | if (!PyMapping_Check(env)) { |
| 4757 | PyErr_SetString(PyExc_TypeError, |
| 4758 | "spawnve() arg 3 must be a mapping object"); |
| 4759 | goto fail_0; |
| 4760 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4761 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4762 | argvlist = PyMem_NEW(char *, argc+1); |
| 4763 | if (argvlist == NULL) { |
| 4764 | PyErr_NoMemory(); |
| 4765 | goto fail_0; |
| 4766 | } |
| 4767 | for (i = 0; i < argc; i++) { |
| 4768 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4769 | &argvlist[i])) |
| 4770 | { |
| 4771 | lastarg = i; |
| 4772 | goto fail_1; |
| 4773 | } |
| 4774 | } |
| 4775 | lastarg = argc; |
| 4776 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4777 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4778 | envlist = parse_envlist(env, &envc); |
| 4779 | if (envlist == NULL) |
| 4780 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4781 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4782 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4783 | Py_BEGIN_ALLOW_THREADS |
| 4784 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4785 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4786 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4787 | if (mode == _OLD_P_OVERLAY) |
| 4788 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4789 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4790 | Py_BEGIN_ALLOW_THREADS |
| 4791 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4792 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4793 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4794 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4795 | if (spawnval == -1) |
| 4796 | (void) posix_error(); |
| 4797 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4798 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4799 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4800 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4801 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4802 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4803 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4804 | while (--envc >= 0) |
| 4805 | PyMem_DEL(envlist[envc]); |
| 4806 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4807 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4808 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4809 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4810 | Py_DECREF(opath); |
| 4811 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4812 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4813 | |
| 4814 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4815 | #if defined(PYOS_OS2) |
| 4816 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4817 | "spawnvp(mode, file, args)\n\n\ |
| 4818 | Execute the program 'file' in a new process, using the environment\n\ |
| 4819 | search path to find the file.\n\ |
| 4820 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4821 | mode: mode of process creation\n\ |
| 4822 | file: executable file name\n\ |
| 4823 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4824 | |
| 4825 | static PyObject * |
| 4826 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4827 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4828 | PyObject *opath; |
| 4829 | char *path; |
| 4830 | PyObject *argv; |
| 4831 | char **argvlist; |
| 4832 | int mode, i, argc; |
| 4833 | Py_intptr_t spawnval; |
| 4834 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4835 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4836 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4837 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4838 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4839 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4840 | PyUnicode_FSConverter, |
| 4841 | &opath, &argv)) |
| 4842 | return NULL; |
| 4843 | path = PyBytes_AsString(opath); |
| 4844 | if (PyList_Check(argv)) { |
| 4845 | argc = PyList_Size(argv); |
| 4846 | getitem = PyList_GetItem; |
| 4847 | } |
| 4848 | else if (PyTuple_Check(argv)) { |
| 4849 | argc = PyTuple_Size(argv); |
| 4850 | getitem = PyTuple_GetItem; |
| 4851 | } |
| 4852 | else { |
| 4853 | PyErr_SetString(PyExc_TypeError, |
| 4854 | "spawnvp() arg 2 must be a tuple or list"); |
| 4855 | Py_DECREF(opath); |
| 4856 | return NULL; |
| 4857 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4858 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4859 | argvlist = PyMem_NEW(char *, argc+1); |
| 4860 | if (argvlist == NULL) { |
| 4861 | Py_DECREF(opath); |
| 4862 | return PyErr_NoMemory(); |
| 4863 | } |
| 4864 | for (i = 0; i < argc; i++) { |
| 4865 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4866 | &argvlist[i])) { |
| 4867 | free_string_array(argvlist, i); |
| 4868 | PyErr_SetString( |
| 4869 | PyExc_TypeError, |
| 4870 | "spawnvp() arg 2 must contain only strings"); |
| 4871 | Py_DECREF(opath); |
| 4872 | return NULL; |
| 4873 | } |
| 4874 | } |
| 4875 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4876 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4877 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4878 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4879 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4880 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4881 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4882 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4883 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4884 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4885 | free_string_array(argvlist, argc); |
| 4886 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4887 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4888 | if (spawnval == -1) |
| 4889 | return posix_error(); |
| 4890 | else |
| 4891 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | |
| 4895 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4896 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4897 | Execute the program 'file' in a new process, using the environment\n\ |
| 4898 | search path to find the file.\n\ |
| 4899 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4900 | mode: mode of process creation\n\ |
| 4901 | file: executable file name\n\ |
| 4902 | args: tuple or list of arguments\n\ |
| 4903 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4904 | |
| 4905 | static PyObject * |
| 4906 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4907 | { |
Ross Lagerwall | dcfde5a | 2011-11-04 07:09:14 +0200 | [diff] [blame] | 4908 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4909 | char *path; |
| 4910 | PyObject *argv, *env; |
| 4911 | char **argvlist; |
| 4912 | char **envlist; |
| 4913 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4914 | int mode; |
| 4915 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4916 | Py_intptr_t spawnval; |
| 4917 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4918 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4919 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4920 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4921 | argv is a list or tuple of strings and env is a dictionary |
| 4922 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4923 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4924 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4925 | PyUnicode_FSConverter, |
| 4926 | &opath, &argv, &env)) |
| 4927 | return NULL; |
| 4928 | path = PyBytes_AsString(opath); |
| 4929 | if (PyList_Check(argv)) { |
| 4930 | argc = PyList_Size(argv); |
| 4931 | getitem = PyList_GetItem; |
| 4932 | } |
| 4933 | else if (PyTuple_Check(argv)) { |
| 4934 | argc = PyTuple_Size(argv); |
| 4935 | getitem = PyTuple_GetItem; |
| 4936 | } |
| 4937 | else { |
| 4938 | PyErr_SetString(PyExc_TypeError, |
| 4939 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4940 | goto fail_0; |
| 4941 | } |
| 4942 | if (!PyMapping_Check(env)) { |
| 4943 | PyErr_SetString(PyExc_TypeError, |
| 4944 | "spawnvpe() arg 3 must be a mapping object"); |
| 4945 | goto fail_0; |
| 4946 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4948 | argvlist = PyMem_NEW(char *, argc+1); |
| 4949 | if (argvlist == NULL) { |
| 4950 | PyErr_NoMemory(); |
| 4951 | goto fail_0; |
| 4952 | } |
| 4953 | for (i = 0; i < argc; i++) { |
| 4954 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4955 | &argvlist[i])) |
| 4956 | { |
| 4957 | lastarg = i; |
| 4958 | goto fail_1; |
| 4959 | } |
| 4960 | } |
| 4961 | lastarg = argc; |
| 4962 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4963 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4964 | envlist = parse_envlist(env, &envc); |
| 4965 | if (envlist == NULL) |
| 4966 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4967 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4968 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4969 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4970 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4971 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4972 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4973 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4974 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4975 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4976 | if (spawnval == -1) |
| 4977 | (void) posix_error(); |
| 4978 | else |
| 4979 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4980 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4981 | while (--envc >= 0) |
| 4982 | PyMem_DEL(envlist[envc]); |
| 4983 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4984 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4985 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4986 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4987 | Py_DECREF(opath); |
| 4988 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4989 | } |
| 4990 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4991 | #endif /* HAVE_SPAWNV */ |
| 4992 | |
| 4993 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4994 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4995 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4996 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4997 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4998 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4999 | 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] | 5000 | |
| 5001 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5002 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5003 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5004 | pid_t pid; |
| 5005 | int result = 0; |
| 5006 | _PyImport_AcquireLock(); |
| 5007 | pid = fork1(); |
| 5008 | if (pid == 0) { |
| 5009 | /* child: this clobbers and resets the import lock. */ |
| 5010 | PyOS_AfterFork(); |
| 5011 | } else { |
| 5012 | /* parent: release the import lock. */ |
| 5013 | result = _PyImport_ReleaseLock(); |
| 5014 | } |
| 5015 | if (pid == -1) |
| 5016 | return posix_error(); |
| 5017 | if (result < 0) { |
| 5018 | /* Don't clobber the OSError if the fork failed. */ |
| 5019 | PyErr_SetString(PyExc_RuntimeError, |
| 5020 | "not holding the import lock"); |
| 5021 | return NULL; |
| 5022 | } |
| 5023 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5024 | } |
| 5025 | #endif |
| 5026 | |
| 5027 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5028 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5029 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5030 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5031 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5032 | 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] | 5033 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5034 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5035 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5036 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5037 | pid_t pid; |
| 5038 | int result = 0; |
| 5039 | _PyImport_AcquireLock(); |
| 5040 | pid = fork(); |
| 5041 | if (pid == 0) { |
| 5042 | /* child: this clobbers and resets the import lock. */ |
| 5043 | PyOS_AfterFork(); |
| 5044 | } else { |
| 5045 | /* parent: release the import lock. */ |
| 5046 | result = _PyImport_ReleaseLock(); |
| 5047 | } |
| 5048 | if (pid == -1) |
| 5049 | return posix_error(); |
| 5050 | if (result < 0) { |
| 5051 | /* Don't clobber the OSError if the fork failed. */ |
| 5052 | PyErr_SetString(PyExc_RuntimeError, |
| 5053 | "not holding the import lock"); |
| 5054 | return NULL; |
| 5055 | } |
| 5056 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5057 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5058 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5059 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5060 | #ifdef HAVE_SCHED_H |
| 5061 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5062 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 5063 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5064 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 5065 | "sched_get_priority_max(policy)\n\n\ |
| 5066 | Get the maximum scheduling priority for *policy*."); |
| 5067 | |
| 5068 | static PyObject * |
| 5069 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 5070 | { |
| 5071 | int policy, max; |
| 5072 | |
| 5073 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 5074 | return NULL; |
| 5075 | max = sched_get_priority_max(policy); |
| 5076 | if (max < 0) |
| 5077 | return posix_error(); |
| 5078 | return PyLong_FromLong(max); |
| 5079 | } |
| 5080 | |
| 5081 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 5082 | "sched_get_priority_min(policy)\n\n\ |
| 5083 | Get the minimum scheduling priority for *policy*."); |
| 5084 | |
| 5085 | static PyObject * |
| 5086 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 5087 | { |
| 5088 | int policy, min; |
| 5089 | |
| 5090 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 5091 | return NULL; |
| 5092 | min = sched_get_priority_min(policy); |
| 5093 | if (min < 0) |
| 5094 | return posix_error(); |
| 5095 | return PyLong_FromLong(min); |
| 5096 | } |
| 5097 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 5098 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 5099 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5100 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5101 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5102 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 5103 | "sched_getscheduler(pid)\n\n\ |
| 5104 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 5105 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 5106 | |
| 5107 | static PyObject * |
| 5108 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 5109 | { |
| 5110 | pid_t pid; |
| 5111 | int policy; |
| 5112 | |
| 5113 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 5114 | return NULL; |
| 5115 | policy = sched_getscheduler(pid); |
| 5116 | if (policy < 0) |
| 5117 | return posix_error(); |
| 5118 | return PyLong_FromLong(policy); |
| 5119 | } |
| 5120 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5121 | #endif |
| 5122 | |
| 5123 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 5124 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5125 | static PyObject * |
| 5126 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5127 | { |
| 5128 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 5129 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5130 | |
| 5131 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 5132 | return NULL; |
| 5133 | res = PyStructSequence_New(type); |
| 5134 | if (!res) |
| 5135 | return NULL; |
| 5136 | Py_INCREF(priority); |
| 5137 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5138 | return res; |
| 5139 | } |
| 5140 | |
| 5141 | PyDoc_STRVAR(sched_param__doc__, |
| 5142 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 5143 | Current has only one field: sched_priority"); |
| 5144 | |
| 5145 | static PyStructSequence_Field sched_param_fields[] = { |
| 5146 | {"sched_priority", "the scheduling priority"}, |
| 5147 | {0} |
| 5148 | }; |
| 5149 | |
| 5150 | static PyStructSequence_Desc sched_param_desc = { |
| 5151 | "sched_param", /* name */ |
| 5152 | sched_param__doc__, /* doc */ |
| 5153 | sched_param_fields, |
| 5154 | 1 |
| 5155 | }; |
| 5156 | |
| 5157 | static int |
| 5158 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 5159 | { |
| 5160 | long priority; |
| 5161 | |
| 5162 | if (Py_TYPE(param) != &SchedParamType) { |
| 5163 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 5164 | return 0; |
| 5165 | } |
| 5166 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 5167 | if (priority == -1 && PyErr_Occurred()) |
| 5168 | return 0; |
| 5169 | if (priority > INT_MAX || priority < INT_MIN) { |
| 5170 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 5171 | return 0; |
| 5172 | } |
| 5173 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 5174 | return 1; |
| 5175 | } |
| 5176 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5177 | #endif |
| 5178 | |
| 5179 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 5180 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5181 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 5182 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 5183 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 5184 | If *pid* is 0, the calling process is changed.\n\ |
| 5185 | *param* is an instance of sched_param."); |
| 5186 | |
| 5187 | static PyObject * |
| 5188 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 5189 | { |
| 5190 | pid_t pid; |
| 5191 | int policy; |
| 5192 | struct sched_param param; |
| 5193 | |
| 5194 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 5195 | &pid, &policy, &convert_sched_param, ¶m)) |
| 5196 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5197 | |
| 5198 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 5199 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 5200 | ** scheduling policy under Solaris/Illumos, and others. |
| 5201 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 5202 | */ |
| 5203 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5204 | return posix_error(); |
| 5205 | Py_RETURN_NONE; |
| 5206 | } |
| 5207 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5208 | #endif |
| 5209 | |
| 5210 | #ifdef HAVE_SCHED_SETPARAM |
| 5211 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5212 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 5213 | "sched_getparam(pid) -> sched_param\n\n\ |
| 5214 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 5215 | sched_param class. A PID of 0 means the calling process."); |
| 5216 | |
| 5217 | static PyObject * |
| 5218 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 5219 | { |
| 5220 | pid_t pid; |
| 5221 | struct sched_param param; |
| 5222 | PyObject *res, *priority; |
| 5223 | |
| 5224 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 5225 | return NULL; |
| 5226 | if (sched_getparam(pid, ¶m)) |
| 5227 | return posix_error(); |
| 5228 | res = PyStructSequence_New(&SchedParamType); |
| 5229 | if (!res) |
| 5230 | return NULL; |
| 5231 | priority = PyLong_FromLong(param.sched_priority); |
| 5232 | if (!priority) { |
| 5233 | Py_DECREF(res); |
| 5234 | return NULL; |
| 5235 | } |
| 5236 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 5237 | return res; |
| 5238 | } |
| 5239 | |
| 5240 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 5241 | "sched_setparam(pid, param)\n\n\ |
| 5242 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 5243 | A PID of 0 means the calling process."); |
| 5244 | |
| 5245 | static PyObject * |
| 5246 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 5247 | { |
| 5248 | pid_t pid; |
| 5249 | struct sched_param param; |
| 5250 | |
| 5251 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 5252 | &pid, &convert_sched_param, ¶m)) |
| 5253 | return NULL; |
| 5254 | if (sched_setparam(pid, ¶m)) |
| 5255 | return posix_error(); |
| 5256 | Py_RETURN_NONE; |
| 5257 | } |
| 5258 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5259 | #endif |
| 5260 | |
| 5261 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 5262 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5263 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 5264 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 5265 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 5266 | |
| 5267 | static PyObject * |
| 5268 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 5269 | { |
| 5270 | pid_t pid; |
| 5271 | struct timespec interval; |
| 5272 | |
| 5273 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 5274 | return NULL; |
| 5275 | if (sched_rr_get_interval(pid, &interval)) |
| 5276 | return posix_error(); |
| 5277 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 5278 | } |
| 5279 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 5280 | #endif |
| 5281 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5282 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 5283 | "sched_yield()\n\n\ |
| 5284 | Voluntarily relinquish the CPU."); |
| 5285 | |
| 5286 | static PyObject * |
| 5287 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 5288 | { |
| 5289 | if (sched_yield()) |
| 5290 | return posix_error(); |
| 5291 | Py_RETURN_NONE; |
| 5292 | } |
| 5293 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5294 | #ifdef HAVE_SCHED_SETAFFINITY |
| 5295 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5296 | typedef struct { |
| 5297 | PyObject_HEAD; |
| 5298 | Py_ssize_t size; |
| 5299 | int ncpus; |
| 5300 | cpu_set_t *set; |
| 5301 | } Py_cpu_set; |
| 5302 | |
| 5303 | static PyTypeObject cpu_set_type; |
| 5304 | |
| 5305 | static void |
| 5306 | cpu_set_dealloc(Py_cpu_set *set) |
| 5307 | { |
| 5308 | assert(set->set); |
| 5309 | CPU_FREE(set->set); |
| 5310 | Py_TYPE(set)->tp_free(set); |
| 5311 | } |
| 5312 | |
| 5313 | static Py_cpu_set * |
| 5314 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 5315 | { |
| 5316 | Py_cpu_set *set; |
| 5317 | |
| 5318 | if (size < 0) { |
| 5319 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 5320 | return NULL; |
| 5321 | } |
| 5322 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 5323 | if (!set) |
| 5324 | return NULL; |
| 5325 | set->ncpus = size; |
| 5326 | set->size = CPU_ALLOC_SIZE(size); |
| 5327 | set->set = CPU_ALLOC(size); |
| 5328 | if (!set->set) { |
| 5329 | type->tp_free(set); |
| 5330 | PyErr_NoMemory(); |
| 5331 | return NULL; |
| 5332 | } |
| 5333 | CPU_ZERO_S(set->size, set->set); |
| 5334 | return set; |
| 5335 | } |
| 5336 | |
| 5337 | static PyObject * |
| 5338 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5339 | { |
| 5340 | int size; |
| 5341 | |
| 5342 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 5343 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 5344 | return NULL; |
| 5345 | return (PyObject *)make_new_cpu_set(type, size); |
| 5346 | } |
| 5347 | |
| 5348 | static PyObject * |
| 5349 | cpu_set_repr(Py_cpu_set *set) |
| 5350 | { |
| 5351 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 5352 | } |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5353 | |
| 5354 | static Py_ssize_t |
| 5355 | cpu_set_len(Py_cpu_set *set) |
| 5356 | { |
| 5357 | return set->ncpus; |
| 5358 | } |
| 5359 | |
| 5360 | static int |
| 5361 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 5362 | { |
| 5363 | int cpu; |
| 5364 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 5365 | return -1; |
| 5366 | if (cpu < 0) { |
| 5367 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 5368 | return -1; |
| 5369 | } |
| 5370 | if (cpu >= set->ncpus) { |
| 5371 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 5372 | return -1; |
| 5373 | } |
| 5374 | return cpu; |
| 5375 | } |
| 5376 | |
| 5377 | PyDoc_STRVAR(cpu_set_set_doc, |
| 5378 | "cpu_set.set(i)\n\n\ |
| 5379 | Add CPU *i* to the set."); |
| 5380 | |
| 5381 | static PyObject * |
| 5382 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 5383 | { |
| 5384 | int cpu = _get_cpu(set, "i|set", args); |
| 5385 | if (cpu == -1) |
| 5386 | return NULL; |
| 5387 | CPU_SET_S(cpu, set->size, set->set); |
| 5388 | Py_RETURN_NONE; |
| 5389 | } |
| 5390 | |
| 5391 | PyDoc_STRVAR(cpu_set_count_doc, |
| 5392 | "cpu_set.count() -> int\n\n\ |
| 5393 | Return the number of CPUs active in the set."); |
| 5394 | |
| 5395 | static PyObject * |
| 5396 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 5397 | { |
| 5398 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 5399 | } |
| 5400 | |
| 5401 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 5402 | "cpu_set.clear(i)\n\n\ |
| 5403 | Remove CPU *i* from the set."); |
| 5404 | |
| 5405 | static PyObject * |
| 5406 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 5407 | { |
| 5408 | int cpu = _get_cpu(set, "i|clear", args); |
| 5409 | if (cpu == -1) |
| 5410 | return NULL; |
| 5411 | CPU_CLR_S(cpu, set->size, set->set); |
| 5412 | Py_RETURN_NONE; |
| 5413 | } |
| 5414 | |
| 5415 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 5416 | "cpu_set.isset(i) -> bool\n\n\ |
| 5417 | Test if CPU *i* is in the set."); |
| 5418 | |
| 5419 | static PyObject * |
| 5420 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 5421 | { |
| 5422 | int cpu = _get_cpu(set, "i|isset", args); |
| 5423 | if (cpu == -1) |
| 5424 | return NULL; |
| 5425 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 5426 | Py_RETURN_TRUE; |
| 5427 | Py_RETURN_FALSE; |
| 5428 | } |
| 5429 | |
| 5430 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 5431 | "cpu_set.zero()\n\n\ |
| 5432 | Clear the cpu_set."); |
| 5433 | |
| 5434 | static PyObject * |
| 5435 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 5436 | { |
| 5437 | CPU_ZERO_S(set->size, set->set); |
| 5438 | Py_RETURN_NONE; |
| 5439 | } |
| 5440 | |
| 5441 | static PyObject * |
| 5442 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 5443 | { |
| 5444 | int eq; |
| 5445 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5446 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) |
| 5447 | Py_RETURN_NOTIMPLEMENTED; |
| 5448 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5449 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 5450 | if ((op == Py_EQ) ? eq : !eq) |
| 5451 | Py_RETURN_TRUE; |
| 5452 | else |
| 5453 | Py_RETURN_FALSE; |
| 5454 | } |
| 5455 | |
| 5456 | #define CPU_SET_BINOP(name, op) \ |
| 5457 | static PyObject * \ |
| 5458 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 5459 | if (res) { \ |
| 5460 | Py_INCREF(res); \ |
| 5461 | } \ |
| 5462 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 5463 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5464 | if (!res) \ |
| 5465 | return NULL; \ |
| 5466 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 5467 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5468 | Py_DECREF(res); \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5469 | Py_RETURN_NOTIMPLEMENTED; \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5470 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 5471 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5472 | op(res->size, res->set, left->set, right->set); \ |
| 5473 | return (PyObject *)res; \ |
| 5474 | } \ |
| 5475 | static PyObject * \ |
| 5476 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5477 | return do_cpu_set_##name(left, right, NULL); \ |
| 5478 | } \ |
| 5479 | static PyObject * \ |
| 5480 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5481 | return do_cpu_set_##name(left, right, left); \ |
| 5482 | } \ |
| 5483 | |
| 5484 | CPU_SET_BINOP(and, CPU_AND_S) |
| 5485 | CPU_SET_BINOP(or, CPU_OR_S) |
| 5486 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 5487 | #undef CPU_SET_BINOP |
| 5488 | |
| 5489 | PyDoc_STRVAR(cpu_set_doc, |
| 5490 | "cpu_set(size)\n\n\ |
| 5491 | Create an empty mask of CPUs."); |
| 5492 | |
| 5493 | static PyNumberMethods cpu_set_as_number = { |
| 5494 | 0, /*nb_add*/ |
| 5495 | 0, /*nb_subtract*/ |
| 5496 | 0, /*nb_multiply*/ |
| 5497 | 0, /*nb_remainder*/ |
| 5498 | 0, /*nb_divmod*/ |
| 5499 | 0, /*nb_power*/ |
| 5500 | 0, /*nb_negative*/ |
| 5501 | 0, /*nb_positive*/ |
| 5502 | 0, /*nb_absolute*/ |
| 5503 | 0, /*nb_bool*/ |
| 5504 | 0, /*nb_invert*/ |
| 5505 | 0, /*nb_lshift*/ |
| 5506 | 0, /*nb_rshift*/ |
| 5507 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 5508 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 5509 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 5510 | 0, /*nb_int*/ |
| 5511 | 0, /*nb_reserved*/ |
| 5512 | 0, /*nb_float*/ |
| 5513 | 0, /*nb_inplace_add*/ |
| 5514 | 0, /*nb_inplace_subtract*/ |
| 5515 | 0, /*nb_inplace_multiply*/ |
| 5516 | 0, /*nb_inplace_remainder*/ |
| 5517 | 0, /*nb_inplace_power*/ |
| 5518 | 0, /*nb_inplace_lshift*/ |
| 5519 | 0, /*nb_inplace_rshift*/ |
| 5520 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5521 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5522 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5523 | }; |
| 5524 | |
| 5525 | static PySequenceMethods cpu_set_as_sequence = { |
| 5526 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5527 | }; |
| 5528 | |
| 5529 | static PyMethodDef cpu_set_methods[] = { |
| 5530 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5531 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5532 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5533 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5534 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5535 | {NULL, NULL} /* sentinel */ |
| 5536 | }; |
| 5537 | |
| 5538 | static PyTypeObject cpu_set_type = { |
| 5539 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5540 | "posix.cpu_set", /* tp_name */ |
| 5541 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5542 | 0, /* tp_itemsize */ |
| 5543 | /* methods */ |
| 5544 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5545 | 0, /* tp_print */ |
| 5546 | 0, /* tp_getattr */ |
| 5547 | 0, /* tp_setattr */ |
| 5548 | 0, /* tp_reserved */ |
| 5549 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5550 | &cpu_set_as_number, /* tp_as_number */ |
| 5551 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5552 | 0, /* tp_as_mapping */ |
| 5553 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5554 | 0, /* tp_call */ |
| 5555 | 0, /* tp_str */ |
| 5556 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5557 | 0, /* tp_setattro */ |
| 5558 | 0, /* tp_as_buffer */ |
| 5559 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5560 | cpu_set_doc, /* tp_doc */ |
| 5561 | 0, /* tp_traverse */ |
| 5562 | 0, /* tp_clear */ |
| 5563 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5564 | 0, /* tp_weaklistoffset */ |
| 5565 | 0, /* tp_iter */ |
| 5566 | 0, /* tp_iternext */ |
| 5567 | cpu_set_methods, /* tp_methods */ |
| 5568 | 0, /* tp_members */ |
| 5569 | 0, /* tp_getset */ |
| 5570 | 0, /* tp_base */ |
| 5571 | 0, /* tp_dict */ |
| 5572 | 0, /* tp_descr_get */ |
| 5573 | 0, /* tp_descr_set */ |
| 5574 | 0, /* tp_dictoffset */ |
| 5575 | 0, /* tp_init */ |
| 5576 | PyType_GenericAlloc, /* tp_alloc */ |
| 5577 | cpu_set_new, /* tp_new */ |
| 5578 | PyObject_Del, /* tp_free */ |
| 5579 | }; |
| 5580 | |
| 5581 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5582 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5583 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5584 | |
| 5585 | static PyObject * |
| 5586 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5587 | { |
| 5588 | pid_t pid; |
| 5589 | Py_cpu_set *cpu_set; |
| 5590 | |
Benjamin Peterson | a17a5d6 | 2011-08-09 16:49:13 -0500 | [diff] [blame] | 5591 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5592 | &pid, &cpu_set_type, &cpu_set)) |
| 5593 | return NULL; |
| 5594 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5595 | return posix_error(); |
| 5596 | Py_RETURN_NONE; |
| 5597 | } |
| 5598 | |
| 5599 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5600 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5601 | Return the affinity of the process with PID *pid*.\n\ |
| 5602 | The returned cpu_set will be of size *ncpus*."); |
| 5603 | |
| 5604 | static PyObject * |
| 5605 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5606 | { |
| 5607 | pid_t pid; |
| 5608 | int ncpus; |
| 5609 | Py_cpu_set *res; |
| 5610 | |
Benjamin Peterson | 7ac9214 | 2011-08-03 08:54:26 -0500 | [diff] [blame] | 5611 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5612 | &pid, &ncpus)) |
| 5613 | return NULL; |
| 5614 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5615 | if (!res) |
| 5616 | return NULL; |
| 5617 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5618 | Py_DECREF(res); |
| 5619 | return posix_error(); |
| 5620 | } |
| 5621 | return (PyObject *)res; |
| 5622 | } |
| 5623 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5624 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5625 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5626 | #endif /* HAVE_SCHED_H */ |
| 5627 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5628 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5629 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5630 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5631 | #define DEV_PTY_FILE "/dev/ptc" |
| 5632 | #define HAVE_DEV_PTMX |
| 5633 | #else |
| 5634 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5635 | #endif |
| 5636 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5637 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5638 | #ifdef HAVE_PTY_H |
| 5639 | #include <pty.h> |
| 5640 | #else |
| 5641 | #ifdef HAVE_LIBUTIL_H |
| 5642 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5643 | #else |
| 5644 | #ifdef HAVE_UTIL_H |
| 5645 | #include <util.h> |
| 5646 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5647 | #endif /* HAVE_LIBUTIL_H */ |
| 5648 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5649 | #ifdef HAVE_STROPTS_H |
| 5650 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5651 | #endif |
| 5652 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5653 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5654 | #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] | 5655 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5656 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5657 | 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] | 5658 | |
| 5659 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5660 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5661 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5662 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5663 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5664 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5665 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5666 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5667 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5668 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5669 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5670 | #endif |
| 5671 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5672 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5673 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5674 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5675 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5676 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5677 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5678 | if (slave_name == NULL) |
| 5679 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5680 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5681 | slave_fd = open(slave_name, O_RDWR); |
| 5682 | if (slave_fd < 0) |
| 5683 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5684 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5685 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5686 | if (master_fd < 0) |
| 5687 | return posix_error(); |
| 5688 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5689 | /* change permission of slave */ |
| 5690 | if (grantpt(master_fd) < 0) { |
| 5691 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5692 | return posix_error(); |
| 5693 | } |
| 5694 | /* unlock slave */ |
| 5695 | if (unlockpt(master_fd) < 0) { |
| 5696 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5697 | return posix_error(); |
| 5698 | } |
| 5699 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5700 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5701 | if (slave_name == NULL) |
| 5702 | return posix_error(); |
| 5703 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5704 | if (slave_fd < 0) |
| 5705 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5706 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5707 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5708 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5709 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5710 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5711 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5712 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5713 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5714 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5715 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5716 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5717 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5718 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5719 | |
| 5720 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5721 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5722 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5723 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5724 | 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] | 5725 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5726 | |
| 5727 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5728 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5729 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5730 | int master_fd = -1, result = 0; |
| 5731 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5732 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5733 | _PyImport_AcquireLock(); |
| 5734 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5735 | if (pid == 0) { |
| 5736 | /* child: this clobbers and resets the import lock. */ |
| 5737 | PyOS_AfterFork(); |
| 5738 | } else { |
| 5739 | /* parent: release the import lock. */ |
| 5740 | result = _PyImport_ReleaseLock(); |
| 5741 | } |
| 5742 | if (pid == -1) |
| 5743 | return posix_error(); |
| 5744 | if (result < 0) { |
| 5745 | /* Don't clobber the OSError if the fork failed. */ |
| 5746 | PyErr_SetString(PyExc_RuntimeError, |
| 5747 | "not holding the import lock"); |
| 5748 | return NULL; |
| 5749 | } |
| 5750 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5751 | } |
| 5752 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5753 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5754 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5755 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5756 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5757 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5758 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5759 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5760 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5761 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5762 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5763 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5764 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5765 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5766 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5767 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5768 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5769 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5770 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5771 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5772 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5773 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5774 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5775 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5776 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5777 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5778 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5779 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5780 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5781 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5782 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5783 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5784 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5785 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5786 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5787 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5788 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5789 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5790 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5791 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5792 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5794 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5795 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5796 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5797 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5798 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5799 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5800 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5801 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5804 | #ifdef HAVE_GETGROUPLIST |
| 5805 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5806 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5807 | Returns a list of groups to which a user belongs.\n\n\ |
| 5808 | user: username to lookup\n\ |
| 5809 | group: base group id of the user"); |
| 5810 | |
| 5811 | static PyObject * |
| 5812 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5813 | { |
| 5814 | #ifdef NGROUPS_MAX |
| 5815 | #define MAX_GROUPS NGROUPS_MAX |
| 5816 | #else |
| 5817 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5818 | #define MAX_GROUPS 64 |
| 5819 | #endif |
| 5820 | |
| 5821 | const char *user; |
| 5822 | int i, ngroups; |
| 5823 | PyObject *list; |
| 5824 | #ifdef __APPLE__ |
| 5825 | int *groups, basegid; |
| 5826 | #else |
| 5827 | gid_t *groups, basegid; |
| 5828 | #endif |
| 5829 | ngroups = MAX_GROUPS; |
| 5830 | |
| 5831 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5832 | return NULL; |
| 5833 | |
| 5834 | #ifdef __APPLE__ |
| 5835 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5836 | #else |
| 5837 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5838 | #endif |
| 5839 | if (groups == NULL) |
| 5840 | return PyErr_NoMemory(); |
| 5841 | |
| 5842 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5843 | PyMem_Del(groups); |
| 5844 | return posix_error(); |
| 5845 | } |
| 5846 | |
| 5847 | list = PyList_New(ngroups); |
| 5848 | if (list == NULL) { |
| 5849 | PyMem_Del(groups); |
| 5850 | return NULL; |
| 5851 | } |
| 5852 | |
| 5853 | for (i = 0; i < ngroups; i++) { |
| 5854 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5855 | if (o == NULL) { |
| 5856 | Py_DECREF(list); |
| 5857 | PyMem_Del(groups); |
| 5858 | return NULL; |
| 5859 | } |
| 5860 | PyList_SET_ITEM(list, i, o); |
| 5861 | } |
| 5862 | |
| 5863 | PyMem_Del(groups); |
| 5864 | |
| 5865 | return list; |
| 5866 | } |
| 5867 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5868 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5869 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5870 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5871 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5872 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5873 | |
| 5874 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5875 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5876 | { |
| 5877 | PyObject *result = NULL; |
| 5878 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5879 | #ifdef NGROUPS_MAX |
| 5880 | #define MAX_GROUPS NGROUPS_MAX |
| 5881 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5882 | /* 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] | 5883 | #define MAX_GROUPS 64 |
| 5884 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5885 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5886 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5887 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5888 | * This is a helper variable to store the intermediate result when |
| 5889 | * that happens. |
| 5890 | * |
| 5891 | * To keep the code readable the OSX behaviour is unconditional, |
| 5892 | * according to the POSIX spec this should be safe on all unix-y |
| 5893 | * systems. |
| 5894 | */ |
| 5895 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5896 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5897 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5898 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5899 | if (n < 0) { |
| 5900 | if (errno == EINVAL) { |
| 5901 | n = getgroups(0, NULL); |
| 5902 | if (n == -1) { |
| 5903 | return posix_error(); |
| 5904 | } |
| 5905 | if (n == 0) { |
| 5906 | /* Avoid malloc(0) */ |
| 5907 | alt_grouplist = grouplist; |
| 5908 | } else { |
| 5909 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5910 | if (alt_grouplist == NULL) { |
| 5911 | errno = EINVAL; |
| 5912 | return posix_error(); |
| 5913 | } |
| 5914 | n = getgroups(n, alt_grouplist); |
| 5915 | if (n == -1) { |
| 5916 | PyMem_Free(alt_grouplist); |
| 5917 | return posix_error(); |
| 5918 | } |
| 5919 | } |
| 5920 | } else { |
| 5921 | return posix_error(); |
| 5922 | } |
| 5923 | } |
| 5924 | result = PyList_New(n); |
| 5925 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5926 | int i; |
| 5927 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5928 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5929 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5930 | Py_DECREF(result); |
| 5931 | result = NULL; |
| 5932 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5933 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5934 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5935 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5936 | } |
| 5937 | |
| 5938 | if (alt_grouplist != grouplist) { |
| 5939 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5940 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5941 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5942 | return result; |
| 5943 | } |
| 5944 | #endif |
| 5945 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5946 | #ifdef HAVE_INITGROUPS |
| 5947 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5948 | "initgroups(username, gid) -> None\n\n\ |
| 5949 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5950 | the groups of which the specified username is a member, plus the specified\n\ |
| 5951 | group id."); |
| 5952 | |
| 5953 | static PyObject * |
| 5954 | posix_initgroups(PyObject *self, PyObject *args) |
| 5955 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5956 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5957 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5958 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5959 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5960 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5961 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5962 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5963 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5964 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5965 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5966 | res = initgroups(username, (gid_t) gid); |
| 5967 | Py_DECREF(oname); |
| 5968 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5969 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5970 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5971 | Py_INCREF(Py_None); |
| 5972 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5973 | } |
| 5974 | #endif |
| 5975 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5976 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5977 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5978 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5979 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5980 | |
| 5981 | static PyObject * |
| 5982 | posix_getpgid(PyObject *self, PyObject *args) |
| 5983 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5984 | pid_t pid, pgid; |
| 5985 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5986 | return NULL; |
| 5987 | pgid = getpgid(pid); |
| 5988 | if (pgid < 0) |
| 5989 | return posix_error(); |
| 5990 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5991 | } |
| 5992 | #endif /* HAVE_GETPGID */ |
| 5993 | |
| 5994 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5995 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5996 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5997 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5998 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5999 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6000 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6001 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6002 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6003 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6004 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6005 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6006 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6007 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6008 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6009 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 6010 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6011 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6012 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6013 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6014 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 6015 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6016 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6017 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6018 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6019 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6020 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6021 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6022 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6023 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 6024 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6025 | return posix_error(); |
| 6026 | Py_INCREF(Py_None); |
| 6027 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6028 | } |
| 6029 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6030 | #endif /* HAVE_SETPGRP */ |
| 6031 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6032 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6033 | |
| 6034 | #ifdef MS_WINDOWS |
| 6035 | #include <tlhelp32.h> |
| 6036 | |
| 6037 | static PyObject* |
| 6038 | win32_getppid() |
| 6039 | { |
| 6040 | HANDLE snapshot; |
| 6041 | pid_t mypid; |
| 6042 | PyObject* result = NULL; |
| 6043 | BOOL have_record; |
| 6044 | PROCESSENTRY32 pe; |
| 6045 | |
| 6046 | mypid = getpid(); /* This function never fails */ |
| 6047 | |
| 6048 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 6049 | if (snapshot == INVALID_HANDLE_VALUE) |
| 6050 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 6051 | |
| 6052 | pe.dwSize = sizeof(pe); |
| 6053 | have_record = Process32First(snapshot, &pe); |
| 6054 | while (have_record) { |
| 6055 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 6056 | /* We could cache the ulong value in a static variable. */ |
| 6057 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 6058 | break; |
| 6059 | } |
| 6060 | |
| 6061 | have_record = Process32Next(snapshot, &pe); |
| 6062 | } |
| 6063 | |
| 6064 | /* If our loop exits and our pid was not found (result will be NULL) |
| 6065 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 6066 | * error anyway, so let's raise it. */ |
| 6067 | if (!result) |
| 6068 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6069 | |
| 6070 | CloseHandle(snapshot); |
| 6071 | |
| 6072 | return result; |
| 6073 | } |
| 6074 | #endif /*MS_WINDOWS*/ |
| 6075 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6076 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6077 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6078 | Return the parent's process id. If the parent process has already exited,\n\ |
| 6079 | Windows machines will still return its id; others systems will return the id\n\ |
| 6080 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6081 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6082 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6083 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6084 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6085 | #ifdef MS_WINDOWS |
| 6086 | return win32_getppid(); |
| 6087 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6088 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6089 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 6090 | } |
| 6091 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6092 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6093 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6094 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6095 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6096 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6097 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6098 | |
| 6099 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6100 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6101 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6102 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6103 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6104 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6105 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6106 | |
| 6107 | if (GetUserNameW(user_name, &num_chars)) { |
| 6108 | /* num_chars is the number of unicode chars plus null terminator */ |
| 6109 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6110 | } |
| 6111 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6112 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 6113 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6114 | char *name; |
| 6115 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6116 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6117 | errno = 0; |
| 6118 | name = getlogin(); |
| 6119 | if (name == NULL) { |
| 6120 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6121 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6122 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6123 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6124 | } |
| 6125 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 6126 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6127 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6128 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6129 | return result; |
| 6130 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 6131 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6132 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6133 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6134 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6135 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6136 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6137 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6138 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6139 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6140 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6141 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6142 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6143 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6144 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6145 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6146 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6147 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6148 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6149 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6150 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6151 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6152 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6153 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6154 | pid_t pid; |
| 6155 | int sig; |
| 6156 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 6157 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6158 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6159 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 6160 | APIRET rc; |
| 6161 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6162 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6163 | |
| 6164 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 6165 | APIRET rc; |
| 6166 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6167 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6168 | |
| 6169 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 6170 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6171 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6172 | if (kill(pid, sig) == -1) |
| 6173 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6174 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6175 | Py_INCREF(Py_None); |
| 6176 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6177 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6178 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6179 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6180 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6181 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6182 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6183 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6184 | |
| 6185 | static PyObject * |
| 6186 | posix_killpg(PyObject *self, PyObject *args) |
| 6187 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6188 | int sig; |
| 6189 | pid_t pgid; |
| 6190 | /* XXX some man pages make the `pgid` parameter an int, others |
| 6191 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 6192 | take the same type. Moreover, pid_t is always at least as wide as |
| 6193 | int (else compilation of this module fails), which is safe. */ |
| 6194 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 6195 | return NULL; |
| 6196 | if (killpg(pgid, sig) == -1) |
| 6197 | return posix_error(); |
| 6198 | Py_INCREF(Py_None); |
| 6199 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6200 | } |
| 6201 | #endif |
| 6202 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6203 | #ifdef MS_WINDOWS |
| 6204 | PyDoc_STRVAR(win32_kill__doc__, |
| 6205 | "kill(pid, sig)\n\n\ |
| 6206 | Kill a process with a signal."); |
| 6207 | |
| 6208 | static PyObject * |
| 6209 | win32_kill(PyObject *self, PyObject *args) |
| 6210 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 6211 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6212 | DWORD pid, sig, err; |
| 6213 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6214 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6215 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 6216 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6217 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6218 | /* Console processes which share a common console can be sent CTRL+C or |
| 6219 | CTRL+BREAK events, provided they handle said events. */ |
| 6220 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 6221 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 6222 | err = GetLastError(); |
| 6223 | PyErr_SetFromWindowsErr(err); |
| 6224 | } |
| 6225 | else |
| 6226 | Py_RETURN_NONE; |
| 6227 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6228 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6229 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 6230 | attempt to open and terminate the process. */ |
| 6231 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 6232 | if (handle == NULL) { |
| 6233 | err = GetLastError(); |
| 6234 | return PyErr_SetFromWindowsErr(err); |
| 6235 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6236 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6237 | if (TerminateProcess(handle, sig) == 0) { |
| 6238 | err = GetLastError(); |
| 6239 | result = PyErr_SetFromWindowsErr(err); |
| 6240 | } else { |
| 6241 | Py_INCREF(Py_None); |
| 6242 | result = Py_None; |
| 6243 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6244 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6245 | CloseHandle(handle); |
| 6246 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 6247 | } |
| 6248 | #endif /* MS_WINDOWS */ |
| 6249 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6250 | #ifdef HAVE_PLOCK |
| 6251 | |
| 6252 | #ifdef HAVE_SYS_LOCK_H |
| 6253 | #include <sys/lock.h> |
| 6254 | #endif |
| 6255 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6256 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6257 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6258 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6259 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6260 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6261 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6262 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6263 | int op; |
| 6264 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 6265 | return NULL; |
| 6266 | if (plock(op) == -1) |
| 6267 | return posix_error(); |
| 6268 | Py_INCREF(Py_None); |
| 6269 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6270 | } |
| 6271 | #endif |
| 6272 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6273 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6274 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6275 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6276 | Set the current process's user id."); |
| 6277 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6278 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6279 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6280 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6281 | long uid_arg; |
| 6282 | uid_t uid; |
| 6283 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 6284 | return NULL; |
| 6285 | uid = uid_arg; |
| 6286 | if (uid != uid_arg) { |
| 6287 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6288 | return NULL; |
| 6289 | } |
| 6290 | if (setuid(uid) < 0) |
| 6291 | return posix_error(); |
| 6292 | Py_INCREF(Py_None); |
| 6293 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6294 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6295 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6296 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6297 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6298 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6299 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6300 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6301 | Set the current process's effective user id."); |
| 6302 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6303 | static PyObject * |
| 6304 | posix_seteuid (PyObject *self, PyObject *args) |
| 6305 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6306 | long euid_arg; |
| 6307 | uid_t euid; |
| 6308 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 6309 | return NULL; |
| 6310 | euid = euid_arg; |
| 6311 | if (euid != euid_arg) { |
| 6312 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6313 | return NULL; |
| 6314 | } |
| 6315 | if (seteuid(euid) < 0) { |
| 6316 | return posix_error(); |
| 6317 | } else { |
| 6318 | Py_INCREF(Py_None); |
| 6319 | return Py_None; |
| 6320 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6321 | } |
| 6322 | #endif /* HAVE_SETEUID */ |
| 6323 | |
| 6324 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6325 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6326 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6327 | Set the current process's effective group id."); |
| 6328 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6329 | static PyObject * |
| 6330 | posix_setegid (PyObject *self, PyObject *args) |
| 6331 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6332 | long egid_arg; |
| 6333 | gid_t egid; |
| 6334 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 6335 | return NULL; |
| 6336 | egid = egid_arg; |
| 6337 | if (egid != egid_arg) { |
| 6338 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6339 | return NULL; |
| 6340 | } |
| 6341 | if (setegid(egid) < 0) { |
| 6342 | return posix_error(); |
| 6343 | } else { |
| 6344 | Py_INCREF(Py_None); |
| 6345 | return Py_None; |
| 6346 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6347 | } |
| 6348 | #endif /* HAVE_SETEGID */ |
| 6349 | |
| 6350 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6351 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6352 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6353 | Set the current process's real and effective user ids."); |
| 6354 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6355 | static PyObject * |
| 6356 | posix_setreuid (PyObject *self, PyObject *args) |
| 6357 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6358 | long ruid_arg, euid_arg; |
| 6359 | uid_t ruid, euid; |
| 6360 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 6361 | return NULL; |
| 6362 | if (ruid_arg == -1) |
| 6363 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 6364 | else |
| 6365 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 6366 | if (euid_arg == -1) |
| 6367 | euid = (uid_t)-1; |
| 6368 | else |
| 6369 | euid = euid_arg; |
| 6370 | if ((euid_arg != -1 && euid != euid_arg) || |
| 6371 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 6372 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6373 | return NULL; |
| 6374 | } |
| 6375 | if (setreuid(ruid, euid) < 0) { |
| 6376 | return posix_error(); |
| 6377 | } else { |
| 6378 | Py_INCREF(Py_None); |
| 6379 | return Py_None; |
| 6380 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6381 | } |
| 6382 | #endif /* HAVE_SETREUID */ |
| 6383 | |
| 6384 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6385 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6386 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6387 | Set the current process's real and effective group ids."); |
| 6388 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6389 | static PyObject * |
| 6390 | posix_setregid (PyObject *self, PyObject *args) |
| 6391 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6392 | long rgid_arg, egid_arg; |
| 6393 | gid_t rgid, egid; |
| 6394 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 6395 | return NULL; |
| 6396 | if (rgid_arg == -1) |
| 6397 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 6398 | else |
| 6399 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6400 | if (egid_arg == -1) |
| 6401 | egid = (gid_t)-1; |
| 6402 | else |
| 6403 | egid = egid_arg; |
| 6404 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6405 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6406 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6407 | return NULL; |
| 6408 | } |
| 6409 | if (setregid(rgid, egid) < 0) { |
| 6410 | return posix_error(); |
| 6411 | } else { |
| 6412 | Py_INCREF(Py_None); |
| 6413 | return Py_None; |
| 6414 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6415 | } |
| 6416 | #endif /* HAVE_SETREGID */ |
| 6417 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6418 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6419 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6420 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6421 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6422 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6423 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6424 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6425 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6426 | long gid_arg; |
| 6427 | gid_t gid; |
| 6428 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6429 | return NULL; |
| 6430 | gid = gid_arg; |
| 6431 | if (gid != gid_arg) { |
| 6432 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6433 | return NULL; |
| 6434 | } |
| 6435 | if (setgid(gid) < 0) |
| 6436 | return posix_error(); |
| 6437 | Py_INCREF(Py_None); |
| 6438 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6439 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6440 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6441 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6442 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6443 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6444 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6445 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6446 | |
| 6447 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6448 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6449 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6450 | int i, len; |
| 6451 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6452 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6453 | if (!PySequence_Check(groups)) { |
| 6454 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6455 | return NULL; |
| 6456 | } |
| 6457 | len = PySequence_Size(groups); |
| 6458 | if (len > MAX_GROUPS) { |
| 6459 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6460 | return NULL; |
| 6461 | } |
| 6462 | for(i = 0; i < len; i++) { |
| 6463 | PyObject *elem; |
| 6464 | elem = PySequence_GetItem(groups, i); |
| 6465 | if (!elem) |
| 6466 | return NULL; |
| 6467 | if (!PyLong_Check(elem)) { |
| 6468 | PyErr_SetString(PyExc_TypeError, |
| 6469 | "groups must be integers"); |
| 6470 | Py_DECREF(elem); |
| 6471 | return NULL; |
| 6472 | } else { |
| 6473 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6474 | if (PyErr_Occurred()) { |
| 6475 | PyErr_SetString(PyExc_TypeError, |
| 6476 | "group id too big"); |
| 6477 | Py_DECREF(elem); |
| 6478 | return NULL; |
| 6479 | } |
| 6480 | grouplist[i] = x; |
| 6481 | /* read back the value to see if it fitted in gid_t */ |
| 6482 | if (grouplist[i] != x) { |
| 6483 | PyErr_SetString(PyExc_TypeError, |
| 6484 | "group id too big"); |
| 6485 | Py_DECREF(elem); |
| 6486 | return NULL; |
| 6487 | } |
| 6488 | } |
| 6489 | Py_DECREF(elem); |
| 6490 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6491 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6492 | if (setgroups(len, grouplist) < 0) |
| 6493 | return posix_error(); |
| 6494 | Py_INCREF(Py_None); |
| 6495 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6496 | } |
| 6497 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6498 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6499 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6500 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6501 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6502 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6503 | PyObject *result; |
| 6504 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6505 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6506 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6507 | if (pid == -1) |
| 6508 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6509 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6510 | if (struct_rusage == NULL) { |
| 6511 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6512 | if (m == NULL) |
| 6513 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6514 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6515 | Py_DECREF(m); |
| 6516 | if (struct_rusage == NULL) |
| 6517 | return NULL; |
| 6518 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6519 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6520 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6521 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6522 | if (!result) |
| 6523 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6524 | |
| 6525 | #ifndef doubletime |
| 6526 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6527 | #endif |
| 6528 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6529 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6530 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6531 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6532 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6533 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6534 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6535 | SET_INT(result, 2, ru->ru_maxrss); |
| 6536 | SET_INT(result, 3, ru->ru_ixrss); |
| 6537 | SET_INT(result, 4, ru->ru_idrss); |
| 6538 | SET_INT(result, 5, ru->ru_isrss); |
| 6539 | SET_INT(result, 6, ru->ru_minflt); |
| 6540 | SET_INT(result, 7, ru->ru_majflt); |
| 6541 | SET_INT(result, 8, ru->ru_nswap); |
| 6542 | SET_INT(result, 9, ru->ru_inblock); |
| 6543 | SET_INT(result, 10, ru->ru_oublock); |
| 6544 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6545 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6546 | SET_INT(result, 13, ru->ru_nsignals); |
| 6547 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6548 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6549 | #undef SET_INT |
| 6550 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6551 | if (PyErr_Occurred()) { |
| 6552 | Py_DECREF(result); |
| 6553 | return NULL; |
| 6554 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6555 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6556 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6557 | } |
| 6558 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6559 | |
| 6560 | #ifdef HAVE_WAIT3 |
| 6561 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6562 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6563 | Wait for completion of a child process."); |
| 6564 | |
| 6565 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6566 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6567 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6568 | pid_t pid; |
| 6569 | int options; |
| 6570 | struct rusage ru; |
| 6571 | WAIT_TYPE status; |
| 6572 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6573 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6574 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6575 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6576 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6577 | Py_BEGIN_ALLOW_THREADS |
| 6578 | pid = wait3(&status, options, &ru); |
| 6579 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6580 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6581 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6582 | } |
| 6583 | #endif /* HAVE_WAIT3 */ |
| 6584 | |
| 6585 | #ifdef HAVE_WAIT4 |
| 6586 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6587 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6588 | Wait for completion of a given child process."); |
| 6589 | |
| 6590 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6591 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6592 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6593 | pid_t pid; |
| 6594 | int options; |
| 6595 | struct rusage ru; |
| 6596 | WAIT_TYPE status; |
| 6597 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6598 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6599 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6600 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6601 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6602 | Py_BEGIN_ALLOW_THREADS |
| 6603 | pid = wait4(pid, &status, options, &ru); |
| 6604 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6605 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6606 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6607 | } |
| 6608 | #endif /* HAVE_WAIT4 */ |
| 6609 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6610 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6611 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6612 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6613 | Wait for the completion of one or more child processes.\n\n\ |
| 6614 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6615 | id specifies the pid to wait on.\n\ |
| 6616 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6617 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6618 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6619 | no children in a waitable state."); |
| 6620 | |
| 6621 | static PyObject * |
| 6622 | posix_waitid(PyObject *self, PyObject *args) |
| 6623 | { |
| 6624 | PyObject *result; |
| 6625 | idtype_t idtype; |
| 6626 | id_t id; |
| 6627 | int options, res; |
| 6628 | siginfo_t si; |
| 6629 | si.si_pid = 0; |
| 6630 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6631 | return NULL; |
| 6632 | Py_BEGIN_ALLOW_THREADS |
| 6633 | res = waitid(idtype, id, &si, options); |
| 6634 | Py_END_ALLOW_THREADS |
| 6635 | if (res == -1) |
| 6636 | return posix_error(); |
| 6637 | |
| 6638 | if (si.si_pid == 0) |
| 6639 | Py_RETURN_NONE; |
| 6640 | |
| 6641 | result = PyStructSequence_New(&WaitidResultType); |
| 6642 | if (!result) |
| 6643 | return NULL; |
| 6644 | |
| 6645 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6646 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6647 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6648 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6649 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6650 | if (PyErr_Occurred()) { |
| 6651 | Py_DECREF(result); |
| 6652 | return NULL; |
| 6653 | } |
| 6654 | |
| 6655 | return result; |
| 6656 | } |
| 6657 | #endif |
| 6658 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6659 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6660 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6661 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6662 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6663 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6664 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6665 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6666 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6667 | pid_t pid; |
| 6668 | int options; |
| 6669 | WAIT_TYPE status; |
| 6670 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6671 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6672 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6673 | return NULL; |
| 6674 | Py_BEGIN_ALLOW_THREADS |
| 6675 | pid = waitpid(pid, &status, options); |
| 6676 | Py_END_ALLOW_THREADS |
| 6677 | if (pid == -1) |
| 6678 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6679 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6680 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6681 | } |
| 6682 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6683 | #elif defined(HAVE_CWAIT) |
| 6684 | |
| 6685 | /* 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] | 6686 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6687 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6688 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6689 | |
| 6690 | static PyObject * |
| 6691 | posix_waitpid(PyObject *self, PyObject *args) |
| 6692 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6693 | Py_intptr_t pid; |
| 6694 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6695 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6696 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6697 | return NULL; |
| 6698 | Py_BEGIN_ALLOW_THREADS |
| 6699 | pid = _cwait(&status, pid, options); |
| 6700 | Py_END_ALLOW_THREADS |
| 6701 | if (pid == -1) |
| 6702 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6703 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6704 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6705 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6706 | } |
| 6707 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6708 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6709 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6710 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6711 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6712 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6713 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6714 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6715 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6716 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6717 | pid_t pid; |
| 6718 | WAIT_TYPE status; |
| 6719 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6720 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6721 | Py_BEGIN_ALLOW_THREADS |
| 6722 | pid = wait(&status); |
| 6723 | Py_END_ALLOW_THREADS |
| 6724 | if (pid == -1) |
| 6725 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6726 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6727 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6728 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6729 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6730 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6732 | PyDoc_STRVAR(posix_lstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6733 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6734 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6735 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6736 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6737 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6738 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6739 | #ifdef HAVE_LSTAT |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6740 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6741 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6742 | #ifdef MS_WINDOWS |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6743 | return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6744 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6745 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6746 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6747 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6748 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6749 | } |
| 6750 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6751 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6752 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6753 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6754 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6755 | Return a string representing the path to which the symbolic link points."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6756 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6757 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6758 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6759 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6760 | PyObject* v; |
| 6761 | char buf[MAXPATHLEN]; |
| 6762 | PyObject *opath; |
| 6763 | char *path; |
| 6764 | int n; |
| 6765 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6766 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6767 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 6768 | PyUnicode_FSConverter, &opath)) |
| 6769 | return NULL; |
| 6770 | path = PyBytes_AsString(opath); |
| 6771 | v = PySequence_GetItem(args, 0); |
| 6772 | if (v == NULL) { |
| 6773 | Py_DECREF(opath); |
| 6774 | return NULL; |
| 6775 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6776 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6777 | if (PyUnicode_Check(v)) { |
| 6778 | arg_is_unicode = 1; |
| 6779 | } |
| 6780 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6781 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6782 | Py_BEGIN_ALLOW_THREADS |
| 6783 | n = readlink(path, buf, (int) sizeof buf); |
| 6784 | Py_END_ALLOW_THREADS |
| 6785 | if (n < 0) |
| 6786 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6787 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6788 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6789 | if (arg_is_unicode) |
| 6790 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 6791 | else |
| 6792 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6793 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6794 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6795 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6796 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6797 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6798 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6799 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6800 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6801 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6802 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6803 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6804 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6805 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6806 | } |
| 6807 | #endif /* HAVE_SYMLINK */ |
| 6808 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6809 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6810 | |
| 6811 | PyDoc_STRVAR(win_readlink__doc__, |
| 6812 | "readlink(path) -> path\n\n\ |
| 6813 | Return a string representing the path to which the symbolic link points."); |
| 6814 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6815 | /* Windows readlink implementation */ |
| 6816 | static PyObject * |
| 6817 | win_readlink(PyObject *self, PyObject *args) |
| 6818 | { |
| 6819 | wchar_t *path; |
| 6820 | DWORD n_bytes_returned; |
| 6821 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6822 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6823 | HANDLE reparse_point_handle; |
| 6824 | |
| 6825 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6826 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6827 | wchar_t *print_name; |
| 6828 | |
| 6829 | if (!PyArg_ParseTuple(args, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6830 | "U:readlink", |
| 6831 | &po)) |
| 6832 | return NULL; |
| 6833 | path = PyUnicode_AsUnicode(po); |
| 6834 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6835 | return NULL; |
| 6836 | |
| 6837 | /* First get a handle to the reparse point */ |
| 6838 | Py_BEGIN_ALLOW_THREADS |
| 6839 | reparse_point_handle = CreateFileW( |
| 6840 | path, |
| 6841 | 0, |
| 6842 | 0, |
| 6843 | 0, |
| 6844 | OPEN_EXISTING, |
| 6845 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6846 | 0); |
| 6847 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6848 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6849 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6850 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6851 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6852 | Py_BEGIN_ALLOW_THREADS |
| 6853 | /* New call DeviceIoControl to read the reparse point */ |
| 6854 | io_result = DeviceIoControl( |
| 6855 | reparse_point_handle, |
| 6856 | FSCTL_GET_REPARSE_POINT, |
| 6857 | 0, 0, /* in buffer */ |
| 6858 | target_buffer, sizeof(target_buffer), |
| 6859 | &n_bytes_returned, |
| 6860 | 0 /* we're not using OVERLAPPED_IO */ |
| 6861 | ); |
| 6862 | CloseHandle(reparse_point_handle); |
| 6863 | Py_END_ALLOW_THREADS |
| 6864 | |
| 6865 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6866 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6867 | |
| 6868 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6869 | { |
| 6870 | PyErr_SetString(PyExc_ValueError, |
| 6871 | "not a symbolic link"); |
| 6872 | return NULL; |
| 6873 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6874 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6875 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6876 | |
| 6877 | result = PyUnicode_FromWideChar(print_name, |
| 6878 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6879 | return result; |
| 6880 | } |
| 6881 | |
| 6882 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6883 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6884 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6885 | |
| 6886 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6887 | static int has_CreateSymbolicLinkW = 0; |
| 6888 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 6889 | static int |
| 6890 | check_CreateSymbolicLinkW() |
| 6891 | { |
| 6892 | HINSTANCE hKernel32; |
| 6893 | /* only recheck */ |
| 6894 | if (has_CreateSymbolicLinkW) |
| 6895 | return has_CreateSymbolicLinkW; |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 6896 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6897 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6898 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6899 | if (Py_CreateSymbolicLinkW) |
| 6900 | has_CreateSymbolicLinkW = 1; |
| 6901 | return has_CreateSymbolicLinkW; |
| 6902 | } |
| 6903 | |
| 6904 | PyDoc_STRVAR(win_symlink__doc__, |
| 6905 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 6906 | Create a symbolic link pointing to src named dst.\n\ |
| 6907 | target_is_directory is required if the target is to be interpreted as\n\ |
| 6908 | a directory.\n\ |
| 6909 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 6910 | NotImplementedError otherwise."); |
| 6911 | |
| 6912 | static PyObject * |
| 6913 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6914 | { |
| 6915 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6916 | PyObject *osrc, *odest; |
| 6917 | PyObject *usrc = NULL, *udest = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6918 | wchar_t *wsrc, *wdest; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6919 | int target_is_directory = 0; |
| 6920 | DWORD res; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6921 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6922 | if (!check_CreateSymbolicLinkW()) |
| 6923 | { |
| 6924 | /* raise NotImplementedError */ |
| 6925 | return PyErr_Format(PyExc_NotImplementedError, |
| 6926 | "CreateSymbolicLinkW not found"); |
| 6927 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6928 | if (!PyArg_ParseTupleAndKeywords( |
| 6929 | args, kwargs, "OO|i:symlink", kwlist, |
| 6930 | &osrc, &odest, &target_is_directory)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6931 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6932 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6933 | usrc = win32_decode_filename(osrc); |
| 6934 | if (!usrc) |
| 6935 | return NULL; |
| 6936 | udest = win32_decode_filename(odest); |
| 6937 | if (!udest) |
| 6938 | goto error; |
| 6939 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6940 | if (win32_can_symlink == 0) |
| 6941 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 6942 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6943 | wsrc = PyUnicode_AsUnicode(usrc); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6944 | if (wsrc == NULL) |
| 6945 | goto error; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6946 | wdest = PyUnicode_AsUnicode(udest); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6947 | if (wsrc == NULL) |
| 6948 | goto error; |
| 6949 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6950 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6951 | res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6952 | Py_END_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6953 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6954 | Py_DECREF(usrc); |
| 6955 | Py_DECREF(udest); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6956 | if (!res) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6957 | return win32_error_object("symlink", osrc); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6958 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6959 | Py_INCREF(Py_None); |
| 6960 | return Py_None; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6961 | |
| 6962 | error: |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6963 | Py_XDECREF(usrc); |
| 6964 | Py_XDECREF(udest); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6965 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6966 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6967 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6968 | |
| 6969 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6970 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6971 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6972 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6973 | { |
| 6974 | ULONG value = 0; |
| 6975 | |
| 6976 | Py_BEGIN_ALLOW_THREADS |
| 6977 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6978 | Py_END_ALLOW_THREADS |
| 6979 | |
| 6980 | return value; |
| 6981 | } |
| 6982 | |
| 6983 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6984 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6985 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6986 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6987 | return Py_BuildValue("ddddd", |
| 6988 | (double)0 /* t.tms_utime / HZ */, |
| 6989 | (double)0 /* t.tms_stime / HZ */, |
| 6990 | (double)0 /* t.tms_cutime / HZ */, |
| 6991 | (double)0 /* t.tms_cstime / HZ */, |
| 6992 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6993 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6994 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 6995 | #define NEED_TICKS_PER_SECOND |
| 6996 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6997 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6998 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6999 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7000 | struct tms t; |
| 7001 | clock_t c; |
| 7002 | errno = 0; |
| 7003 | c = times(&t); |
| 7004 | if (c == (clock_t) -1) |
| 7005 | return posix_error(); |
| 7006 | return Py_BuildValue("ddddd", |
| 7007 | (double)t.tms_utime / ticks_per_second, |
| 7008 | (double)t.tms_stime / ticks_per_second, |
| 7009 | (double)t.tms_cutime / ticks_per_second, |
| 7010 | (double)t.tms_cstime / ticks_per_second, |
| 7011 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7012 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7013 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7014 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7015 | |
| 7016 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7017 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7018 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7019 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7020 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7021 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7022 | FILETIME create, exit, kernel, user; |
| 7023 | HANDLE hProc; |
| 7024 | hProc = GetCurrentProcess(); |
| 7025 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 7026 | /* The fields of a FILETIME structure are the hi and lo part |
| 7027 | of a 64-bit value expressed in 100 nanosecond units. |
| 7028 | 1e7 is one second in such units; 1e-7 the inverse. |
| 7029 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 7030 | */ |
| 7031 | return Py_BuildValue( |
| 7032 | "ddddd", |
| 7033 | (double)(user.dwHighDateTime*429.4967296 + |
| 7034 | user.dwLowDateTime*1e-7), |
| 7035 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 7036 | kernel.dwLowDateTime*1e-7), |
| 7037 | (double)0, |
| 7038 | (double)0, |
| 7039 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 7040 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7041 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7042 | |
| 7043 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7044 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7045 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7046 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 7047 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7048 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7049 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7050 | #ifdef HAVE_GETSID |
| 7051 | PyDoc_STRVAR(posix_getsid__doc__, |
| 7052 | "getsid(pid) -> sid\n\n\ |
| 7053 | Call the system call getsid()."); |
| 7054 | |
| 7055 | static PyObject * |
| 7056 | posix_getsid(PyObject *self, PyObject *args) |
| 7057 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7058 | pid_t pid; |
| 7059 | int sid; |
| 7060 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 7061 | return NULL; |
| 7062 | sid = getsid(pid); |
| 7063 | if (sid < 0) |
| 7064 | return posix_error(); |
| 7065 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7066 | } |
| 7067 | #endif /* HAVE_GETSID */ |
| 7068 | |
| 7069 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7070 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7071 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7072 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7073 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7074 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7075 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7076 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7077 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7078 | if (setsid() < 0) |
| 7079 | return posix_error(); |
| 7080 | Py_INCREF(Py_None); |
| 7081 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7082 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7083 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7084 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7085 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7086 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7087 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7088 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7089 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7090 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7091 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7092 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7093 | pid_t pid; |
| 7094 | int pgrp; |
| 7095 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 7096 | return NULL; |
| 7097 | if (setpgid(pid, pgrp) < 0) |
| 7098 | return posix_error(); |
| 7099 | Py_INCREF(Py_None); |
| 7100 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7101 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7102 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7103 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7104 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7105 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7106 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7107 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7108 | 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] | 7109 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7110 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7111 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7112 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7113 | int fd; |
| 7114 | pid_t pgid; |
| 7115 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 7116 | return NULL; |
| 7117 | pgid = tcgetpgrp(fd); |
| 7118 | if (pgid < 0) |
| 7119 | return posix_error(); |
| 7120 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7121 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7122 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7123 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7124 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7125 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7126 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7127 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7128 | 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] | 7129 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7130 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7131 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7132 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7133 | int fd; |
| 7134 | pid_t pgid; |
| 7135 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 7136 | return NULL; |
| 7137 | if (tcsetpgrp(fd, pgid) < 0) |
| 7138 | return posix_error(); |
| 7139 | Py_INCREF(Py_None); |
| 7140 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 7141 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7142 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7143 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7144 | /* Functions acting on file descriptors */ |
| 7145 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7146 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7147 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7148 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7149 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7150 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7151 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7152 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7153 | PyObject *ofile; |
| 7154 | char *file; |
| 7155 | int flag; |
| 7156 | int mode = 0777; |
| 7157 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7158 | |
| 7159 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7160 | PyObject *po; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 7161 | if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7162 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 7163 | if (wpath == NULL) |
| 7164 | return NULL; |
| 7165 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7166 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7167 | fd = _wopen(wpath, flag, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7168 | Py_END_ALLOW_THREADS |
| 7169 | if (fd < 0) |
| 7170 | return posix_error(); |
| 7171 | return PyLong_FromLong((long)fd); |
| 7172 | } |
| 7173 | /* Drop the argument parsing error as narrow strings |
| 7174 | are also valid. */ |
| 7175 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 7176 | #endif |
| 7177 | |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 7178 | if (!PyArg_ParseTuple(args, "O&i|i:open", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7179 | PyUnicode_FSConverter, &ofile, |
| 7180 | &flag, &mode)) |
| 7181 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 7182 | #ifdef MS_WINDOWS |
| 7183 | if (win32_warn_bytes_api()) { |
| 7184 | Py_DECREF(ofile); |
| 7185 | return NULL; |
| 7186 | } |
| 7187 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7188 | file = PyBytes_AsString(ofile); |
| 7189 | Py_BEGIN_ALLOW_THREADS |
| 7190 | fd = open(file, flag, mode); |
| 7191 | Py_END_ALLOW_THREADS |
| 7192 | if (fd < 0) |
| 7193 | return posix_error_with_allocated_filename(ofile); |
| 7194 | Py_DECREF(ofile); |
| 7195 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7196 | } |
| 7197 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7198 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7199 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7200 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7201 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7202 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7203 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7204 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7205 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7206 | int fd, res; |
| 7207 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 7208 | return NULL; |
| 7209 | if (!_PyVerify_fd(fd)) |
| 7210 | return posix_error(); |
| 7211 | Py_BEGIN_ALLOW_THREADS |
| 7212 | res = close(fd); |
| 7213 | Py_END_ALLOW_THREADS |
| 7214 | if (res < 0) |
| 7215 | return posix_error(); |
| 7216 | Py_INCREF(Py_None); |
| 7217 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7221 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7222 | "closerange(fd_low, fd_high)\n\n\ |
| 7223 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 7224 | |
| 7225 | static PyObject * |
| 7226 | posix_closerange(PyObject *self, PyObject *args) |
| 7227 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7228 | int fd_from, fd_to, i; |
| 7229 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 7230 | return NULL; |
| 7231 | Py_BEGIN_ALLOW_THREADS |
| 7232 | for (i = fd_from; i < fd_to; i++) |
| 7233 | if (_PyVerify_fd(i)) |
| 7234 | close(i); |
| 7235 | Py_END_ALLOW_THREADS |
| 7236 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7237 | } |
| 7238 | |
| 7239 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7240 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7241 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7242 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7243 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7244 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7245 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7246 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7247 | int fd; |
| 7248 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 7249 | return NULL; |
| 7250 | if (!_PyVerify_fd(fd)) |
| 7251 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7252 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7253 | if (fd < 0) |
| 7254 | return posix_error(); |
| 7255 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7256 | } |
| 7257 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7259 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 7260 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7261 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7262 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7263 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7264 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7265 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7266 | int fd, fd2, res; |
| 7267 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 7268 | return NULL; |
| 7269 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 7270 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7271 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7272 | if (res < 0) |
| 7273 | return posix_error(); |
| 7274 | Py_INCREF(Py_None); |
| 7275 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7276 | } |
| 7277 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7278 | #ifdef HAVE_LOCKF |
| 7279 | PyDoc_STRVAR(posix_lockf__doc__, |
| 7280 | "lockf(fd, cmd, len)\n\n\ |
| 7281 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 7282 | fd is an open file descriptor.\n\ |
| 7283 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 7284 | F_TEST.\n\ |
| 7285 | len specifies the section of the file to lock."); |
| 7286 | |
| 7287 | static PyObject * |
| 7288 | posix_lockf(PyObject *self, PyObject *args) |
| 7289 | { |
| 7290 | int fd, cmd, res; |
| 7291 | off_t len; |
| 7292 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 7293 | &fd, &cmd, _parse_off_t, &len)) |
| 7294 | return NULL; |
| 7295 | |
| 7296 | Py_BEGIN_ALLOW_THREADS |
| 7297 | res = lockf(fd, cmd, len); |
| 7298 | Py_END_ALLOW_THREADS |
| 7299 | |
| 7300 | if (res < 0) |
| 7301 | return posix_error(); |
| 7302 | |
| 7303 | Py_RETURN_NONE; |
| 7304 | } |
| 7305 | #endif |
| 7306 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7307 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7308 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7309 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7310 | Set the current position of a file descriptor.\n\ |
| 7311 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7312 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7313 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7314 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7315 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7316 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7317 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7318 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7319 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7320 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7321 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7322 | PyObject *posobj; |
| 7323 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7324 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7325 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7326 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7327 | switch (how) { |
| 7328 | case 0: how = SEEK_SET; break; |
| 7329 | case 1: how = SEEK_CUR; break; |
| 7330 | case 2: how = SEEK_END; break; |
| 7331 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7332 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7333 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7334 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7335 | pos = PyLong_AsLong(posobj); |
| 7336 | #else |
| 7337 | pos = PyLong_AsLongLong(posobj); |
| 7338 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7339 | if (PyErr_Occurred()) |
| 7340 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7341 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7342 | if (!_PyVerify_fd(fd)) |
| 7343 | return posix_error(); |
| 7344 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7345 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7346 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7347 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7348 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7349 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7350 | Py_END_ALLOW_THREADS |
| 7351 | if (res < 0) |
| 7352 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7353 | |
| 7354 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7355 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7356 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7357 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7358 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7359 | } |
| 7360 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7361 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7362 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7363 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7364 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7365 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7366 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7367 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7368 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7369 | int fd, size; |
| 7370 | Py_ssize_t n; |
| 7371 | PyObject *buffer; |
| 7372 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7373 | return NULL; |
| 7374 | if (size < 0) { |
| 7375 | errno = EINVAL; |
| 7376 | return posix_error(); |
| 7377 | } |
| 7378 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7379 | if (buffer == NULL) |
| 7380 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7381 | if (!_PyVerify_fd(fd)) { |
| 7382 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7383 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7384 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7385 | Py_BEGIN_ALLOW_THREADS |
| 7386 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7387 | Py_END_ALLOW_THREADS |
| 7388 | if (n < 0) { |
| 7389 | Py_DECREF(buffer); |
| 7390 | return posix_error(); |
| 7391 | } |
| 7392 | if (n != size) |
| 7393 | _PyBytes_Resize(&buffer, n); |
| 7394 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7395 | } |
| 7396 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7397 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7398 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7399 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7400 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7401 | { |
| 7402 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7403 | Py_ssize_t blen, total = 0; |
| 7404 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7405 | *iov = PyMem_New(struct iovec, cnt); |
| 7406 | if (*iov == NULL) { |
| 7407 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7408 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7409 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7410 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7411 | *buf = PyMem_New(Py_buffer, cnt); |
| 7412 | if (*buf == NULL) { |
| 7413 | PyMem_Del(*iov); |
| 7414 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7415 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7416 | } |
| 7417 | |
| 7418 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7419 | PyObject *item = PySequence_GetItem(seq, i); |
| 7420 | if (item == NULL) |
| 7421 | goto fail; |
| 7422 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7423 | Py_DECREF(item); |
| 7424 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7425 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7426 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7427 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7428 | blen = (*buf)[i].len; |
| 7429 | (*iov)[i].iov_len = blen; |
| 7430 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7431 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7432 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7433 | |
| 7434 | fail: |
| 7435 | PyMem_Del(*iov); |
| 7436 | for (j = 0; j < i; j++) { |
| 7437 | PyBuffer_Release(&(*buf)[j]); |
| 7438 | } |
| 7439 | PyMem_Del(*buf); |
| 7440 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7441 | } |
| 7442 | |
| 7443 | static void |
| 7444 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7445 | { |
| 7446 | int i; |
| 7447 | PyMem_Del(iov); |
| 7448 | for (i = 0; i < cnt; i++) { |
| 7449 | PyBuffer_Release(&buf[i]); |
| 7450 | } |
| 7451 | PyMem_Del(buf); |
| 7452 | } |
| 7453 | #endif |
| 7454 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7455 | #ifdef HAVE_READV |
| 7456 | PyDoc_STRVAR(posix_readv__doc__, |
| 7457 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7458 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7459 | is an arbitrary sequence of writable buffers.\n\ |
| 7460 | Returns the total number of bytes read."); |
| 7461 | |
| 7462 | static PyObject * |
| 7463 | posix_readv(PyObject *self, PyObject *args) |
| 7464 | { |
| 7465 | int fd, cnt; |
| 7466 | Py_ssize_t n; |
| 7467 | PyObject *seq; |
| 7468 | struct iovec *iov; |
| 7469 | Py_buffer *buf; |
| 7470 | |
| 7471 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7472 | return NULL; |
| 7473 | if (!PySequence_Check(seq)) { |
| 7474 | PyErr_SetString(PyExc_TypeError, |
| 7475 | "readv() arg 2 must be a sequence"); |
| 7476 | return NULL; |
| 7477 | } |
| 7478 | cnt = PySequence_Size(seq); |
| 7479 | |
| 7480 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7481 | return NULL; |
| 7482 | |
| 7483 | Py_BEGIN_ALLOW_THREADS |
| 7484 | n = readv(fd, iov, cnt); |
| 7485 | Py_END_ALLOW_THREADS |
| 7486 | |
| 7487 | iov_cleanup(iov, buf, cnt); |
| 7488 | return PyLong_FromSsize_t(n); |
| 7489 | } |
| 7490 | #endif |
| 7491 | |
| 7492 | #ifdef HAVE_PREAD |
| 7493 | PyDoc_STRVAR(posix_pread__doc__, |
| 7494 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7495 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7496 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7497 | |
| 7498 | static PyObject * |
| 7499 | posix_pread(PyObject *self, PyObject *args) |
| 7500 | { |
| 7501 | int fd, size; |
| 7502 | off_t offset; |
| 7503 | Py_ssize_t n; |
| 7504 | PyObject *buffer; |
| 7505 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7506 | return NULL; |
| 7507 | |
| 7508 | if (size < 0) { |
| 7509 | errno = EINVAL; |
| 7510 | return posix_error(); |
| 7511 | } |
| 7512 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7513 | if (buffer == NULL) |
| 7514 | return NULL; |
| 7515 | if (!_PyVerify_fd(fd)) { |
| 7516 | Py_DECREF(buffer); |
| 7517 | return posix_error(); |
| 7518 | } |
| 7519 | Py_BEGIN_ALLOW_THREADS |
| 7520 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7521 | Py_END_ALLOW_THREADS |
| 7522 | if (n < 0) { |
| 7523 | Py_DECREF(buffer); |
| 7524 | return posix_error(); |
| 7525 | } |
| 7526 | if (n != size) |
| 7527 | _PyBytes_Resize(&buffer, n); |
| 7528 | return buffer; |
| 7529 | } |
| 7530 | #endif |
| 7531 | |
| 7532 | PyDoc_STRVAR(posix_write__doc__, |
| 7533 | "write(fd, string) -> byteswritten\n\n\ |
| 7534 | Write a string to a file descriptor."); |
| 7535 | |
| 7536 | static PyObject * |
| 7537 | posix_write(PyObject *self, PyObject *args) |
| 7538 | { |
| 7539 | Py_buffer pbuf; |
| 7540 | int fd; |
| 7541 | Py_ssize_t size, len; |
| 7542 | |
| 7543 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7544 | return NULL; |
| 7545 | if (!_PyVerify_fd(fd)) { |
| 7546 | PyBuffer_Release(&pbuf); |
| 7547 | return posix_error(); |
| 7548 | } |
| 7549 | len = pbuf.len; |
| 7550 | Py_BEGIN_ALLOW_THREADS |
| 7551 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7552 | if (len > INT_MAX) |
| 7553 | len = INT_MAX; |
| 7554 | size = write(fd, pbuf.buf, (int)len); |
| 7555 | #else |
| 7556 | size = write(fd, pbuf.buf, len); |
| 7557 | #endif |
| 7558 | Py_END_ALLOW_THREADS |
| 7559 | PyBuffer_Release(&pbuf); |
| 7560 | if (size < 0) |
| 7561 | return posix_error(); |
| 7562 | return PyLong_FromSsize_t(size); |
| 7563 | } |
| 7564 | |
| 7565 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7566 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7567 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7568 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7569 | -> byteswritten\n\ |
| 7570 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7571 | |
| 7572 | static PyObject * |
| 7573 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7574 | { |
| 7575 | int in, out; |
| 7576 | Py_ssize_t ret; |
| 7577 | off_t offset; |
| 7578 | |
| 7579 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7580 | #ifndef __APPLE__ |
| 7581 | Py_ssize_t len; |
| 7582 | #endif |
| 7583 | PyObject *headers = NULL, *trailers = NULL; |
| 7584 | Py_buffer *hbuf, *tbuf; |
| 7585 | off_t sbytes; |
| 7586 | struct sf_hdtr sf; |
| 7587 | int flags = 0; |
| 7588 | sf.headers = NULL; |
| 7589 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7590 | static char *keywords[] = {"out", "in", |
| 7591 | "offset", "count", |
| 7592 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7593 | |
| 7594 | #ifdef __APPLE__ |
| 7595 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7596 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7597 | #else |
| 7598 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7599 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7600 | #endif |
| 7601 | &headers, &trailers, &flags)) |
| 7602 | return NULL; |
| 7603 | if (headers != NULL) { |
| 7604 | if (!PySequence_Check(headers)) { |
| 7605 | PyErr_SetString(PyExc_TypeError, |
| 7606 | "sendfile() headers must be a sequence or None"); |
| 7607 | return NULL; |
| 7608 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7609 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7610 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7611 | if (sf.hdr_cnt > 0 && |
| 7612 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7613 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7614 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7615 | #ifdef __APPLE__ |
| 7616 | sbytes += i; |
| 7617 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7618 | } |
| 7619 | } |
| 7620 | if (trailers != NULL) { |
| 7621 | if (!PySequence_Check(trailers)) { |
| 7622 | PyErr_SetString(PyExc_TypeError, |
| 7623 | "sendfile() trailers must be a sequence or None"); |
| 7624 | return NULL; |
| 7625 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7626 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7627 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7628 | if (sf.trl_cnt > 0 && |
| 7629 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7630 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7631 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7632 | #ifdef __APPLE__ |
| 7633 | sbytes += i; |
| 7634 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7635 | } |
| 7636 | } |
| 7637 | |
| 7638 | Py_BEGIN_ALLOW_THREADS |
| 7639 | #ifdef __APPLE__ |
| 7640 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7641 | #else |
| 7642 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7643 | #endif |
| 7644 | Py_END_ALLOW_THREADS |
| 7645 | |
| 7646 | if (sf.headers != NULL) |
| 7647 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7648 | if (sf.trailers != NULL) |
| 7649 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7650 | |
| 7651 | if (ret < 0) { |
| 7652 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7653 | if (sbytes != 0) { |
| 7654 | // some data has been sent |
| 7655 | goto done; |
| 7656 | } |
| 7657 | else { |
| 7658 | // no data has been sent; upper application is supposed |
| 7659 | // to retry on EAGAIN or EBUSY |
| 7660 | return posix_error(); |
| 7661 | } |
| 7662 | } |
| 7663 | return posix_error(); |
| 7664 | } |
| 7665 | goto done; |
| 7666 | |
| 7667 | done: |
| 7668 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7669 | return Py_BuildValue("l", sbytes); |
| 7670 | #else |
| 7671 | return Py_BuildValue("L", sbytes); |
| 7672 | #endif |
| 7673 | |
| 7674 | #else |
| 7675 | Py_ssize_t count; |
| 7676 | PyObject *offobj; |
| 7677 | static char *keywords[] = {"out", "in", |
| 7678 | "offset", "count", NULL}; |
| 7679 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7680 | keywords, &out, &in, &offobj, &count)) |
| 7681 | return NULL; |
| 7682 | #ifdef linux |
| 7683 | if (offobj == Py_None) { |
| 7684 | Py_BEGIN_ALLOW_THREADS |
| 7685 | ret = sendfile(out, in, NULL, count); |
| 7686 | Py_END_ALLOW_THREADS |
| 7687 | if (ret < 0) |
| 7688 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7689 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7690 | } |
| 7691 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7692 | if (!_parse_off_t(offobj, &offset)) |
| 7693 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7694 | Py_BEGIN_ALLOW_THREADS |
| 7695 | ret = sendfile(out, in, &offset, count); |
| 7696 | Py_END_ALLOW_THREADS |
| 7697 | if (ret < 0) |
| 7698 | return posix_error(); |
| 7699 | return Py_BuildValue("n", ret); |
| 7700 | #endif |
| 7701 | } |
| 7702 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7703 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7704 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7705 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7706 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7707 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7708 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7709 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7710 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7711 | int fd; |
| 7712 | STRUCT_STAT st; |
| 7713 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7714 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7715 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7716 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7717 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7718 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7719 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7720 | if (!_PyVerify_fd(fd)) |
| 7721 | return posix_error(); |
| 7722 | Py_BEGIN_ALLOW_THREADS |
| 7723 | res = FSTAT(fd, &st); |
| 7724 | Py_END_ALLOW_THREADS |
| 7725 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7726 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7727 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7728 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7729 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7730 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7731 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7732 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7733 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7734 | } |
| 7735 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7736 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7737 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7738 | 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] | 7739 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7740 | |
| 7741 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7742 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7743 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7744 | int fd; |
| 7745 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7746 | return NULL; |
| 7747 | if (!_PyVerify_fd(fd)) |
| 7748 | return PyBool_FromLong(0); |
| 7749 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7750 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7751 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7752 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7753 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7754 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7755 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7756 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7757 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7758 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7759 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7760 | #if defined(PYOS_OS2) |
| 7761 | HFILE read, write; |
| 7762 | APIRET rc; |
| 7763 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7764 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7765 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7766 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7767 | |
| 7768 | return Py_BuildValue("(ii)", read, write); |
| 7769 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7770 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7771 | int fds[2]; |
| 7772 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7773 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7774 | if (res != 0) |
| 7775 | return posix_error(); |
| 7776 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7777 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7778 | HANDLE read, write; |
| 7779 | int read_fd, write_fd; |
| 7780 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7781 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7782 | if (!ok) |
| 7783 | return win32_error("CreatePipe", NULL); |
| 7784 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7785 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7786 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7787 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7788 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7789 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7790 | #endif /* HAVE_PIPE */ |
| 7791 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7792 | #ifdef HAVE_PIPE2 |
| 7793 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7794 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7795 | Create a pipe with flags set atomically.\n\ |
| 7796 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7797 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7798 | "); |
| 7799 | |
| 7800 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7801 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7802 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7803 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7804 | int fds[2]; |
| 7805 | int res; |
| 7806 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7807 | flags = PyLong_AsLong(arg); |
| 7808 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7809 | return NULL; |
| 7810 | |
| 7811 | res = pipe2(fds, flags); |
| 7812 | if (res != 0) |
| 7813 | return posix_error(); |
| 7814 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7815 | } |
| 7816 | #endif /* HAVE_PIPE2 */ |
| 7817 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7818 | #ifdef HAVE_WRITEV |
| 7819 | PyDoc_STRVAR(posix_writev__doc__, |
| 7820 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7821 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7822 | arbitrary sequence of buffers.\n\ |
| 7823 | Returns the total bytes written."); |
| 7824 | |
| 7825 | static PyObject * |
| 7826 | posix_writev(PyObject *self, PyObject *args) |
| 7827 | { |
| 7828 | int fd, cnt; |
| 7829 | Py_ssize_t res; |
| 7830 | PyObject *seq; |
| 7831 | struct iovec *iov; |
| 7832 | Py_buffer *buf; |
| 7833 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7834 | return NULL; |
| 7835 | if (!PySequence_Check(seq)) { |
| 7836 | PyErr_SetString(PyExc_TypeError, |
| 7837 | "writev() arg 2 must be a sequence"); |
| 7838 | return NULL; |
| 7839 | } |
| 7840 | cnt = PySequence_Size(seq); |
| 7841 | |
| 7842 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7843 | return NULL; |
| 7844 | } |
| 7845 | |
| 7846 | Py_BEGIN_ALLOW_THREADS |
| 7847 | res = writev(fd, iov, cnt); |
| 7848 | Py_END_ALLOW_THREADS |
| 7849 | |
| 7850 | iov_cleanup(iov, buf, cnt); |
| 7851 | return PyLong_FromSsize_t(res); |
| 7852 | } |
| 7853 | #endif |
| 7854 | |
| 7855 | #ifdef HAVE_PWRITE |
| 7856 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7857 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7858 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7859 | offset unchanged."); |
| 7860 | |
| 7861 | static PyObject * |
| 7862 | posix_pwrite(PyObject *self, PyObject *args) |
| 7863 | { |
| 7864 | Py_buffer pbuf; |
| 7865 | int fd; |
| 7866 | off_t offset; |
| 7867 | Py_ssize_t size; |
| 7868 | |
| 7869 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7870 | return NULL; |
| 7871 | |
| 7872 | if (!_PyVerify_fd(fd)) { |
| 7873 | PyBuffer_Release(&pbuf); |
| 7874 | return posix_error(); |
| 7875 | } |
| 7876 | Py_BEGIN_ALLOW_THREADS |
| 7877 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7878 | Py_END_ALLOW_THREADS |
| 7879 | PyBuffer_Release(&pbuf); |
| 7880 | if (size < 0) |
| 7881 | return posix_error(); |
| 7882 | return PyLong_FromSsize_t(size); |
| 7883 | } |
| 7884 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7885 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7886 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7887 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7888 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7889 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7890 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7891 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7892 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7893 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7894 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7895 | char *filename; |
| 7896 | int mode = 0666; |
| 7897 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7898 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 7899 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7900 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7901 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7902 | Py_BEGIN_ALLOW_THREADS |
| 7903 | res = mkfifo(filename, mode); |
| 7904 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7905 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7906 | if (res < 0) |
| 7907 | return posix_error(); |
| 7908 | Py_INCREF(Py_None); |
| 7909 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7910 | } |
| 7911 | #endif |
| 7912 | |
| 7913 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7914 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7915 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7916 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7917 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7918 | named filename. mode specifies both the permissions to use and the\n\ |
| 7919 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7920 | 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] | 7921 | device defines the newly created device special file (probably using\n\ |
| 7922 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7923 | |
| 7924 | |
| 7925 | static PyObject * |
| 7926 | posix_mknod(PyObject *self, PyObject *args) |
| 7927 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7928 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7929 | char *filename; |
| 7930 | int mode = 0600; |
| 7931 | int device = 0; |
| 7932 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7933 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 7934 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7935 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7936 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7937 | Py_BEGIN_ALLOW_THREADS |
| 7938 | res = mknod(filename, mode, device); |
| 7939 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7940 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7941 | if (res < 0) |
| 7942 | return posix_error(); |
| 7943 | Py_INCREF(Py_None); |
| 7944 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7945 | } |
| 7946 | #endif |
| 7947 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7948 | #ifdef HAVE_DEVICE_MACROS |
| 7949 | PyDoc_STRVAR(posix_major__doc__, |
| 7950 | "major(device) -> major number\n\ |
| 7951 | Extracts a device major number from a raw device number."); |
| 7952 | |
| 7953 | static PyObject * |
| 7954 | posix_major(PyObject *self, PyObject *args) |
| 7955 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7956 | int device; |
| 7957 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7958 | return NULL; |
| 7959 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7960 | } |
| 7961 | |
| 7962 | PyDoc_STRVAR(posix_minor__doc__, |
| 7963 | "minor(device) -> minor number\n\ |
| 7964 | Extracts a device minor number from a raw device number."); |
| 7965 | |
| 7966 | static PyObject * |
| 7967 | posix_minor(PyObject *self, PyObject *args) |
| 7968 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7969 | int device; |
| 7970 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7971 | return NULL; |
| 7972 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7973 | } |
| 7974 | |
| 7975 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7976 | "makedev(major, minor) -> device number\n\ |
| 7977 | Composes a raw device number from the major and minor device numbers."); |
| 7978 | |
| 7979 | static PyObject * |
| 7980 | posix_makedev(PyObject *self, PyObject *args) |
| 7981 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7982 | int major, minor; |
| 7983 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7984 | return NULL; |
| 7985 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7986 | } |
| 7987 | #endif /* device macros */ |
| 7988 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7989 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7990 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7991 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7992 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7993 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7994 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7995 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7996 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7997 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7998 | int fd; |
| 7999 | off_t length; |
| 8000 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8001 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8002 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8003 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8005 | Py_BEGIN_ALLOW_THREADS |
| 8006 | res = ftruncate(fd, length); |
| 8007 | Py_END_ALLOW_THREADS |
| 8008 | if (res < 0) |
| 8009 | return posix_error(); |
| 8010 | Py_INCREF(Py_None); |
| 8011 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8012 | } |
| 8013 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8014 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8015 | #ifdef HAVE_TRUNCATE |
| 8016 | PyDoc_STRVAR(posix_truncate__doc__, |
| 8017 | "truncate(path, length)\n\n\ |
| 8018 | Truncate the file given by path to length bytes."); |
| 8019 | |
| 8020 | static PyObject * |
| 8021 | posix_truncate(PyObject *self, PyObject *args) |
| 8022 | { |
| 8023 | PyObject *opath; |
| 8024 | const char *path; |
| 8025 | off_t length; |
| 8026 | int res; |
| 8027 | |
| 8028 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 8029 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 8030 | return NULL; |
| 8031 | path = PyBytes_AsString(opath); |
| 8032 | |
| 8033 | Py_BEGIN_ALLOW_THREADS |
| 8034 | res = truncate(path, length); |
| 8035 | Py_END_ALLOW_THREADS |
| 8036 | Py_DECREF(opath); |
| 8037 | if (res < 0) |
| 8038 | return posix_error(); |
| 8039 | Py_RETURN_NONE; |
| 8040 | } |
| 8041 | #endif |
| 8042 | |
| 8043 | #ifdef HAVE_POSIX_FALLOCATE |
| 8044 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 8045 | "posix_fallocate(fd, offset, len)\n\n\ |
| 8046 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 8047 | starting from offset and continuing for len bytes."); |
| 8048 | |
| 8049 | static PyObject * |
| 8050 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 8051 | { |
| 8052 | off_t len, offset; |
| 8053 | int res, fd; |
| 8054 | |
| 8055 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 8056 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 8057 | return NULL; |
| 8058 | |
| 8059 | Py_BEGIN_ALLOW_THREADS |
| 8060 | res = posix_fallocate(fd, offset, len); |
| 8061 | Py_END_ALLOW_THREADS |
| 8062 | if (res != 0) { |
| 8063 | errno = res; |
| 8064 | return posix_error(); |
| 8065 | } |
| 8066 | Py_RETURN_NONE; |
| 8067 | } |
| 8068 | #endif |
| 8069 | |
| 8070 | #ifdef HAVE_POSIX_FADVISE |
| 8071 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 8072 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 8073 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 8074 | the kernel to make optimizations.\n\ |
| 8075 | The advice applies to the region of the file specified by fd starting at\n\ |
| 8076 | offset and continuing for len bytes.\n\ |
| 8077 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 8078 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 8079 | POSIX_FADV_DONTNEED."); |
| 8080 | |
| 8081 | static PyObject * |
| 8082 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 8083 | { |
| 8084 | off_t len, offset; |
| 8085 | int res, fd, advice; |
| 8086 | |
| 8087 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 8088 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 8089 | return NULL; |
| 8090 | |
| 8091 | Py_BEGIN_ALLOW_THREADS |
| 8092 | res = posix_fadvise(fd, offset, len, advice); |
| 8093 | Py_END_ALLOW_THREADS |
| 8094 | if (res != 0) { |
| 8095 | errno = res; |
| 8096 | return posix_error(); |
| 8097 | } |
| 8098 | Py_RETURN_NONE; |
| 8099 | } |
| 8100 | #endif |
| 8101 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8102 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8103 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8104 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8105 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8106 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8107 | /* Save putenv() parameters as values here, so we can collect them when they |
| 8108 | * get re-set with another call for the same key. */ |
| 8109 | static PyObject *posix_putenv_garbage; |
| 8110 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8111 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8112 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8113 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8114 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8115 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8116 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8117 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8118 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8119 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8120 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8121 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8122 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8123 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8124 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8125 | if (newstr == NULL) { |
| 8126 | PyErr_NoMemory(); |
| 8127 | goto error; |
| 8128 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8129 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 8130 | PyErr_Format(PyExc_ValueError, |
| 8131 | "the environment variable is longer than %u characters", |
| 8132 | _MAX_ENV); |
| 8133 | goto error; |
| 8134 | } |
| 8135 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8136 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 8137 | if (newenv == NULL) |
| 8138 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8139 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8140 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8141 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8142 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8143 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8144 | PyObject *os1, *os2; |
| 8145 | char *s1, *s2; |
| 8146 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8147 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8148 | if (!PyArg_ParseTuple(args, |
| 8149 | "O&O&:putenv", |
| 8150 | PyUnicode_FSConverter, &os1, |
| 8151 | PyUnicode_FSConverter, &os2)) |
| 8152 | return NULL; |
| 8153 | s1 = PyBytes_AsString(os1); |
| 8154 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8155 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8156 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 8157 | if (newstr == NULL) { |
| 8158 | PyErr_NoMemory(); |
| 8159 | goto error; |
| 8160 | } |
| 8161 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8162 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8163 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8164 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8165 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8166 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 8167 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8168 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8169 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 8170 | * this will cause previous value to be collected. This has to |
| 8171 | * happen after the real putenv() call because the old value |
| 8172 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8173 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8174 | /* really not much we can do; just leak */ |
| 8175 | PyErr_Clear(); |
| 8176 | } |
| 8177 | else { |
| 8178 | Py_DECREF(newstr); |
| 8179 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8180 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8181 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8182 | Py_DECREF(os1); |
| 8183 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8184 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8185 | Py_RETURN_NONE; |
| 8186 | |
| 8187 | error: |
| 8188 | #ifndef MS_WINDOWS |
| 8189 | Py_DECREF(os1); |
| 8190 | Py_DECREF(os2); |
| 8191 | #endif |
| 8192 | Py_XDECREF(newstr); |
| 8193 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8194 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8195 | #endif /* putenv */ |
| 8196 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8197 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8198 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8199 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8200 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8201 | |
| 8202 | static PyObject * |
| 8203 | posix_unsetenv(PyObject *self, PyObject *args) |
| 8204 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8205 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8206 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8207 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8208 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8209 | |
| 8210 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8211 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8212 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8213 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8214 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8215 | #ifdef HAVE_BROKEN_UNSETENV |
| 8216 | unsetenv(PyBytes_AS_STRING(name)); |
| 8217 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8218 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8219 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 8220 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 8221 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 8222 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 8223 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8224 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8225 | /* Remove the key from posix_putenv_garbage; |
| 8226 | * this will cause it to be collected. This has to |
| 8227 | * happen after the real unsetenv() call because the |
| 8228 | * old value was still accessible until then. |
| 8229 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8230 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8231 | /* really not much we can do; just leak */ |
| 8232 | PyErr_Clear(); |
| 8233 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 8234 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 8235 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8236 | } |
| 8237 | #endif /* unsetenv */ |
| 8238 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8239 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8240 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8241 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8242 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 8243 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8244 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8245 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8246 | int code; |
| 8247 | char *message; |
| 8248 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 8249 | return NULL; |
| 8250 | message = strerror(code); |
| 8251 | if (message == NULL) { |
| 8252 | PyErr_SetString(PyExc_ValueError, |
| 8253 | "strerror() argument out of range"); |
| 8254 | return NULL; |
| 8255 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 8256 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8257 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8258 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8259 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8260 | #ifdef HAVE_SYS_WAIT_H |
| 8261 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8262 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8263 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8264 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8265 | 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] | 8266 | |
| 8267 | static PyObject * |
| 8268 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 8269 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8270 | WAIT_TYPE status; |
| 8271 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8272 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8273 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 8274 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8275 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8276 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8277 | } |
| 8278 | #endif /* WCOREDUMP */ |
| 8279 | |
| 8280 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8281 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8282 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8283 | 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] | 8284 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8285 | |
| 8286 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8287 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8288 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8289 | WAIT_TYPE status; |
| 8290 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8291 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8292 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 8293 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8294 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8295 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8296 | } |
| 8297 | #endif /* WIFCONTINUED */ |
| 8298 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8299 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8300 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8301 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8302 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8303 | |
| 8304 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8305 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8306 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8307 | WAIT_TYPE status; |
| 8308 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8309 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8310 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 8311 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8312 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8313 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8314 | } |
| 8315 | #endif /* WIFSTOPPED */ |
| 8316 | |
| 8317 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8318 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8319 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8320 | 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] | 8321 | |
| 8322 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8323 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8324 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8325 | WAIT_TYPE status; |
| 8326 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8327 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8328 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 8329 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8330 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8331 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8332 | } |
| 8333 | #endif /* WIFSIGNALED */ |
| 8334 | |
| 8335 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8336 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8337 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8338 | 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] | 8339 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8340 | |
| 8341 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8342 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8343 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8344 | WAIT_TYPE status; |
| 8345 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8347 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 8348 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8349 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8350 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8351 | } |
| 8352 | #endif /* WIFEXITED */ |
| 8353 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8354 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8355 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8356 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8357 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8358 | |
| 8359 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8360 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8361 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8362 | WAIT_TYPE status; |
| 8363 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8364 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8365 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8366 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8367 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8368 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8369 | } |
| 8370 | #endif /* WEXITSTATUS */ |
| 8371 | |
| 8372 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8373 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8374 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8375 | 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] | 8376 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8377 | |
| 8378 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8379 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8380 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8381 | WAIT_TYPE status; |
| 8382 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8383 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8384 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8385 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8387 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8388 | } |
| 8389 | #endif /* WTERMSIG */ |
| 8390 | |
| 8391 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8392 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8393 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8394 | Return the signal that stopped the process that provided\n\ |
| 8395 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8396 | |
| 8397 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8398 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8399 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8400 | WAIT_TYPE status; |
| 8401 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8402 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8403 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8404 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8405 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8406 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8407 | } |
| 8408 | #endif /* WSTOPSIG */ |
| 8409 | |
| 8410 | #endif /* HAVE_SYS_WAIT_H */ |
| 8411 | |
| 8412 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8413 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8414 | #ifdef _SCO_DS |
| 8415 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8416 | needed definitions in sys/statvfs.h */ |
| 8417 | #define _SVID3 |
| 8418 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8419 | #include <sys/statvfs.h> |
| 8420 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8421 | static PyObject* |
| 8422 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8423 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8424 | if (v == NULL) |
| 8425 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8426 | |
| 8427 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8428 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8429 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8430 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8431 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8432 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8433 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8434 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8435 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8436 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8437 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8438 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8439 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8440 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8441 | PyStructSequence_SET_ITEM(v, 2, |
| 8442 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8443 | PyStructSequence_SET_ITEM(v, 3, |
| 8444 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8445 | PyStructSequence_SET_ITEM(v, 4, |
| 8446 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8447 | PyStructSequence_SET_ITEM(v, 5, |
| 8448 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8449 | PyStructSequence_SET_ITEM(v, 6, |
| 8450 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8451 | PyStructSequence_SET_ITEM(v, 7, |
| 8452 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8453 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8454 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8455 | #endif |
| 8456 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8457 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8458 | } |
| 8459 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8460 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8461 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8462 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8463 | |
| 8464 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8465 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8466 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8467 | int fd, res; |
| 8468 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8469 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8470 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8471 | return NULL; |
| 8472 | Py_BEGIN_ALLOW_THREADS |
| 8473 | res = fstatvfs(fd, &st); |
| 8474 | Py_END_ALLOW_THREADS |
| 8475 | if (res != 0) |
| 8476 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8477 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8478 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8479 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8480 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8481 | |
| 8482 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8483 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8484 | #include <sys/statvfs.h> |
| 8485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8486 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8487 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8488 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8489 | |
| 8490 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8491 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8492 | { |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8493 | PyObject *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8494 | int res; |
| 8495 | struct statvfs st; |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8496 | if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8497 | return NULL; |
| 8498 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8499 | res = statvfs(PyBytes_AS_STRING(path), &st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8500 | Py_END_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8501 | if (res != 0) { |
| 8502 | posix_error_with_filename(PyBytes_AS_STRING(path)); |
| 8503 | Py_DECREF(path); |
| 8504 | return NULL; |
| 8505 | } |
| 8506 | Py_DECREF(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8508 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8509 | } |
| 8510 | #endif /* HAVE_STATVFS */ |
| 8511 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8512 | #ifdef MS_WINDOWS |
| 8513 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8514 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8515 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8516 | |
| 8517 | static PyObject * |
| 8518 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8519 | { |
| 8520 | BOOL retval; |
| 8521 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8522 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8523 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8524 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8525 | return NULL; |
| 8526 | |
| 8527 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8528 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8529 | Py_END_ALLOW_THREADS |
| 8530 | if (retval == 0) |
| 8531 | return PyErr_SetFromWindowsErr(0); |
| 8532 | |
| 8533 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8534 | } |
| 8535 | #endif |
| 8536 | |
| 8537 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8538 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8539 | * It maps strings representing configuration variable names to |
| 8540 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8541 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8542 | * rarely-used constants. There are three separate tables that use |
| 8543 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8544 | * |
| 8545 | * This code is always included, even if none of the interfaces that |
| 8546 | * need it are included. The #if hackery needed to avoid it would be |
| 8547 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8548 | */ |
| 8549 | struct constdef { |
| 8550 | char *name; |
| 8551 | long value; |
| 8552 | }; |
| 8553 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8554 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8555 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8556 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8557 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8558 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8559 | *valuep = PyLong_AS_LONG(arg); |
| 8560 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8561 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8562 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8563 | /* look up the value in the table using a binary search */ |
| 8564 | size_t lo = 0; |
| 8565 | size_t mid; |
| 8566 | size_t hi = tablesize; |
| 8567 | int cmp; |
| 8568 | const char *confname; |
| 8569 | if (!PyUnicode_Check(arg)) { |
| 8570 | PyErr_SetString(PyExc_TypeError, |
| 8571 | "configuration names must be strings or integers"); |
| 8572 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8573 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8574 | confname = _PyUnicode_AsString(arg); |
| 8575 | if (confname == NULL) |
| 8576 | return 0; |
| 8577 | while (lo < hi) { |
| 8578 | mid = (lo + hi) / 2; |
| 8579 | cmp = strcmp(confname, table[mid].name); |
| 8580 | if (cmp < 0) |
| 8581 | hi = mid; |
| 8582 | else if (cmp > 0) |
| 8583 | lo = mid + 1; |
| 8584 | else { |
| 8585 | *valuep = table[mid].value; |
| 8586 | return 1; |
| 8587 | } |
| 8588 | } |
| 8589 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8590 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8591 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8592 | } |
| 8593 | |
| 8594 | |
| 8595 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8596 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8597 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8598 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8599 | #endif |
| 8600 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8601 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8602 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8603 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8605 | #endif |
| 8606 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8608 | #endif |
| 8609 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8610 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8611 | #endif |
| 8612 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8613 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8614 | #endif |
| 8615 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8616 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8617 | #endif |
| 8618 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8619 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8620 | #endif |
| 8621 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8622 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8623 | #endif |
| 8624 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8625 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8626 | #endif |
| 8627 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8628 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8629 | #endif |
| 8630 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8631 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8632 | #endif |
| 8633 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8634 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8635 | #endif |
| 8636 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8637 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8638 | #endif |
| 8639 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8640 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8641 | #endif |
| 8642 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8643 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8644 | #endif |
| 8645 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8646 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8647 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8648 | #ifdef _PC_ACL_ENABLED |
| 8649 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8650 | #endif |
| 8651 | #ifdef _PC_MIN_HOLE_SIZE |
| 8652 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8653 | #endif |
| 8654 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8655 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8656 | #endif |
| 8657 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8658 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8659 | #endif |
| 8660 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8661 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8662 | #endif |
| 8663 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8664 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8665 | #endif |
| 8666 | #ifdef _PC_REC_XFER_ALIGN |
| 8667 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8668 | #endif |
| 8669 | #ifdef _PC_SYMLINK_MAX |
| 8670 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8671 | #endif |
| 8672 | #ifdef _PC_XATTR_ENABLED |
| 8673 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8674 | #endif |
| 8675 | #ifdef _PC_XATTR_EXISTS |
| 8676 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8677 | #endif |
| 8678 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8679 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8680 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8681 | }; |
| 8682 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8683 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8684 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8685 | { |
| 8686 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8687 | sizeof(posix_constants_pathconf) |
| 8688 | / sizeof(struct constdef)); |
| 8689 | } |
| 8690 | #endif |
| 8691 | |
| 8692 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8693 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8694 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8695 | 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] | 8696 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8697 | |
| 8698 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8699 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8700 | { |
| 8701 | PyObject *result = NULL; |
| 8702 | int name, fd; |
| 8703 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8704 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8705 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8706 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8707 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8708 | errno = 0; |
| 8709 | limit = fpathconf(fd, name); |
| 8710 | if (limit == -1 && errno != 0) |
| 8711 | posix_error(); |
| 8712 | else |
| 8713 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8714 | } |
| 8715 | return result; |
| 8716 | } |
| 8717 | #endif |
| 8718 | |
| 8719 | |
| 8720 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8721 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8722 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8723 | 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] | 8724 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8725 | |
| 8726 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8727 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8728 | { |
| 8729 | PyObject *result = NULL; |
| 8730 | int name; |
| 8731 | char *path; |
| 8732 | |
| 8733 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 8734 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8735 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8736 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8737 | errno = 0; |
| 8738 | limit = pathconf(path, name); |
| 8739 | if (limit == -1 && errno != 0) { |
| 8740 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8741 | /* could be a path or name problem */ |
| 8742 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8743 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8744 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8745 | } |
| 8746 | else |
| 8747 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8748 | } |
| 8749 | return result; |
| 8750 | } |
| 8751 | #endif |
| 8752 | |
| 8753 | #ifdef HAVE_CONFSTR |
| 8754 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8755 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8756 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8757 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8758 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8759 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8760 | #endif |
| 8761 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8762 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8763 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8764 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8765 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8766 | #endif |
| 8767 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8768 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8769 | #endif |
| 8770 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8771 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8772 | #endif |
| 8773 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8774 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8775 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8776 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8777 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8778 | #endif |
| 8779 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8780 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8781 | #endif |
| 8782 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8783 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8784 | #endif |
| 8785 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8786 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8787 | #endif |
| 8788 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8789 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8790 | #endif |
| 8791 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8792 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8793 | #endif |
| 8794 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8795 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8796 | #endif |
| 8797 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8798 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8799 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8800 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8801 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8802 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8803 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8804 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8805 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8806 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8807 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8808 | #endif |
| 8809 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8810 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8811 | #endif |
| 8812 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8813 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8814 | #endif |
| 8815 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8816 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8817 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8818 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8819 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8820 | #endif |
| 8821 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8822 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8823 | #endif |
| 8824 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8825 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8826 | #endif |
| 8827 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8828 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8829 | #endif |
| 8830 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8831 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8832 | #endif |
| 8833 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8834 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8835 | #endif |
| 8836 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8837 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8838 | #endif |
| 8839 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8840 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8841 | #endif |
| 8842 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8843 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8844 | #endif |
| 8845 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8846 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8847 | #endif |
| 8848 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8849 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8850 | #endif |
| 8851 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8852 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8853 | #endif |
| 8854 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8855 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8856 | #endif |
| 8857 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8858 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8859 | #endif |
| 8860 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8861 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8862 | #endif |
| 8863 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8864 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8865 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8866 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8867 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8868 | #endif |
| 8869 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8870 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8871 | #endif |
| 8872 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8873 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8874 | #endif |
| 8875 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8876 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8877 | #endif |
| 8878 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8879 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8880 | #endif |
| 8881 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8882 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8883 | #endif |
| 8884 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8885 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8886 | #endif |
| 8887 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8888 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8889 | #endif |
| 8890 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8891 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8892 | #endif |
| 8893 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8894 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8895 | #endif |
| 8896 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8897 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8898 | #endif |
| 8899 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8900 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8901 | #endif |
| 8902 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8903 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8904 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8905 | }; |
| 8906 | |
| 8907 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8908 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8909 | { |
| 8910 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8911 | sizeof(posix_constants_confstr) |
| 8912 | / sizeof(struct constdef)); |
| 8913 | } |
| 8914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8915 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8916 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8917 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8918 | |
| 8919 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8920 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8921 | { |
| 8922 | PyObject *result = NULL; |
| 8923 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8924 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8925 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8926 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8927 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8928 | return NULL; |
| 8929 | |
| 8930 | errno = 0; |
| 8931 | len = confstr(name, buffer, sizeof(buffer)); |
| 8932 | if (len == 0) { |
| 8933 | if (errno) { |
| 8934 | posix_error(); |
| 8935 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8936 | } |
| 8937 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8938 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8939 | } |
| 8940 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8941 | |
| 8942 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8943 | char *buf = PyMem_Malloc(len); |
| 8944 | if (buf == NULL) |
| 8945 | return PyErr_NoMemory(); |
| 8946 | confstr(name, buf, len); |
| 8947 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8948 | PyMem_Free(buf); |
| 8949 | } |
| 8950 | else |
| 8951 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8952 | return result; |
| 8953 | } |
| 8954 | #endif |
| 8955 | |
| 8956 | |
| 8957 | #ifdef HAVE_SYSCONF |
| 8958 | static struct constdef posix_constants_sysconf[] = { |
| 8959 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8960 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8961 | #endif |
| 8962 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8963 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8964 | #endif |
| 8965 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8966 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8967 | #endif |
| 8968 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8969 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8970 | #endif |
| 8971 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8972 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8973 | #endif |
| 8974 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8975 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8976 | #endif |
| 8977 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8978 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8979 | #endif |
| 8980 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8981 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8982 | #endif |
| 8983 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8984 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8985 | #endif |
| 8986 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8987 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8988 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8989 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8990 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8991 | #endif |
| 8992 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8993 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8994 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8995 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8996 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8997 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8998 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8999 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9000 | #endif |
| 9001 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9002 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9003 | #endif |
| 9004 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9005 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9006 | #endif |
| 9007 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9008 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9009 | #endif |
| 9010 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9011 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9012 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9013 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9014 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9015 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9016 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9017 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9018 | #endif |
| 9019 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9020 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9021 | #endif |
| 9022 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9023 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9024 | #endif |
| 9025 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9026 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9027 | #endif |
| 9028 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9029 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9030 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9031 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9032 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9033 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9034 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9035 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9036 | #endif |
| 9037 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9038 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9039 | #endif |
| 9040 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9041 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9042 | #endif |
| 9043 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9044 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9045 | #endif |
| 9046 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9047 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9048 | #endif |
| 9049 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9050 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9051 | #endif |
| 9052 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9053 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9054 | #endif |
| 9055 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9056 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9057 | #endif |
| 9058 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9059 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9060 | #endif |
| 9061 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9062 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9063 | #endif |
| 9064 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9065 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9066 | #endif |
| 9067 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9068 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9069 | #endif |
| 9070 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9071 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9072 | #endif |
| 9073 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9074 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9075 | #endif |
| 9076 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9077 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9078 | #endif |
| 9079 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9080 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9081 | #endif |
| 9082 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9084 | #endif |
| 9085 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9086 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9087 | #endif |
| 9088 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9089 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9090 | #endif |
| 9091 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9092 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9093 | #endif |
| 9094 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9095 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9096 | #endif |
| 9097 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9098 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9099 | #endif |
| 9100 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9101 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9102 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9103 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9104 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9105 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9106 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9107 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9108 | #endif |
| 9109 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9110 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9111 | #endif |
| 9112 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9113 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9114 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9115 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9116 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9117 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9118 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9119 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9120 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9121 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9123 | #endif |
| 9124 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9125 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9126 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9127 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9128 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9129 | #endif |
| 9130 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9131 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9132 | #endif |
| 9133 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9134 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9135 | #endif |
| 9136 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9137 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9138 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9139 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9140 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9141 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9142 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9143 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9144 | #endif |
| 9145 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9146 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9147 | #endif |
| 9148 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9149 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9150 | #endif |
| 9151 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9152 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9153 | #endif |
| 9154 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9155 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9156 | #endif |
| 9157 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9158 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9159 | #endif |
| 9160 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9161 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9162 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9163 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9164 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9165 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9166 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9167 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9168 | #endif |
| 9169 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9170 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9171 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9172 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9173 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9174 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9175 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9176 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9177 | #endif |
| 9178 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9179 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9180 | #endif |
| 9181 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9182 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9183 | #endif |
| 9184 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9185 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9186 | #endif |
| 9187 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9188 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9189 | #endif |
| 9190 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9191 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9192 | #endif |
| 9193 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9194 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9195 | #endif |
| 9196 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9197 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9198 | #endif |
| 9199 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9200 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9201 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9202 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9203 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9204 | #endif |
| 9205 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9206 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9207 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9208 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9209 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9210 | #endif |
| 9211 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9212 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9213 | #endif |
| 9214 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9215 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9216 | #endif |
| 9217 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9218 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9219 | #endif |
| 9220 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9221 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9222 | #endif |
| 9223 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9224 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9225 | #endif |
| 9226 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9227 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9228 | #endif |
| 9229 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9230 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9231 | #endif |
| 9232 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9233 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9234 | #endif |
| 9235 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9236 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9237 | #endif |
| 9238 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9239 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9240 | #endif |
| 9241 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9242 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9243 | #endif |
| 9244 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9245 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9246 | #endif |
| 9247 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9248 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9249 | #endif |
| 9250 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9251 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9252 | #endif |
| 9253 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9254 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9255 | #endif |
| 9256 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9258 | #endif |
| 9259 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9260 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9261 | #endif |
| 9262 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9263 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9264 | #endif |
| 9265 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9266 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9267 | #endif |
| 9268 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9269 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9270 | #endif |
| 9271 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9272 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9273 | #endif |
| 9274 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9275 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9276 | #endif |
| 9277 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9279 | #endif |
| 9280 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9281 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9282 | #endif |
| 9283 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9284 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9285 | #endif |
| 9286 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9287 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9288 | #endif |
| 9289 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9290 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9291 | #endif |
| 9292 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9293 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9294 | #endif |
| 9295 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9296 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9297 | #endif |
| 9298 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9299 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9300 | #endif |
| 9301 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9302 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9303 | #endif |
| 9304 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9305 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9306 | #endif |
| 9307 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9308 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9309 | #endif |
| 9310 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9311 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9312 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9313 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9314 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9315 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9316 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9317 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9318 | #endif |
| 9319 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9320 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9321 | #endif |
| 9322 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9323 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9324 | #endif |
| 9325 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9326 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9327 | #endif |
| 9328 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9329 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9330 | #endif |
| 9331 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9332 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9333 | #endif |
| 9334 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9335 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9336 | #endif |
| 9337 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9338 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9339 | #endif |
| 9340 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9341 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9342 | #endif |
| 9343 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9344 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9345 | #endif |
| 9346 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9347 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9348 | #endif |
| 9349 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9350 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9351 | #endif |
| 9352 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9353 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9354 | #endif |
| 9355 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9356 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9357 | #endif |
| 9358 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9359 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9360 | #endif |
| 9361 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9362 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9363 | #endif |
| 9364 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9365 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9366 | #endif |
| 9367 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9368 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9369 | #endif |
| 9370 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9371 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9372 | #endif |
| 9373 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9375 | #endif |
| 9376 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9377 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9378 | #endif |
| 9379 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9380 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9381 | #endif |
| 9382 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9383 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9384 | #endif |
| 9385 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9386 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9387 | #endif |
| 9388 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9389 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9390 | #endif |
| 9391 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9392 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9393 | #endif |
| 9394 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9395 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9396 | #endif |
| 9397 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9398 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9399 | #endif |
| 9400 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9401 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9402 | #endif |
| 9403 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9404 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9405 | #endif |
| 9406 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9407 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9408 | #endif |
| 9409 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9410 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9411 | #endif |
| 9412 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9413 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9414 | #endif |
| 9415 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9416 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9417 | #endif |
| 9418 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9419 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9420 | #endif |
| 9421 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9422 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9423 | #endif |
| 9424 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9425 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9426 | #endif |
| 9427 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9428 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9429 | #endif |
| 9430 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9431 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9432 | #endif |
| 9433 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9434 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9435 | #endif |
| 9436 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9437 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9438 | #endif |
| 9439 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9440 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9441 | #endif |
| 9442 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9443 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9444 | #endif |
| 9445 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9446 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9447 | #endif |
| 9448 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9449 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9450 | #endif |
| 9451 | }; |
| 9452 | |
| 9453 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9454 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9455 | { |
| 9456 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9457 | sizeof(posix_constants_sysconf) |
| 9458 | / sizeof(struct constdef)); |
| 9459 | } |
| 9460 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9461 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9462 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9463 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9464 | |
| 9465 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9466 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9467 | { |
| 9468 | PyObject *result = NULL; |
| 9469 | int name; |
| 9470 | |
| 9471 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9472 | int value; |
| 9473 | |
| 9474 | errno = 0; |
| 9475 | value = sysconf(name); |
| 9476 | if (value == -1 && errno != 0) |
| 9477 | posix_error(); |
| 9478 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9479 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9480 | } |
| 9481 | return result; |
| 9482 | } |
| 9483 | #endif |
| 9484 | |
| 9485 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9486 | /* This code is used to ensure that the tables of configuration value names |
| 9487 | * are in sorted order as required by conv_confname(), and also to build the |
| 9488 | * the exported dictionaries that are used to publish information about the |
| 9489 | * names available on the host platform. |
| 9490 | * |
| 9491 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9492 | * when used, even for platforms we're not able to test on. It also makes |
| 9493 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9494 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9495 | |
| 9496 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9497 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9498 | { |
| 9499 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9500 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9501 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9502 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9503 | |
| 9504 | return strcmp(c1->name, c2->name); |
| 9505 | } |
| 9506 | |
| 9507 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9508 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9509 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9510 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9511 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9512 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9513 | |
| 9514 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9515 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9516 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9517 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9518 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9519 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9520 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9521 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9522 | Py_XDECREF(o); |
| 9523 | Py_DECREF(d); |
| 9524 | return -1; |
| 9525 | } |
| 9526 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9527 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9528 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9529 | } |
| 9530 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9531 | /* Return -1 on failure, 0 on success. */ |
| 9532 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9533 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9534 | { |
| 9535 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9536 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9537 | sizeof(posix_constants_pathconf) |
| 9538 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9539 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9540 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9541 | #endif |
| 9542 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9543 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9544 | sizeof(posix_constants_confstr) |
| 9545 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9546 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9547 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9548 | #endif |
| 9549 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9550 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9551 | sizeof(posix_constants_sysconf) |
| 9552 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9553 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9554 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9555 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9556 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9557 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9558 | |
| 9559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9560 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9561 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9562 | 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] | 9563 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9564 | |
| 9565 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9566 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9567 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9568 | abort(); |
| 9569 | /*NOTREACHED*/ |
| 9570 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9571 | return NULL; |
| 9572 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9573 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9574 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9575 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9576 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9577 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9578 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9579 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9580 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9581 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9582 | application (if any) its extension is associated.\n\ |
| 9583 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9584 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9585 | \n\ |
| 9586 | startfile returns as soon as the associated application is launched.\n\ |
| 9587 | There is no option to wait for the application to close, and no way\n\ |
| 9588 | to retrieve the application's exit status.\n\ |
| 9589 | \n\ |
| 9590 | The filepath is relative to the current directory. If you want to use\n\ |
| 9591 | 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] | 9592 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9593 | |
| 9594 | static PyObject * |
| 9595 | win32_startfile(PyObject *self, PyObject *args) |
| 9596 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9597 | PyObject *ofilepath; |
| 9598 | char *filepath; |
| 9599 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9600 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9601 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9602 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9603 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9604 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9605 | &unipath, &operation)) { |
| 9606 | PyErr_Clear(); |
| 9607 | goto normal; |
| 9608 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9609 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9610 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9611 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9612 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9613 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9614 | PyErr_Clear(); |
| 9615 | operation = NULL; |
| 9616 | goto normal; |
| 9617 | } |
| 9618 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9619 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9620 | wpath = PyUnicode_AsUnicode(unipath); |
| 9621 | if (wpath == NULL) |
| 9622 | goto normal; |
| 9623 | if (uoperation) { |
| 9624 | woperation = PyUnicode_AsUnicode(uoperation); |
| 9625 | if (woperation == NULL) |
| 9626 | goto normal; |
| 9627 | } |
| 9628 | else |
| 9629 | woperation = NULL; |
| 9630 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9631 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9632 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 9633 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9634 | Py_END_ALLOW_THREADS |
| 9635 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9636 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9637 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9638 | win32_error_object("startfile", unipath); |
| 9639 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9640 | } |
| 9641 | Py_INCREF(Py_None); |
| 9642 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9643 | |
| 9644 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9645 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9646 | PyUnicode_FSConverter, &ofilepath, |
| 9647 | &operation)) |
| 9648 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 9649 | if (win32_warn_bytes_api()) { |
| 9650 | Py_DECREF(ofilepath); |
| 9651 | return NULL; |
| 9652 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9653 | filepath = PyBytes_AsString(ofilepath); |
| 9654 | Py_BEGIN_ALLOW_THREADS |
| 9655 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9656 | NULL, NULL, SW_SHOWNORMAL); |
| 9657 | Py_END_ALLOW_THREADS |
| 9658 | if (rc <= (HINSTANCE)32) { |
| 9659 | PyObject *errval = win32_error("startfile", filepath); |
| 9660 | Py_DECREF(ofilepath); |
| 9661 | return errval; |
| 9662 | } |
| 9663 | Py_DECREF(ofilepath); |
| 9664 | Py_INCREF(Py_None); |
| 9665 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9666 | } |
| 9667 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9668 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9669 | #ifdef HAVE_GETLOADAVG |
| 9670 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9671 | "getloadavg() -> (float, float, float)\n\n\ |
| 9672 | Return the number of processes in the system run queue averaged over\n\ |
| 9673 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9674 | was unobtainable"); |
| 9675 | |
| 9676 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9677 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9678 | { |
| 9679 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9680 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9681 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9682 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9683 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9684 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9685 | } |
| 9686 | #endif |
| 9687 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9688 | PyDoc_STRVAR(device_encoding__doc__, |
| 9689 | "device_encoding(fd) -> str\n\n\ |
| 9690 | Return a string describing the encoding of the device\n\ |
| 9691 | if the output is a terminal; else return None."); |
| 9692 | |
| 9693 | static PyObject * |
| 9694 | device_encoding(PyObject *self, PyObject *args) |
| 9695 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9696 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9697 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9698 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9699 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9700 | |
| 9701 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9702 | } |
| 9703 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9704 | #ifdef __VMS |
| 9705 | /* Use openssl random routine */ |
| 9706 | #include <openssl/rand.h> |
| 9707 | PyDoc_STRVAR(vms_urandom__doc__, |
| 9708 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9709 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9710 | |
| 9711 | static PyObject* |
| 9712 | vms_urandom(PyObject *self, PyObject *args) |
| 9713 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9714 | int howMany; |
| 9715 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9716 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9717 | /* Read arguments */ |
| 9718 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9719 | return NULL; |
| 9720 | if (howMany < 0) |
| 9721 | return PyErr_Format(PyExc_ValueError, |
| 9722 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9723 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9724 | /* Allocate bytes */ |
| 9725 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9726 | if (result != NULL) { |
| 9727 | /* Get random data */ |
| 9728 | if (RAND_pseudo_bytes((unsigned char*) |
| 9729 | PyBytes_AS_STRING(result), |
| 9730 | howMany) < 0) { |
| 9731 | Py_DECREF(result); |
| 9732 | return PyErr_Format(PyExc_ValueError, |
| 9733 | "RAND_pseudo_bytes"); |
| 9734 | } |
| 9735 | } |
| 9736 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9737 | } |
| 9738 | #endif |
| 9739 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9740 | #ifdef HAVE_SETRESUID |
| 9741 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9742 | "setresuid(ruid, euid, suid)\n\n\ |
| 9743 | Set the current process's real, effective, and saved user ids."); |
| 9744 | |
| 9745 | static PyObject* |
| 9746 | posix_setresuid (PyObject *self, PyObject *args) |
| 9747 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9748 | /* We assume uid_t is no larger than a long. */ |
| 9749 | long ruid, euid, suid; |
| 9750 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9751 | return NULL; |
| 9752 | if (setresuid(ruid, euid, suid) < 0) |
| 9753 | return posix_error(); |
| 9754 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9755 | } |
| 9756 | #endif |
| 9757 | |
| 9758 | #ifdef HAVE_SETRESGID |
| 9759 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9760 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9761 | Set the current process's real, effective, and saved group ids."); |
| 9762 | |
| 9763 | static PyObject* |
| 9764 | posix_setresgid (PyObject *self, PyObject *args) |
| 9765 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9766 | /* We assume uid_t is no larger than a long. */ |
| 9767 | long rgid, egid, sgid; |
| 9768 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9769 | return NULL; |
| 9770 | if (setresgid(rgid, egid, sgid) < 0) |
| 9771 | return posix_error(); |
| 9772 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9773 | } |
| 9774 | #endif |
| 9775 | |
| 9776 | #ifdef HAVE_GETRESUID |
| 9777 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9778 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9779 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9780 | |
| 9781 | static PyObject* |
| 9782 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9783 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9784 | uid_t ruid, euid, suid; |
| 9785 | long l_ruid, l_euid, l_suid; |
| 9786 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9787 | return posix_error(); |
| 9788 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9789 | l_ruid = ruid; |
| 9790 | l_euid = euid; |
| 9791 | l_suid = suid; |
| 9792 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9793 | } |
| 9794 | #endif |
| 9795 | |
| 9796 | #ifdef HAVE_GETRESGID |
| 9797 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9798 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9799 | 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] | 9800 | |
| 9801 | static PyObject* |
| 9802 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9803 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9804 | uid_t rgid, egid, sgid; |
| 9805 | long l_rgid, l_egid, l_sgid; |
| 9806 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9807 | return posix_error(); |
| 9808 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9809 | l_rgid = rgid; |
| 9810 | l_egid = egid; |
| 9811 | l_sgid = sgid; |
| 9812 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9813 | } |
| 9814 | #endif |
| 9815 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9816 | /* Posix *at family of functions: |
| 9817 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 9818 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 9819 | unlinkat, utimensat, mkfifoat */ |
| 9820 | |
| 9821 | #ifdef HAVE_FACCESSAT |
| 9822 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 9823 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 9824 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9825 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 9826 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 9827 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9828 | is interpreted relative to the current working directory."); |
| 9829 | |
| 9830 | static PyObject * |
| 9831 | posix_faccessat(PyObject *self, PyObject *args) |
| 9832 | { |
| 9833 | PyObject *opath; |
| 9834 | char *path; |
| 9835 | int mode; |
| 9836 | int res; |
| 9837 | int dirfd, flags = 0; |
| 9838 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 9839 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9840 | return NULL; |
| 9841 | path = PyBytes_AsString(opath); |
| 9842 | Py_BEGIN_ALLOW_THREADS |
| 9843 | res = faccessat(dirfd, path, mode, flags); |
| 9844 | Py_END_ALLOW_THREADS |
| 9845 | Py_DECREF(opath); |
| 9846 | return PyBool_FromLong(res == 0); |
| 9847 | } |
| 9848 | #endif |
| 9849 | |
| 9850 | #ifdef HAVE_FCHMODAT |
| 9851 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 9852 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 9853 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9854 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9855 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9856 | is interpreted relative to the current working directory."); |
| 9857 | |
| 9858 | static PyObject * |
| 9859 | posix_fchmodat(PyObject *self, PyObject *args) |
| 9860 | { |
| 9861 | int dirfd, mode, res; |
| 9862 | int flags = 0; |
| 9863 | PyObject *opath; |
| 9864 | char *path; |
| 9865 | |
| 9866 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 9867 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9868 | return NULL; |
| 9869 | |
| 9870 | path = PyBytes_AsString(opath); |
| 9871 | |
| 9872 | Py_BEGIN_ALLOW_THREADS |
| 9873 | res = fchmodat(dirfd, path, mode, flags); |
| 9874 | Py_END_ALLOW_THREADS |
| 9875 | Py_DECREF(opath); |
| 9876 | if (res < 0) |
| 9877 | return posix_error(); |
| 9878 | Py_RETURN_NONE; |
| 9879 | } |
| 9880 | #endif /* HAVE_FCHMODAT */ |
| 9881 | |
| 9882 | #ifdef HAVE_FCHOWNAT |
| 9883 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 9884 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 9885 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9886 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9887 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9888 | is interpreted relative to the current working directory."); |
| 9889 | |
| 9890 | static PyObject * |
| 9891 | posix_fchownat(PyObject *self, PyObject *args) |
| 9892 | { |
| 9893 | PyObject *opath; |
| 9894 | int dirfd, res; |
| 9895 | long uid, gid; |
| 9896 | int flags = 0; |
| 9897 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9898 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9899 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 9900 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 9901 | return NULL; |
| 9902 | |
| 9903 | path = PyBytes_AsString(opath); |
| 9904 | |
| 9905 | Py_BEGIN_ALLOW_THREADS |
| 9906 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 9907 | Py_END_ALLOW_THREADS |
| 9908 | Py_DECREF(opath); |
| 9909 | if (res < 0) |
| 9910 | return posix_error(); |
| 9911 | Py_RETURN_NONE; |
| 9912 | } |
| 9913 | #endif /* HAVE_FCHOWNAT */ |
| 9914 | |
| 9915 | #ifdef HAVE_FSTATAT |
| 9916 | PyDoc_STRVAR(posix_fstatat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9917 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9918 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9919 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9920 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9921 | is interpreted relative to the current working directory."); |
| 9922 | |
| 9923 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9924 | posix_fstatat(PyObject *self, PyObject *args) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9925 | { |
| 9926 | PyObject *opath; |
| 9927 | char *path; |
| 9928 | STRUCT_STAT st; |
| 9929 | int dirfd, res, flags = 0; |
| 9930 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9931 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 9932 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9933 | return NULL; |
| 9934 | path = PyBytes_AsString(opath); |
| 9935 | |
| 9936 | Py_BEGIN_ALLOW_THREADS |
| 9937 | res = fstatat(dirfd, path, &st, flags); |
| 9938 | Py_END_ALLOW_THREADS |
| 9939 | Py_DECREF(opath); |
| 9940 | if (res != 0) |
| 9941 | return posix_error(); |
| 9942 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9943 | return _pystat_fromstructstat(&st); |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9944 | } |
| 9945 | #endif |
| 9946 | |
| 9947 | #ifdef HAVE_FUTIMESAT |
| 9948 | PyDoc_STRVAR(posix_futimesat__doc__, |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9949 | "futimesat(dirfd, path[, (atime, mtime)])\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9950 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9951 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9952 | is interpreted relative to the current working directory."); |
| 9953 | |
| 9954 | static PyObject * |
| 9955 | posix_futimesat(PyObject *self, PyObject *args) |
| 9956 | { |
| 9957 | PyObject *opath; |
| 9958 | char *path; |
| 9959 | int res, dirfd; |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9960 | PyObject* arg = Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9961 | time_t atime, mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9962 | long ansec, mnsec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9963 | |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9964 | if (!PyArg_ParseTuple(args, "iO&|O:futimesat", |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9965 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 9966 | return NULL; |
| 9967 | path = PyBytes_AsString(opath); |
| 9968 | if (arg == Py_None) { |
| 9969 | /* optional time values not given */ |
| 9970 | Py_BEGIN_ALLOW_THREADS |
| 9971 | res = futimesat(dirfd, path, NULL); |
| 9972 | Py_END_ALLOW_THREADS |
| 9973 | } |
| 9974 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 9975 | PyErr_SetString(PyExc_TypeError, |
| 9976 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 9977 | Py_DECREF(opath); |
| 9978 | return NULL; |
| 9979 | } |
| 9980 | else { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 9981 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 0), |
| 9982 | &atime, &ansec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9983 | Py_DECREF(opath); |
| 9984 | return NULL; |
| 9985 | } |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 9986 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 1), |
| 9987 | &mtime, &mnsec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9988 | Py_DECREF(opath); |
| 9989 | return NULL; |
| 9990 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9991 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9992 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9993 | { |
| 9994 | #ifdef HAVE_UTIMENSAT |
| 9995 | struct timespec buf[2]; |
| 9996 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9997 | buf[0].tv_nsec = ansec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9998 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9999 | buf[1].tv_nsec = mnsec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 10000 | res = utimensat(dirfd, path, buf, 0); |
| 10001 | #else |
| 10002 | struct timeval buf[2]; |
| 10003 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 10004 | buf[0].tv_usec = ansec / 1000; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 10005 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 10006 | buf[1].tv_usec = mnsec / 1000; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10007 | res = futimesat(dirfd, path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 10008 | #endif |
| 10009 | } |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10010 | Py_END_ALLOW_THREADS |
| 10011 | } |
| 10012 | Py_DECREF(opath); |
| 10013 | if (res < 0) { |
| 10014 | return posix_error(); |
| 10015 | } |
| 10016 | Py_RETURN_NONE; |
| 10017 | } |
| 10018 | #endif |
| 10019 | |
| 10020 | #ifdef HAVE_LINKAT |
| 10021 | PyDoc_STRVAR(posix_linkat__doc__, |
| 10022 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 10023 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 10024 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 10025 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 10026 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 10027 | srcpath is interpreted relative to the current working directory. This\n\ |
| 10028 | also applies for dstpath."); |
| 10029 | |
| 10030 | static PyObject * |
| 10031 | posix_linkat(PyObject *self, PyObject *args) |
| 10032 | { |
| 10033 | PyObject *osrc, *odst; |
| 10034 | char *src, *dst; |
| 10035 | int res, srcfd, dstfd; |
| 10036 | int flags = 0; |
| 10037 | |
| 10038 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 10039 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 10040 | return NULL; |
| 10041 | src = PyBytes_AsString(osrc); |
| 10042 | dst = PyBytes_AsString(odst); |
| 10043 | Py_BEGIN_ALLOW_THREADS |
| 10044 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 10045 | Py_END_ALLOW_THREADS |
| 10046 | Py_DECREF(osrc); |
| 10047 | Py_DECREF(odst); |
| 10048 | if (res < 0) |
| 10049 | return posix_error(); |
| 10050 | Py_RETURN_NONE; |
| 10051 | } |
| 10052 | #endif /* HAVE_LINKAT */ |
| 10053 | |
| 10054 | #ifdef HAVE_MKDIRAT |
| 10055 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 10056 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 10057 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10058 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10059 | is interpreted relative to the current working directory."); |
| 10060 | |
| 10061 | static PyObject * |
| 10062 | posix_mkdirat(PyObject *self, PyObject *args) |
| 10063 | { |
| 10064 | int res, dirfd; |
| 10065 | PyObject *opath; |
| 10066 | char *path; |
| 10067 | int mode = 0777; |
| 10068 | |
| 10069 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 10070 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 10071 | return NULL; |
| 10072 | path = PyBytes_AsString(opath); |
| 10073 | Py_BEGIN_ALLOW_THREADS |
| 10074 | res = mkdirat(dirfd, path, mode); |
| 10075 | Py_END_ALLOW_THREADS |
| 10076 | Py_DECREF(opath); |
| 10077 | if (res < 0) |
| 10078 | return posix_error(); |
| 10079 | Py_RETURN_NONE; |
| 10080 | } |
| 10081 | #endif |
| 10082 | |
| 10083 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 10084 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 10085 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 10086 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10087 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10088 | is interpreted relative to the current working directory."); |
| 10089 | |
| 10090 | static PyObject * |
| 10091 | posix_mknodat(PyObject *self, PyObject *args) |
| 10092 | { |
| 10093 | PyObject *opath; |
| 10094 | char *filename; |
| 10095 | int mode = 0600; |
| 10096 | int device = 0; |
| 10097 | int res, dirfd; |
| 10098 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 10099 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 10100 | return NULL; |
| 10101 | filename = PyBytes_AS_STRING(opath); |
| 10102 | Py_BEGIN_ALLOW_THREADS |
| 10103 | res = mknodat(dirfd, filename, mode, device); |
| 10104 | Py_END_ALLOW_THREADS |
| 10105 | Py_DECREF(opath); |
| 10106 | if (res < 0) |
| 10107 | return posix_error(); |
| 10108 | Py_RETURN_NONE; |
| 10109 | } |
| 10110 | #endif |
| 10111 | |
| 10112 | #ifdef HAVE_OPENAT |
| 10113 | PyDoc_STRVAR(posix_openat__doc__, |
| 10114 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 10115 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10116 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10117 | is interpreted relative to the current working directory."); |
| 10118 | |
| 10119 | static PyObject * |
| 10120 | posix_openat(PyObject *self, PyObject *args) |
| 10121 | { |
| 10122 | PyObject *ofile; |
| 10123 | char *file; |
| 10124 | int flag, dirfd, fd; |
| 10125 | int mode = 0777; |
| 10126 | |
| 10127 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 10128 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 10129 | &flag, &mode)) |
| 10130 | return NULL; |
| 10131 | file = PyBytes_AsString(ofile); |
| 10132 | Py_BEGIN_ALLOW_THREADS |
| 10133 | fd = openat(dirfd, file, flag, mode); |
| 10134 | Py_END_ALLOW_THREADS |
| 10135 | Py_DECREF(ofile); |
| 10136 | if (fd < 0) |
| 10137 | return posix_error(); |
| 10138 | return PyLong_FromLong((long)fd); |
| 10139 | } |
| 10140 | #endif |
| 10141 | |
| 10142 | #ifdef HAVE_READLINKAT |
| 10143 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 10144 | "readlinkat(dirfd, path) -> path\n\n\ |
| 10145 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10146 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10147 | is interpreted relative to the current working directory."); |
| 10148 | |
| 10149 | static PyObject * |
| 10150 | posix_readlinkat(PyObject *self, PyObject *args) |
| 10151 | { |
| 10152 | PyObject *v, *opath; |
| 10153 | char buf[MAXPATHLEN]; |
| 10154 | char *path; |
| 10155 | int n, dirfd; |
| 10156 | int arg_is_unicode = 0; |
| 10157 | |
| 10158 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 10159 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 10160 | return NULL; |
| 10161 | path = PyBytes_AsString(opath); |
| 10162 | v = PySequence_GetItem(args, 1); |
| 10163 | if (v == NULL) { |
| 10164 | Py_DECREF(opath); |
| 10165 | return NULL; |
| 10166 | } |
| 10167 | |
| 10168 | if (PyUnicode_Check(v)) { |
| 10169 | arg_is_unicode = 1; |
| 10170 | } |
| 10171 | Py_DECREF(v); |
| 10172 | |
| 10173 | Py_BEGIN_ALLOW_THREADS |
| 10174 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 10175 | Py_END_ALLOW_THREADS |
| 10176 | Py_DECREF(opath); |
| 10177 | if (n < 0) |
| 10178 | return posix_error(); |
| 10179 | |
| 10180 | if (arg_is_unicode) |
| 10181 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 10182 | else |
| 10183 | return PyBytes_FromStringAndSize(buf, n); |
| 10184 | } |
| 10185 | #endif /* HAVE_READLINKAT */ |
| 10186 | |
| 10187 | #ifdef HAVE_RENAMEAT |
| 10188 | PyDoc_STRVAR(posix_renameat__doc__, |
| 10189 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 10190 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 10191 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 10192 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 10193 | oldpath is interpreted relative to the current working directory. This\n\ |
| 10194 | also applies for newpath."); |
| 10195 | |
| 10196 | static PyObject * |
| 10197 | posix_renameat(PyObject *self, PyObject *args) |
| 10198 | { |
| 10199 | int res; |
| 10200 | PyObject *opathold, *opathnew; |
| 10201 | char *opath, *npath; |
| 10202 | int oldfd, newfd; |
| 10203 | |
| 10204 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 10205 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 10206 | return NULL; |
| 10207 | opath = PyBytes_AsString(opathold); |
| 10208 | npath = PyBytes_AsString(opathnew); |
| 10209 | Py_BEGIN_ALLOW_THREADS |
| 10210 | res = renameat(oldfd, opath, newfd, npath); |
| 10211 | Py_END_ALLOW_THREADS |
| 10212 | Py_DECREF(opathold); |
| 10213 | Py_DECREF(opathnew); |
| 10214 | if (res < 0) |
| 10215 | return posix_error(); |
| 10216 | Py_RETURN_NONE; |
| 10217 | } |
| 10218 | #endif |
| 10219 | |
| 10220 | #if HAVE_SYMLINKAT |
| 10221 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 10222 | "symlinkat(src, dstfd, dst)\n\n\ |
| 10223 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 10224 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 10225 | is interpreted relative to the current working directory."); |
| 10226 | |
| 10227 | static PyObject * |
| 10228 | posix_symlinkat(PyObject *self, PyObject *args) |
| 10229 | { |
| 10230 | int res, dstfd; |
| 10231 | PyObject *osrc, *odst; |
| 10232 | char *src, *dst; |
| 10233 | |
| 10234 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 10235 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 10236 | return NULL; |
| 10237 | src = PyBytes_AsString(osrc); |
| 10238 | dst = PyBytes_AsString(odst); |
| 10239 | Py_BEGIN_ALLOW_THREADS |
| 10240 | res = symlinkat(src, dstfd, dst); |
| 10241 | Py_END_ALLOW_THREADS |
| 10242 | Py_DECREF(osrc); |
| 10243 | Py_DECREF(odst); |
| 10244 | if (res < 0) |
| 10245 | return posix_error(); |
| 10246 | Py_RETURN_NONE; |
| 10247 | } |
| 10248 | #endif /* HAVE_SYMLINKAT */ |
| 10249 | |
| 10250 | #ifdef HAVE_UNLINKAT |
| 10251 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 10252 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 10253 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10254 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 10255 | specified, unlinkat() behaves like rmdir().\n\ |
| 10256 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10257 | is interpreted relative to the current working directory."); |
| 10258 | |
| 10259 | static PyObject * |
| 10260 | posix_unlinkat(PyObject *self, PyObject *args) |
| 10261 | { |
| 10262 | int dirfd, res, flags = 0; |
| 10263 | PyObject *opath; |
| 10264 | char *path; |
| 10265 | |
| 10266 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 10267 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 10268 | return NULL; |
| 10269 | path = PyBytes_AsString(opath); |
| 10270 | Py_BEGIN_ALLOW_THREADS |
| 10271 | res = unlinkat(dirfd, path, flags); |
| 10272 | Py_END_ALLOW_THREADS |
| 10273 | Py_DECREF(opath); |
| 10274 | if (res < 0) |
| 10275 | return posix_error(); |
| 10276 | Py_RETURN_NONE; |
| 10277 | } |
| 10278 | #endif |
| 10279 | |
| 10280 | #ifdef HAVE_UTIMENSAT |
| 10281 | PyDoc_STRVAR(posix_utimensat__doc__, |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10282 | "utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\ |
| 10283 | mtime=(mtime_sec, mtime_nsec), flags=0])\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10284 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 10285 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 10286 | relative, it is taken as relative to dirfd.\n\ |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10287 | If atime and mtime are both None, which is the default, set atime and\n\ |
| 10288 | mtime to the current time.\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10289 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 10290 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10291 | is interpreted relative to the current working directory.\n\ |
| 10292 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 10293 | current time.\n\ |
| 10294 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 10295 | |
| 10296 | static PyObject * |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10297 | posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10298 | { |
| 10299 | PyObject *opath; |
| 10300 | char *path; |
| 10301 | int res, dirfd, flags = 0; |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10302 | PyObject *atime = Py_None; |
| 10303 | PyObject *mtime = Py_None; |
| 10304 | |
| 10305 | static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL}; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10306 | |
| 10307 | struct timespec buf[2]; |
| 10308 | |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10309 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10310 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 10311 | return NULL; |
| 10312 | path = PyBytes_AsString(opath); |
| 10313 | if (atime == Py_None && mtime == Py_None) { |
| 10314 | /* optional time values not given */ |
| 10315 | Py_BEGIN_ALLOW_THREADS |
| 10316 | res = utimensat(dirfd, path, NULL, flags); |
| 10317 | Py_END_ALLOW_THREADS |
| 10318 | } |
| 10319 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 10320 | PyErr_SetString(PyExc_TypeError, |
| 10321 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 10322 | Py_DECREF(opath); |
| 10323 | return NULL; |
| 10324 | } |
| 10325 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 10326 | PyErr_SetString(PyExc_TypeError, |
| 10327 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 10328 | Py_DECREF(opath); |
| 10329 | return NULL; |
| 10330 | } |
| 10331 | else { |
| 10332 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 10333 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 10334 | Py_DECREF(opath); |
| 10335 | return NULL; |
| 10336 | } |
| 10337 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 10338 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 10339 | Py_DECREF(opath); |
| 10340 | return NULL; |
| 10341 | } |
| 10342 | Py_BEGIN_ALLOW_THREADS |
| 10343 | res = utimensat(dirfd, path, buf, flags); |
| 10344 | Py_END_ALLOW_THREADS |
| 10345 | } |
| 10346 | Py_DECREF(opath); |
| 10347 | if (res < 0) { |
| 10348 | return posix_error(); |
| 10349 | } |
| 10350 | Py_RETURN_NONE; |
| 10351 | } |
| 10352 | #endif |
| 10353 | |
| 10354 | #ifdef HAVE_MKFIFOAT |
| 10355 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 10356 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 10357 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10358 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10359 | is interpreted relative to the current working directory."); |
| 10360 | |
| 10361 | static PyObject * |
| 10362 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 10363 | { |
| 10364 | PyObject *opath; |
| 10365 | char *filename; |
| 10366 | int mode = 0666; |
| 10367 | int res, dirfd; |
| 10368 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 10369 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 10370 | return NULL; |
| 10371 | filename = PyBytes_AS_STRING(opath); |
| 10372 | Py_BEGIN_ALLOW_THREADS |
| 10373 | res = mkfifoat(dirfd, filename, mode); |
| 10374 | Py_END_ALLOW_THREADS |
| 10375 | Py_DECREF(opath); |
| 10376 | if (res < 0) |
| 10377 | return posix_error(); |
| 10378 | Py_RETURN_NONE; |
| 10379 | } |
| 10380 | #endif |
| 10381 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10382 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10383 | |
| 10384 | static int |
| 10385 | try_getxattr(const char *path, const char *name, |
| 10386 | ssize_t (*get)(const char *, const char *, void *, size_t), |
| 10387 | Py_ssize_t buf_size, PyObject **res) |
| 10388 | { |
| 10389 | PyObject *value; |
| 10390 | Py_ssize_t len; |
| 10391 | |
| 10392 | assert(buf_size <= XATTR_SIZE_MAX); |
| 10393 | value = PyBytes_FromStringAndSize(NULL, buf_size); |
| 10394 | if (!value) |
| 10395 | return 0; |
| 10396 | Py_BEGIN_ALLOW_THREADS; |
| 10397 | len = get(path, name, PyBytes_AS_STRING(value), buf_size); |
| 10398 | Py_END_ALLOW_THREADS; |
| 10399 | if (len < 0) { |
| 10400 | Py_DECREF(value); |
| 10401 | if (errno == ERANGE) { |
| 10402 | value = NULL; |
| 10403 | } |
| 10404 | else { |
| 10405 | posix_error(); |
| 10406 | return 0; |
| 10407 | } |
| 10408 | } |
| 10409 | else if (len != buf_size) { |
| 10410 | /* Can only shrink. */ |
| 10411 | _PyBytes_Resize(&value, len); |
| 10412 | } |
| 10413 | *res = value; |
| 10414 | return 1; |
| 10415 | } |
| 10416 | |
| 10417 | static PyObject * |
| 10418 | getxattr_common(const char *path, PyObject *name_obj, |
| 10419 | ssize_t (*get)(const char *, const char *, void *, size_t)) |
| 10420 | { |
| 10421 | PyObject *value; |
| 10422 | const char *name = PyBytes_AS_STRING(name_obj); |
| 10423 | |
| 10424 | /* Try a small value first. */ |
| 10425 | if (!try_getxattr(path, name, get, 128, &value)) |
| 10426 | return NULL; |
| 10427 | if (value) |
| 10428 | return value; |
| 10429 | /* Now the maximum possible one. */ |
| 10430 | if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value)) |
| 10431 | return NULL; |
| 10432 | assert(value); |
| 10433 | return value; |
| 10434 | } |
| 10435 | |
| 10436 | PyDoc_STRVAR(posix_getxattr__doc__, |
| 10437 | "getxattr(path, attr) -> value\n\n\ |
| 10438 | Return the value of extended attribute *name* on *path*."); |
| 10439 | |
| 10440 | static PyObject * |
| 10441 | posix_getxattr(PyObject *self, PyObject *args) |
| 10442 | { |
| 10443 | PyObject *path, *res, *name; |
| 10444 | |
| 10445 | if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path, |
| 10446 | PyUnicode_FSConverter, &name)) |
| 10447 | return NULL; |
| 10448 | res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr); |
| 10449 | Py_DECREF(path); |
| 10450 | Py_DECREF(name); |
| 10451 | return res; |
| 10452 | } |
| 10453 | |
| 10454 | PyDoc_STRVAR(posix_lgetxattr__doc__, |
| 10455 | "lgetxattr(path, attr) -> value\n\n\ |
| 10456 | Like getxattr but don't follow symlinks."); |
| 10457 | |
| 10458 | static PyObject * |
| 10459 | posix_lgetxattr(PyObject *self, PyObject *args) |
| 10460 | { |
| 10461 | PyObject *path, *res, *name; |
| 10462 | |
| 10463 | if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path, |
| 10464 | PyUnicode_FSConverter, &name)) |
| 10465 | return NULL; |
| 10466 | res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr); |
| 10467 | Py_DECREF(path); |
| 10468 | Py_DECREF(name); |
| 10469 | return res; |
| 10470 | } |
| 10471 | |
| 10472 | static ssize_t |
| 10473 | wrap_fgetxattr(const char *path, const char *name, void *value, size_t size) |
| 10474 | { |
| 10475 | /* Hack to share code. */ |
| 10476 | return fgetxattr((int)(Py_uintptr_t)path, name, value, size); |
| 10477 | } |
| 10478 | |
| 10479 | PyDoc_STRVAR(posix_fgetxattr__doc__, |
| 10480 | "fgetxattr(fd, attr) -> value\n\n\ |
| 10481 | Like getxattr but operate on a fd instead of a path."); |
| 10482 | |
| 10483 | static PyObject * |
| 10484 | posix_fgetxattr(PyObject *self, PyObject *args) |
| 10485 | { |
| 10486 | PyObject *res, *name; |
| 10487 | int fd; |
| 10488 | |
| 10489 | if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name)) |
| 10490 | return NULL; |
| 10491 | res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr); |
| 10492 | Py_DECREF(name); |
| 10493 | return res; |
| 10494 | } |
| 10495 | |
| 10496 | PyDoc_STRVAR(posix_setxattr__doc__, |
| 10497 | "setxattr(path, attr, value, flags=0)\n\n\ |
| 10498 | Set extended attribute *attr* on *path* to *value*."); |
| 10499 | |
| 10500 | static PyObject * |
| 10501 | posix_setxattr(PyObject *self, PyObject *args) |
| 10502 | { |
| 10503 | PyObject *path, *name; |
| 10504 | Py_buffer data; |
| 10505 | int flags = 0, err; |
| 10506 | |
| 10507 | if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter, |
| 10508 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10509 | return NULL; |
| 10510 | Py_BEGIN_ALLOW_THREADS; |
| 10511 | err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10512 | data.buf, data.len, flags); |
| 10513 | Py_END_ALLOW_THREADS; |
| 10514 | Py_DECREF(path); |
| 10515 | Py_DECREF(name); |
| 10516 | PyBuffer_Release(&data); |
| 10517 | if (err) |
| 10518 | return posix_error(); |
| 10519 | Py_RETURN_NONE; |
| 10520 | } |
| 10521 | |
| 10522 | PyDoc_STRVAR(posix_lsetxattr__doc__, |
| 10523 | "lsetxattr(path, attr, value, flags=0)\n\n\ |
| 10524 | Like setxattr but don't follow symlinks."); |
| 10525 | |
| 10526 | static PyObject * |
| 10527 | posix_lsetxattr(PyObject *self, PyObject *args) |
| 10528 | { |
| 10529 | PyObject *path, *name; |
| 10530 | Py_buffer data; |
| 10531 | int flags = 0, err; |
| 10532 | |
| 10533 | if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter, |
| 10534 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10535 | return NULL; |
| 10536 | Py_BEGIN_ALLOW_THREADS; |
| 10537 | err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10538 | data.buf, data.len, flags); |
| 10539 | Py_END_ALLOW_THREADS; |
| 10540 | Py_DECREF(path); |
| 10541 | Py_DECREF(name); |
| 10542 | PyBuffer_Release(&data); |
| 10543 | if (err) |
| 10544 | return posix_error(); |
| 10545 | Py_RETURN_NONE; |
| 10546 | } |
| 10547 | |
| 10548 | PyDoc_STRVAR(posix_fsetxattr__doc__, |
| 10549 | "fsetxattr(fd, attr, value, flags=0)\n\n\ |
| 10550 | Like setxattr but operates on *fd* instead of a path."); |
| 10551 | |
| 10552 | static PyObject * |
| 10553 | posix_fsetxattr(PyObject *self, PyObject *args) |
| 10554 | { |
| 10555 | Py_buffer data; |
| 10556 | const char *name; |
| 10557 | int fd, flags = 0, err; |
| 10558 | |
| 10559 | if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter, |
| 10560 | &name, &data, &flags)) |
| 10561 | return NULL; |
| 10562 | Py_BEGIN_ALLOW_THREADS; |
| 10563 | err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags); |
| 10564 | Py_END_ALLOW_THREADS; |
| 10565 | Py_DECREF(name); |
| 10566 | PyBuffer_Release(&data); |
| 10567 | if (err) |
| 10568 | return posix_error(); |
| 10569 | Py_RETURN_NONE; |
| 10570 | } |
| 10571 | |
| 10572 | PyDoc_STRVAR(posix_removexattr__doc__, |
| 10573 | "removexattr(path, attr)\n\n\ |
| 10574 | Remove extended attribute *attr* on *path*."); |
| 10575 | |
| 10576 | static PyObject * |
| 10577 | posix_removexattr(PyObject *self, PyObject *args) |
| 10578 | { |
| 10579 | PyObject *path, *name; |
| 10580 | int err; |
| 10581 | |
| 10582 | if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path, |
| 10583 | PyUnicode_FSConverter, &name)) |
| 10584 | return NULL; |
| 10585 | Py_BEGIN_ALLOW_THREADS; |
| 10586 | err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10587 | Py_END_ALLOW_THREADS; |
| 10588 | Py_DECREF(path); |
| 10589 | Py_DECREF(name); |
| 10590 | if (err) |
| 10591 | return posix_error(); |
| 10592 | Py_RETURN_NONE; |
| 10593 | } |
| 10594 | |
| 10595 | PyDoc_STRVAR(posix_lremovexattr__doc__, |
| 10596 | "lremovexattr(path, attr)\n\n\ |
| 10597 | Like removexattr but don't follow symlinks."); |
| 10598 | |
| 10599 | static PyObject * |
| 10600 | posix_lremovexattr(PyObject *self, PyObject *args) |
| 10601 | { |
| 10602 | PyObject *path, *name; |
| 10603 | int err; |
| 10604 | |
| 10605 | if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path, |
| 10606 | PyUnicode_FSConverter, &name)) |
| 10607 | return NULL; |
| 10608 | Py_BEGIN_ALLOW_THREADS; |
| 10609 | err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10610 | Py_END_ALLOW_THREADS; |
| 10611 | Py_DECREF(path); |
| 10612 | Py_DECREF(name); |
| 10613 | if (err) |
| 10614 | return posix_error(); |
| 10615 | Py_RETURN_NONE; |
| 10616 | } |
| 10617 | |
| 10618 | PyDoc_STRVAR(posix_fremovexattr__doc__, |
| 10619 | "fremovexattr(fd, attr)\n\n\ |
| 10620 | Like removexattr but operates on a file descriptor."); |
| 10621 | |
| 10622 | static PyObject * |
| 10623 | posix_fremovexattr(PyObject *self, PyObject *args) |
| 10624 | { |
| 10625 | PyObject *name; |
| 10626 | int fd, err; |
| 10627 | |
| 10628 | if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd, |
| 10629 | PyUnicode_FSConverter, &name)) |
| 10630 | return NULL; |
| 10631 | Py_BEGIN_ALLOW_THREADS; |
| 10632 | err = fremovexattr(fd, PyBytes_AS_STRING(name)); |
| 10633 | Py_END_ALLOW_THREADS; |
| 10634 | Py_DECREF(name); |
| 10635 | if (err) |
| 10636 | return posix_error(); |
| 10637 | Py_RETURN_NONE; |
| 10638 | } |
| 10639 | |
| 10640 | static Py_ssize_t |
| 10641 | try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t), |
| 10642 | Py_ssize_t buf_size, char **buf) |
| 10643 | { |
| 10644 | Py_ssize_t len; |
| 10645 | |
| 10646 | *buf = PyMem_MALLOC(buf_size); |
| 10647 | if (!*buf) { |
| 10648 | PyErr_NoMemory(); |
| 10649 | return -1; |
| 10650 | } |
| 10651 | Py_BEGIN_ALLOW_THREADS; |
| 10652 | len = list(path, *buf, buf_size); |
| 10653 | Py_END_ALLOW_THREADS; |
| 10654 | if (len < 0) { |
| 10655 | PyMem_FREE(*buf); |
| 10656 | if (errno != ERANGE) |
| 10657 | posix_error(); |
| 10658 | return -1; |
| 10659 | } |
| 10660 | return len; |
| 10661 | } |
| 10662 | |
| 10663 | static PyObject * |
| 10664 | listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t)) |
| 10665 | { |
| 10666 | PyObject *res, *attr; |
| 10667 | Py_ssize_t len, err, start, i; |
| 10668 | char *buf; |
| 10669 | |
| 10670 | len = try_listxattr(path, list, 256, &buf); |
| 10671 | if (len < 0) { |
| 10672 | if (PyErr_Occurred()) |
| 10673 | return NULL; |
| 10674 | len = try_listxattr(path, list, XATTR_LIST_MAX, &buf); |
| 10675 | if (len < 0) |
| 10676 | return NULL; |
| 10677 | } |
| 10678 | res = PyList_New(0); |
| 10679 | if (!res) { |
| 10680 | PyMem_FREE(buf); |
| 10681 | return NULL; |
| 10682 | } |
| 10683 | for (start = i = 0; i < len; i++) { |
| 10684 | if (!buf[i]) { |
| 10685 | attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start); |
| 10686 | if (!attr) { |
| 10687 | Py_DECREF(res); |
| 10688 | PyMem_FREE(buf); |
| 10689 | return NULL; |
| 10690 | } |
| 10691 | err = PyList_Append(res, attr); |
| 10692 | Py_DECREF(attr); |
| 10693 | if (err) { |
| 10694 | Py_DECREF(res); |
| 10695 | PyMem_FREE(buf); |
| 10696 | return NULL; |
| 10697 | } |
| 10698 | start = i + 1; |
| 10699 | } |
| 10700 | } |
| 10701 | PyMem_FREE(buf); |
| 10702 | return res; |
| 10703 | } |
| 10704 | |
| 10705 | PyDoc_STRVAR(posix_listxattr__doc__, |
| 10706 | "listxattr(path)\n\n\ |
| 10707 | Return a list of extended attributes on *path*."); |
| 10708 | |
| 10709 | static PyObject * |
| 10710 | posix_listxattr(PyObject *self, PyObject *args) |
| 10711 | { |
| 10712 | PyObject *path, *res; |
| 10713 | |
| 10714 | if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path)) |
| 10715 | return NULL; |
| 10716 | res = listxattr_common(PyBytes_AS_STRING(path), listxattr); |
| 10717 | Py_DECREF(path); |
| 10718 | return res; |
| 10719 | } |
| 10720 | |
| 10721 | PyDoc_STRVAR(posix_llistxattr__doc__, |
| 10722 | "llistxattr(path)\n\n\ |
| 10723 | Like listxattr but don't follow symlinks.."); |
| 10724 | |
| 10725 | static PyObject * |
| 10726 | posix_llistxattr(PyObject *self, PyObject *args) |
| 10727 | { |
| 10728 | PyObject *path, *res; |
| 10729 | |
| 10730 | if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path)) |
| 10731 | return NULL; |
| 10732 | res = listxattr_common(PyBytes_AS_STRING(path), llistxattr); |
| 10733 | Py_DECREF(path); |
| 10734 | return res; |
| 10735 | } |
| 10736 | |
| 10737 | static ssize_t |
| 10738 | wrap_flistxattr(const char *path, char *buf, size_t len) |
| 10739 | { |
| 10740 | /* Hack to share code. */ |
| 10741 | return flistxattr((int)(Py_uintptr_t)path, buf, len); |
| 10742 | } |
| 10743 | |
| 10744 | PyDoc_STRVAR(posix_flistxattr__doc__, |
| 10745 | "flistxattr(path)\n\n\ |
| 10746 | Like flistxattr but operates on a file descriptor."); |
| 10747 | |
| 10748 | static PyObject * |
| 10749 | posix_flistxattr(PyObject *self, PyObject *args) |
| 10750 | { |
| 10751 | long fd; |
| 10752 | |
| 10753 | if (!PyArg_ParseTuple(args, "i:flistxattr", &fd)) |
| 10754 | return NULL; |
| 10755 | return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr); |
| 10756 | } |
| 10757 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10758 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10759 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10760 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10761 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10762 | "urandom(n) -> str\n\n\ |
| 10763 | Return n random bytes suitable for cryptographic use."); |
| 10764 | |
| 10765 | static PyObject * |
| 10766 | posix_urandom(PyObject *self, PyObject *args) |
| 10767 | { |
| 10768 | Py_ssize_t size; |
| 10769 | PyObject *result; |
| 10770 | int ret; |
| 10771 | |
| 10772 | /* Read arguments */ |
| 10773 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10774 | return NULL; |
| 10775 | if (size < 0) |
| 10776 | return PyErr_Format(PyExc_ValueError, |
| 10777 | "negative argument not allowed"); |
| 10778 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10779 | if (result == NULL) |
| 10780 | return NULL; |
| 10781 | |
| 10782 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10783 | PyBytes_GET_SIZE(result)); |
| 10784 | if (ret == -1) { |
| 10785 | Py_DECREF(result); |
| 10786 | return NULL; |
| 10787 | } |
| 10788 | return result; |
| 10789 | } |
| 10790 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10791 | /* Terminal size querying */ |
| 10792 | |
| 10793 | static PyTypeObject TerminalSizeType; |
| 10794 | |
| 10795 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10796 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10797 | |
| 10798 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10799 | {"columns", "width of the terminal window in characters"}, |
| 10800 | {"lines", "height of the terminal window in characters"}, |
| 10801 | {NULL, NULL} |
| 10802 | }; |
| 10803 | |
| 10804 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10805 | "os.terminal_size", |
| 10806 | TerminalSize_docstring, |
| 10807 | TerminalSize_fields, |
| 10808 | 2, |
| 10809 | }; |
| 10810 | |
| 10811 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10812 | PyDoc_STRVAR(termsize__doc__, |
| 10813 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10814 | "\n" \ |
| 10815 | "The optional argument fd (default standard output) specifies\n" \ |
| 10816 | "which file descriptor should be queried.\n" \ |
| 10817 | "\n" \ |
| 10818 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10819 | "is thrown.\n" \ |
| 10820 | "\n" \ |
| 10821 | "This function will only be defined if an implementation is\n" \ |
| 10822 | "available for this system.\n" \ |
| 10823 | "\n" \ |
| 10824 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10825 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10826 | |
| 10827 | static PyObject* |
| 10828 | get_terminal_size(PyObject *self, PyObject *args) |
| 10829 | { |
| 10830 | int columns, lines; |
| 10831 | PyObject *termsize; |
| 10832 | |
| 10833 | int fd = fileno(stdout); |
| 10834 | /* Under some conditions stdout may not be connected and |
| 10835 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10836 | * GUI apps don't have valid standard streams by default. |
| 10837 | * |
| 10838 | * If this happens, and the optional fd argument is not present, |
| 10839 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10840 | */ |
| 10841 | |
| 10842 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10843 | return NULL; |
| 10844 | |
| 10845 | #ifdef TERMSIZE_USE_IOCTL |
| 10846 | { |
| 10847 | struct winsize w; |
| 10848 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10849 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10850 | columns = w.ws_col; |
| 10851 | lines = w.ws_row; |
| 10852 | } |
| 10853 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10854 | |
| 10855 | #ifdef TERMSIZE_USE_CONIO |
| 10856 | { |
| 10857 | DWORD nhandle; |
| 10858 | HANDLE handle; |
| 10859 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10860 | switch (fd) { |
| 10861 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10862 | break; |
| 10863 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10864 | break; |
| 10865 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10866 | break; |
| 10867 | default: |
| 10868 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10869 | } |
| 10870 | handle = GetStdHandle(nhandle); |
| 10871 | if (handle == NULL) |
| 10872 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10873 | if (handle == INVALID_HANDLE_VALUE) |
| 10874 | return PyErr_SetFromWindowsErr(0); |
| 10875 | |
| 10876 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10877 | return PyErr_SetFromWindowsErr(0); |
| 10878 | |
| 10879 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10880 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10881 | } |
| 10882 | #endif /* TERMSIZE_USE_CONIO */ |
| 10883 | |
| 10884 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10885 | if (termsize == NULL) |
| 10886 | return NULL; |
| 10887 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10888 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10889 | if (PyErr_Occurred()) { |
| 10890 | Py_DECREF(termsize); |
| 10891 | return NULL; |
| 10892 | } |
| 10893 | return termsize; |
| 10894 | } |
| 10895 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10896 | |
| 10897 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10898 | static PyMethodDef posix_methods[] = { |
Larry Hastings | cfe6f2a | 2012-05-05 17:39:09 -0700 | [diff] [blame^] | 10899 | {"access", (PyCFunction)posix_access, |
| 10900 | METH_VARARGS | METH_KEYWORDS, |
| 10901 | posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10902 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10903 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10904 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10905 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10906 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10907 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10908 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10909 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10910 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10911 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10912 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10913 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10914 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10915 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10916 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10917 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10918 | #endif /* HAVE_LCHMOD */ |
| 10919 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10920 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10921 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10922 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10923 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10924 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10925 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10926 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10927 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10928 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10929 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10930 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10931 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10932 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10933 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10934 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10935 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10936 | METH_NOARGS, posix_getcwd__doc__}, |
| 10937 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10938 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10939 | #endif |
| 10940 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10941 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10942 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10943 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10944 | #ifdef HAVE_FDOPENDIR |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 10945 | {"flistdir", posix_flistdir, METH_VARARGS, posix_flistdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10946 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10947 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10948 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10949 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10950 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10951 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10952 | #ifdef HAVE_GETPRIORITY |
| 10953 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10954 | #endif /* HAVE_GETPRIORITY */ |
| 10955 | #ifdef HAVE_SETPRIORITY |
| 10956 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10957 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10958 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10959 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10960 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10961 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10962 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10963 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10964 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 10965 | {"replace", posix_replace, METH_VARARGS, posix_replace__doc__}, |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10966 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10967 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10968 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10969 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10970 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10971 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10972 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10973 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10974 | win_symlink__doc__}, |
| 10975 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10976 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10977 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10978 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10979 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10980 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10981 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10982 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10983 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 10984 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10985 | {"utime", (PyCFunction)posix_utime, |
| 10986 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10987 | #ifdef HAVE_FUTIMES |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10988 | {"futimes", (PyCFunction)posix_futimes, |
| 10989 | METH_VARARGS | METH_KEYWORDS, posix_futimes__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10990 | #endif |
| 10991 | #ifdef HAVE_LUTIMES |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10992 | {"lutimes", (PyCFunction)posix_lutimes, |
| 10993 | METH_VARARGS | METH_KEYWORDS, posix_lutimes__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10994 | #endif |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10995 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10996 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10997 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10998 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10999 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11000 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 11001 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11002 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11003 | #ifdef HAVE_FEXECVE |
| 11004 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 11005 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11006 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11007 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 11008 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 11009 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11010 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 11011 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 11012 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 11013 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11014 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11015 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 11016 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11017 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11018 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11019 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11020 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11021 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11022 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 11023 | {"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] | 11024 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11025 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11026 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11027 | #endif |
| 11028 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11029 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11030 | #endif |
| 11031 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11032 | {"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] | 11033 | #endif |
| 11034 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11035 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11036 | #endif |
| 11037 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11038 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 11039 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11040 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11041 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11042 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 11043 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 11044 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 11045 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11046 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11047 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 11048 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11049 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11050 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 11051 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 11052 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11053 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 11054 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11055 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11056 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 11057 | #endif /* HAVE_GETEUID */ |
| 11058 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11059 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11060 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 11061 | #ifdef HAVE_GETGROUPLIST |
| 11062 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 11063 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11064 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11065 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11066 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11068 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11069 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11070 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11071 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11072 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11073 | #endif /* HAVE_GETPPID */ |
| 11074 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11075 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11076 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11077 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11078 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11079 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11080 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11081 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11082 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11083 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11084 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 11085 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11086 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11087 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 11088 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11089 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11090 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 11091 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 11092 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 11093 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11094 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11096 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11097 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11098 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11099 | #endif /* HAVE_SETEUID */ |
| 11100 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11101 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11102 | #endif /* HAVE_SETEGID */ |
| 11103 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11104 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11105 | #endif /* HAVE_SETREUID */ |
| 11106 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11107 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 11108 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11109 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11111 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11112 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 11114 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11115 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11116 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 11117 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11118 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11119 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 11120 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11121 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11122 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11123 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11124 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11125 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 11126 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11127 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11128 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11129 | #endif /* HAVE_WAIT3 */ |
| 11130 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11131 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11132 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11133 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11134 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 11135 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 11136 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11137 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11138 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11139 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11140 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 11141 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11142 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11143 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11144 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11145 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11146 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11147 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11148 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11149 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11150 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11151 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11152 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11153 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11154 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 11155 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 11156 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 11157 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 11158 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 11159 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11160 | #ifdef HAVE_LOCKF |
| 11161 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 11162 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11163 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 11164 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11165 | #ifdef HAVE_READV |
| 11166 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 11167 | #endif |
| 11168 | #ifdef HAVE_PREAD |
| 11169 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 11170 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11171 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11172 | #ifdef HAVE_WRITEV |
| 11173 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 11174 | #endif |
| 11175 | #ifdef HAVE_PWRITE |
| 11176 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 11177 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11178 | #ifdef HAVE_SENDFILE |
| 11179 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 11180 | posix_sendfile__doc__}, |
| 11181 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11182 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11183 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11184 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11185 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11186 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11187 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 11188 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11189 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11190 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11191 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11192 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 11193 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11194 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 11195 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11196 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11197 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 11198 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 11199 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 11200 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11201 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11202 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11203 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11204 | #ifdef HAVE_TRUNCATE |
| 11205 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 11206 | #endif |
| 11207 | #ifdef HAVE_POSIX_FALLOCATE |
| 11208 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 11209 | #endif |
| 11210 | #ifdef HAVE_POSIX_FADVISE |
| 11211 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 11212 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11213 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11214 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 11215 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11216 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11217 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 11218 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11219 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11220 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11221 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11222 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11223 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11224 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11225 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11226 | #ifdef HAVE_SYNC |
| 11227 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 11228 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11229 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11230 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 11231 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11232 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11233 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11234 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11235 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11236 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11237 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 11238 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11239 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11240 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11241 | #endif /* WIFSTOPPED */ |
| 11242 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11243 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11244 | #endif /* WIFSIGNALED */ |
| 11245 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11246 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11247 | #endif /* WIFEXITED */ |
| 11248 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11249 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11250 | #endif /* WEXITSTATUS */ |
| 11251 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11252 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11253 | #endif /* WTERMSIG */ |
| 11254 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11255 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11256 | #endif /* WSTOPSIG */ |
| 11257 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11258 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11259 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11260 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11261 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11263 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11264 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11265 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11266 | #endif |
| 11267 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11268 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11269 | #endif |
| 11270 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11271 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11272 | #endif |
| 11273 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11274 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11275 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11276 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11277 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11278 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 11279 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 11280 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 11281 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11282 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 11283 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11284 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11285 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11286 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 11287 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11288 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11289 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11290 | #endif |
| 11291 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11292 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11293 | #endif |
| 11294 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11295 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11296 | #endif |
| 11297 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11298 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11299 | #endif |
| 11300 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11301 | /* posix *at family of functions */ |
| 11302 | #ifdef HAVE_FACCESSAT |
| 11303 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 11304 | #endif |
| 11305 | #ifdef HAVE_FCHMODAT |
| 11306 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 11307 | #endif /* HAVE_FCHMODAT */ |
| 11308 | #ifdef HAVE_FCHOWNAT |
| 11309 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 11310 | #endif /* HAVE_FCHOWNAT */ |
| 11311 | #ifdef HAVE_FSTATAT |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11312 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11313 | #endif |
| 11314 | #ifdef HAVE_FUTIMESAT |
| 11315 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 11316 | #endif |
| 11317 | #ifdef HAVE_LINKAT |
| 11318 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 11319 | #endif /* HAVE_LINKAT */ |
| 11320 | #ifdef HAVE_MKDIRAT |
| 11321 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 11322 | #endif |
| 11323 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 11324 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 11325 | #endif |
| 11326 | #ifdef HAVE_OPENAT |
| 11327 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 11328 | #endif |
| 11329 | #ifdef HAVE_READLINKAT |
| 11330 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 11331 | #endif /* HAVE_READLINKAT */ |
| 11332 | #ifdef HAVE_RENAMEAT |
| 11333 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 11334 | #endif |
| 11335 | #if HAVE_SYMLINKAT |
| 11336 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 11337 | #endif /* HAVE_SYMLINKAT */ |
| 11338 | #ifdef HAVE_UNLINKAT |
| 11339 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 11340 | #endif |
| 11341 | #ifdef HAVE_UTIMENSAT |
Jesus Cea | d03a491 | 2011-11-08 17:28:04 +0100 | [diff] [blame] | 11342 | {"utimensat", (PyCFunction)posix_utimensat, |
| 11343 | METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 11344 | posix_utimensat__doc__}, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11345 | #endif |
| 11346 | #ifdef HAVE_MKFIFOAT |
| 11347 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 11348 | #endif |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11349 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11350 | {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__}, |
| 11351 | {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__}, |
| 11352 | {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__}, |
| 11353 | {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__}, |
| 11354 | {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__}, |
| 11355 | {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__}, |
| 11356 | {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__}, |
| 11357 | {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__}, |
| 11358 | {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__}, |
| 11359 | {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__}, |
| 11360 | {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__}, |
| 11361 | {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__}, |
| 11362 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11363 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 11364 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 11365 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11366 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11367 | }; |
| 11368 | |
| 11369 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11370 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11371 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11372 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11373 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11374 | } |
| 11375 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11376 | #if defined(PYOS_OS2) |
| 11377 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11378 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11379 | { |
| 11380 | APIRET rc; |
| 11381 | ULONG values[QSV_MAX+1]; |
| 11382 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 11383 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11384 | |
| 11385 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 11386 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11387 | Py_END_ALLOW_THREADS |
| 11388 | |
| 11389 | if (rc != NO_ERROR) { |
| 11390 | os2_error(rc); |
| 11391 | return -1; |
| 11392 | } |
| 11393 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11394 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 11395 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 11396 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 11397 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 11398 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 11399 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 11400 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11401 | |
| 11402 | switch (values[QSV_VERSION_MINOR]) { |
| 11403 | case 0: ver = "2.00"; break; |
| 11404 | case 10: ver = "2.10"; break; |
| 11405 | case 11: ver = "2.11"; break; |
| 11406 | case 30: ver = "3.00"; break; |
| 11407 | case 40: ver = "4.00"; break; |
| 11408 | case 50: ver = "5.00"; break; |
| 11409 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11410 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11411 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11412 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11413 | ver = &tmp[0]; |
| 11414 | } |
| 11415 | |
| 11416 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11417 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11418 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11419 | |
| 11420 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 11421 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 11422 | tmp[1] = ':'; |
| 11423 | tmp[2] = '\0'; |
| 11424 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11425 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11426 | } |
| 11427 | #endif |
| 11428 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11429 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11430 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11431 | enable_symlink() |
| 11432 | { |
| 11433 | HANDLE tok; |
| 11434 | TOKEN_PRIVILEGES tok_priv; |
| 11435 | LUID luid; |
| 11436 | int meth_idx = 0; |
| 11437 | |
| 11438 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11439 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11440 | |
| 11441 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11442 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11443 | |
| 11444 | tok_priv.PrivilegeCount = 1; |
| 11445 | tok_priv.Privileges[0].Luid = luid; |
| 11446 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11447 | |
| 11448 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11449 | sizeof(TOKEN_PRIVILEGES), |
| 11450 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11451 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11452 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11453 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11454 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11455 | } |
| 11456 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11457 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11458 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11459 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11460 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11461 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11462 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11463 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11464 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11465 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11466 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11467 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11468 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11469 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11470 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11471 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11472 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11473 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11474 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11475 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11476 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11477 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11478 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11479 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11480 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11481 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11482 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11483 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11484 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11485 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11486 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11487 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11488 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11489 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11490 | #endif |
| 11491 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11492 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11493 | #endif |
| 11494 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11495 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11496 | #endif |
| 11497 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11498 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11499 | #endif |
| 11500 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11501 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11502 | #endif |
| 11503 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11504 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11505 | #endif |
| 11506 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11507 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11508 | #endif |
| 11509 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11510 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11511 | #endif |
| 11512 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11513 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11514 | #endif |
| 11515 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11516 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11517 | #endif |
| 11518 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11519 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11520 | #endif |
| 11521 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11522 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11523 | #endif |
| 11524 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11525 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11526 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11527 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11528 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11529 | #endif |
| 11530 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11531 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11532 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11533 | #ifdef O_XATTR |
| 11534 | if (ins(d, "O_XATTR", (long)O_XATTR)) return -1; |
| 11535 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11536 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11537 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11538 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11539 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11540 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11541 | #endif |
| 11542 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11543 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11544 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11545 | #ifdef O_EXEC |
| 11546 | if (ins(d, "O_EXEC", (long)O_EXEC)) return -1; |
| 11547 | #endif |
| 11548 | #ifdef O_SEARCH |
| 11549 | if (ins(d, "O_SEARCH", (long)O_SEARCH)) return -1; |
| 11550 | #endif |
| 11551 | #ifdef O_TTY_INIT |
| 11552 | if (ins(d, "O_TTY_INIT", (long)O_TTY_INIT)) return -1; |
| 11553 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11554 | #ifdef PRIO_PROCESS |
| 11555 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11556 | #endif |
| 11557 | #ifdef PRIO_PGRP |
| 11558 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11559 | #endif |
| 11560 | #ifdef PRIO_USER |
| 11561 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11562 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11563 | #ifdef O_CLOEXEC |
| 11564 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11565 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11566 | #ifdef O_ACCMODE |
| 11567 | if (ins(d, "O_ACCMODE", (long)O_ACCMODE)) return -1; |
| 11568 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11569 | /* posix - constants for *at functions */ |
| 11570 | #ifdef AT_SYMLINK_NOFOLLOW |
| 11571 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 11572 | #endif |
| 11573 | #ifdef AT_EACCESS |
| 11574 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 11575 | #endif |
| 11576 | #ifdef AT_FDCWD |
| 11577 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 11578 | #endif |
| 11579 | #ifdef AT_REMOVEDIR |
| 11580 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 11581 | #endif |
| 11582 | #ifdef AT_SYMLINK_FOLLOW |
| 11583 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 11584 | #endif |
| 11585 | #ifdef UTIME_NOW |
| 11586 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11587 | #endif |
| 11588 | #ifdef UTIME_OMIT |
| 11589 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11590 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11591 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11592 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11593 | /* MS Windows */ |
| 11594 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11595 | /* Don't inherit in child processes. */ |
| 11596 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11597 | #endif |
| 11598 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11599 | /* Optimize for short life (keep in memory). */ |
| 11600 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11601 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11602 | #endif |
| 11603 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11604 | /* Automatically delete when last handle is closed. */ |
| 11605 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11606 | #endif |
| 11607 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11608 | /* Optimize for random access. */ |
| 11609 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11610 | #endif |
| 11611 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11612 | /* Optimize for sequential access. */ |
| 11613 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11614 | #endif |
| 11615 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11616 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11617 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11618 | /* Send a SIGIO signal whenever input or output |
| 11619 | becomes available on file descriptor */ |
| 11620 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11621 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11622 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11623 | /* Direct disk access. */ |
| 11624 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11625 | #endif |
| 11626 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11627 | /* Must be a directory. */ |
| 11628 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11629 | #endif |
| 11630 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11631 | /* Do not follow links. */ |
| 11632 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11633 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11634 | #ifdef O_NOLINKS |
| 11635 | /* Fails if link count of the named file is greater than 1 */ |
| 11636 | if (ins(d, "O_NOLINKS", (long)O_NOLINKS)) return -1; |
| 11637 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11638 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11639 | /* Do not update the access time. */ |
| 11640 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11641 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11642 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11643 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11644 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11645 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11646 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11647 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11648 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11649 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11650 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11651 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11652 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11653 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11654 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11655 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11656 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11657 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11658 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11659 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11660 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11661 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11662 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11663 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11664 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11665 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11666 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11667 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11668 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11669 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11670 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11671 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11672 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11673 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11674 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11675 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11676 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11677 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11678 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11679 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11680 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11681 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11682 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11683 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11684 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11685 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11686 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11687 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11688 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11689 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11690 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11691 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11692 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11693 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11694 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11695 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11696 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11697 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11698 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11699 | #endif /* ST_RDONLY */ |
| 11700 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11701 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11702 | #endif /* ST_NOSUID */ |
| 11703 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11704 | /* FreeBSD sendfile() constants */ |
| 11705 | #ifdef SF_NODISKIO |
| 11706 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11707 | #endif |
| 11708 | #ifdef SF_MNOWAIT |
| 11709 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11710 | #endif |
| 11711 | #ifdef SF_SYNC |
| 11712 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11713 | #endif |
| 11714 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11715 | /* constants for posix_fadvise */ |
| 11716 | #ifdef POSIX_FADV_NORMAL |
| 11717 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11718 | #endif |
| 11719 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11720 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11721 | #endif |
| 11722 | #ifdef POSIX_FADV_RANDOM |
| 11723 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11724 | #endif |
| 11725 | #ifdef POSIX_FADV_NOREUSE |
| 11726 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11727 | #endif |
| 11728 | #ifdef POSIX_FADV_WILLNEED |
| 11729 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11730 | #endif |
| 11731 | #ifdef POSIX_FADV_DONTNEED |
| 11732 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11733 | #endif |
| 11734 | |
| 11735 | /* constants for waitid */ |
| 11736 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11737 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11738 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11739 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11740 | #endif |
| 11741 | #ifdef WEXITED |
| 11742 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11743 | #endif |
| 11744 | #ifdef WNOWAIT |
| 11745 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11746 | #endif |
| 11747 | #ifdef WSTOPPED |
| 11748 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11749 | #endif |
| 11750 | #ifdef CLD_EXITED |
| 11751 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11752 | #endif |
| 11753 | #ifdef CLD_DUMPED |
| 11754 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11755 | #endif |
| 11756 | #ifdef CLD_TRAPPED |
| 11757 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11758 | #endif |
| 11759 | #ifdef CLD_CONTINUED |
| 11760 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11761 | #endif |
| 11762 | |
| 11763 | /* constants for lockf */ |
| 11764 | #ifdef F_LOCK |
| 11765 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11766 | #endif |
| 11767 | #ifdef F_TLOCK |
| 11768 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11769 | #endif |
| 11770 | #ifdef F_ULOCK |
| 11771 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11772 | #endif |
| 11773 | #ifdef F_TEST |
| 11774 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11775 | #endif |
| 11776 | |
| 11777 | /* constants for futimens */ |
| 11778 | #ifdef UTIME_NOW |
| 11779 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11780 | #endif |
| 11781 | #ifdef UTIME_OMIT |
| 11782 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11783 | #endif |
| 11784 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11785 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11786 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11787 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11788 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11789 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11790 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11791 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11792 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11793 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11794 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11795 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11796 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11797 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11798 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11799 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11800 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11801 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11802 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11803 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11804 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11805 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11806 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11807 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11808 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11809 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11810 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11811 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11812 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11813 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11814 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11815 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11816 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11817 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11818 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11819 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11820 | #ifdef SCHED_SPORADIC |
| 11821 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11822 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11823 | #ifdef SCHED_BATCH |
| 11824 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11825 | #endif |
| 11826 | #ifdef SCHED_IDLE |
| 11827 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11828 | #endif |
| 11829 | #ifdef SCHED_RESET_ON_FORK |
| 11830 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11831 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11832 | #ifdef SCHED_SYS |
| 11833 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11834 | #endif |
| 11835 | #ifdef SCHED_IA |
| 11836 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11837 | #endif |
| 11838 | #ifdef SCHED_FSS |
| 11839 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11840 | #endif |
| 11841 | #ifdef SCHED_FX |
| 11842 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11843 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11844 | #endif |
| 11845 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11846 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11847 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11848 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11849 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11850 | #endif |
| 11851 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11852 | #ifdef RTLD_LAZY |
| 11853 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11854 | #endif |
| 11855 | #ifdef RTLD_NOW |
| 11856 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11857 | #endif |
| 11858 | #ifdef RTLD_GLOBAL |
| 11859 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11860 | #endif |
| 11861 | #ifdef RTLD_LOCAL |
| 11862 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11863 | #endif |
| 11864 | #ifdef RTLD_NODELETE |
| 11865 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11866 | #endif |
| 11867 | #ifdef RTLD_NOLOAD |
| 11868 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11869 | #endif |
| 11870 | #ifdef RTLD_DEEPBIND |
| 11871 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11872 | #endif |
| 11873 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11874 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11875 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11876 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11877 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11878 | } |
| 11879 | |
| 11880 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11881 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11882 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11883 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11884 | |
| 11885 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11886 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11887 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11888 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11889 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11890 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11891 | #define MODNAME "posix" |
| 11892 | #endif |
| 11893 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11894 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11895 | PyModuleDef_HEAD_INIT, |
| 11896 | MODNAME, |
| 11897 | posix__doc__, |
| 11898 | -1, |
| 11899 | posix_methods, |
| 11900 | NULL, |
| 11901 | NULL, |
| 11902 | NULL, |
| 11903 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11904 | }; |
| 11905 | |
| 11906 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11907 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11908 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11909 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11910 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11911 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11912 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11913 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11914 | #endif |
| 11915 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11916 | m = PyModule_Create(&posixmodule); |
| 11917 | if (m == NULL) |
| 11918 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11919 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11920 | /* Initialize environ dictionary */ |
| 11921 | v = convertenviron(); |
| 11922 | Py_XINCREF(v); |
| 11923 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11924 | return NULL; |
| 11925 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11926 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11927 | if (all_ins(m)) |
| 11928 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11929 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11930 | if (setup_confname_tables(m)) |
| 11931 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11933 | Py_INCREF(PyExc_OSError); |
| 11934 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11935 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11936 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11937 | if (PyType_Ready(&cpu_set_type) < 0) |
| 11938 | return NULL; |
| 11939 | Py_INCREF(&cpu_set_type); |
| 11940 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11941 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11942 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11943 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11944 | if (posix_putenv_garbage == NULL) |
| 11945 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11946 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11948 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11949 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11950 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11951 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11952 | #endif |
| 11953 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11954 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11955 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11956 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11957 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11958 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11959 | structseq_new = StatResultType.tp_new; |
| 11960 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11961 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11962 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11963 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11964 | #ifdef NEED_TICKS_PER_SECOND |
| 11965 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11966 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11967 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11968 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11969 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11970 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11971 | # endif |
| 11972 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11973 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11974 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11975 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11976 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11977 | SchedParamType.tp_new = sched_param_new; |
| 11978 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11979 | |
| 11980 | /* initialize TerminalSize_info */ |
| 11981 | PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc); |
| 11982 | Py_INCREF(&TerminalSizeType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11983 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11984 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11985 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11986 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11987 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11988 | Py_INCREF((PyObject*) &StatResultType); |
| 11989 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11990 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11991 | PyModule_AddObject(m, "statvfs_result", |
| 11992 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11993 | |
| 11994 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11995 | Py_INCREF(&SchedParamType); |
| 11996 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11997 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11998 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11999 | |
| 12000 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12001 | /* |
| 12002 | * Step 2 of weak-linking support on Mac OS X. |
| 12003 | * |
| 12004 | * The code below removes functions that are not available on the |
| 12005 | * currently active platform. |
| 12006 | * |
| 12007 | * This block allow one to use a python binary that was build on |
| 12008 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 12009 | * OSX 10.4. |
| 12010 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12011 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12012 | if (fstatvfs == NULL) { |
| 12013 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 12014 | return NULL; |
| 12015 | } |
| 12016 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12017 | #endif /* HAVE_FSTATVFS */ |
| 12018 | |
| 12019 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12020 | if (statvfs == NULL) { |
| 12021 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 12022 | return NULL; |
| 12023 | } |
| 12024 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12025 | #endif /* HAVE_STATVFS */ |
| 12026 | |
| 12027 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12028 | if (lchown == NULL) { |
| 12029 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 12030 | return NULL; |
| 12031 | } |
| 12032 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12033 | #endif /* HAVE_LCHOWN */ |
| 12034 | |
| 12035 | |
| 12036 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12037 | |
| 12038 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 12039 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 12040 | billion = PyLong_FromLong(1000000000); |
| 12041 | if (!billion) |
| 12042 | return NULL; |
| 12043 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12044 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12045 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12046 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12047 | |
| 12048 | #ifdef __cplusplus |
| 12049 | } |
| 12050 | #endif |