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 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 401 | /* A helper used by a number of POSIX-only functions */ |
| 402 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 403 | static int |
| 404 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 405 | { |
| 406 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 407 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 408 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 409 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 410 | #endif |
| 411 | if (PyErr_Occurred()) |
| 412 | return 0; |
| 413 | return 1; |
| 414 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 415 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 416 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 417 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 418 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 419 | * valid and throw an assertion if it isn't. |
| 420 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 421 | * an assertion can be useful, but it does contradict the POSIX standard |
| 422 | * which for write(2) states: |
| 423 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 424 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 425 | * writing." |
| 426 | * Furthermore, python allows the user to enter any old integer |
| 427 | * as a fd and should merely raise a python exception on error. |
| 428 | * The Microsoft CRT doesn't provide an official way to check for the |
| 429 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 430 | * 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] | 431 | * internal structures involved. |
| 432 | * The structures below must be updated for each version of visual studio |
| 433 | * according to the file internal.h in the CRT source, until MS comes |
| 434 | * up with a less hacky way to do this. |
| 435 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 436 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 437 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 438 | /* The actual size of the structure is determined at runtime. |
| 439 | * Only the first items must be present. |
| 440 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 441 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 442 | intptr_t osfhnd; |
| 443 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 444 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 445 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 446 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 447 | #define IOINFO_L2E 5 |
| 448 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 449 | #define IOINFO_ARRAYS 64 |
| 450 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 451 | #define FOPEN 0x01 |
| 452 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 453 | |
| 454 | /* This function emulates what the windows CRT does to validate file handles */ |
| 455 | int |
| 456 | _PyVerify_fd(int fd) |
| 457 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 458 | const int i1 = fd >> IOINFO_L2E; |
| 459 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 461 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 462 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 463 | /* Determine the actual size of the ioinfo structure, |
| 464 | * as used by the CRT loaded in memory |
| 465 | */ |
| 466 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 467 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 468 | } |
| 469 | if (sizeof_ioinfo == 0) { |
| 470 | /* This should not happen... */ |
| 471 | goto fail; |
| 472 | } |
| 473 | |
| 474 | /* See that it isn't a special CLEAR fileno */ |
| 475 | if (fd != _NO_CONSOLE_FILENO) { |
| 476 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 477 | * we check pointer validity and other info |
| 478 | */ |
| 479 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 480 | /* finally, check that the file is open */ |
| 481 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 482 | if (info->osfile & FOPEN) { |
| 483 | return 1; |
| 484 | } |
| 485 | } |
| 486 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 487 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 488 | errno = EBADF; |
| 489 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 493 | static int |
| 494 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 495 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 496 | if (!_PyVerify_fd(fd1)) |
| 497 | return 0; |
| 498 | if (fd2 == _NO_CONSOLE_FILENO) |
| 499 | return 0; |
| 500 | if ((unsigned)fd2 < _NHANDLE_) |
| 501 | return 1; |
| 502 | else |
| 503 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 504 | } |
| 505 | #else |
| 506 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 507 | #define _PyVerify_fd_dup2(A, B) (1) |
| 508 | #endif |
| 509 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 510 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 511 | /* The following structure was copied from |
| 512 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 513 | include doesn't seem to be present in the Windows SDK (at least as included |
| 514 | with Visual Studio Express). */ |
| 515 | typedef struct _REPARSE_DATA_BUFFER { |
| 516 | ULONG ReparseTag; |
| 517 | USHORT ReparseDataLength; |
| 518 | USHORT Reserved; |
| 519 | union { |
| 520 | struct { |
| 521 | USHORT SubstituteNameOffset; |
| 522 | USHORT SubstituteNameLength; |
| 523 | USHORT PrintNameOffset; |
| 524 | USHORT PrintNameLength; |
| 525 | ULONG Flags; |
| 526 | WCHAR PathBuffer[1]; |
| 527 | } SymbolicLinkReparseBuffer; |
| 528 | |
| 529 | struct { |
| 530 | USHORT SubstituteNameOffset; |
| 531 | USHORT SubstituteNameLength; |
| 532 | USHORT PrintNameOffset; |
| 533 | USHORT PrintNameLength; |
| 534 | WCHAR PathBuffer[1]; |
| 535 | } MountPointReparseBuffer; |
| 536 | |
| 537 | struct { |
| 538 | UCHAR DataBuffer[1]; |
| 539 | } GenericReparseBuffer; |
| 540 | }; |
| 541 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 542 | |
| 543 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 544 | GenericReparseBuffer) |
| 545 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 546 | |
| 547 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 548 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 549 | { |
| 550 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 551 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 552 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 553 | |
| 554 | if (0 == DeviceIoControl( |
| 555 | reparse_point_handle, |
| 556 | FSCTL_GET_REPARSE_POINT, |
| 557 | NULL, 0, /* in buffer */ |
| 558 | target_buffer, sizeof(target_buffer), |
| 559 | &n_bytes_returned, |
| 560 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 561 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 562 | |
| 563 | if (reparse_tag) |
| 564 | *reparse_tag = rdb->ReparseTag; |
| 565 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 566 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 567 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 568 | |
| 569 | static int |
| 570 | win32_warn_bytes_api() |
| 571 | { |
| 572 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 573 | "The Windows bytes API has been deprecated, " |
| 574 | "use Unicode filenames instead", |
| 575 | 1); |
| 576 | } |
| 577 | |
| 578 | static PyObject* |
| 579 | win32_decode_filename(PyObject *obj) |
| 580 | { |
| 581 | PyObject *unicode; |
| 582 | if (PyUnicode_Check(obj)) { |
| 583 | if (PyUnicode_READY(obj)) |
| 584 | return NULL; |
| 585 | Py_INCREF(obj); |
| 586 | return obj; |
| 587 | } |
| 588 | if (!PyUnicode_FSDecoder(obj, &unicode)) |
| 589 | return NULL; |
| 590 | if (win32_warn_bytes_api()) { |
| 591 | Py_DECREF(unicode); |
| 592 | return NULL; |
| 593 | } |
| 594 | return unicode; |
| 595 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 596 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 597 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 598 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 599 | #ifdef WITH_NEXT_FRAMEWORK |
| 600 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 601 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 602 | */ |
| 603 | #include <crt_externs.h> |
| 604 | static char **environ; |
| 605 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 606 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 607 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 608 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 609 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 610 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 611 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 612 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 613 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 614 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 615 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 616 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 617 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 618 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 619 | APIRET rc; |
| 620 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 621 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 622 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 623 | d = PyDict_New(); |
| 624 | if (d == NULL) |
| 625 | return NULL; |
| 626 | #ifdef WITH_NEXT_FRAMEWORK |
| 627 | if (environ == NULL) |
| 628 | environ = *_NSGetEnviron(); |
| 629 | #endif |
| 630 | #ifdef MS_WINDOWS |
| 631 | /* _wenviron must be initialized in this way if the program is started |
| 632 | through main() instead of wmain(). */ |
| 633 | _wgetenv(L""); |
| 634 | if (_wenviron == NULL) |
| 635 | return d; |
| 636 | /* This part ignores errors */ |
| 637 | for (e = _wenviron; *e != NULL; e++) { |
| 638 | PyObject *k; |
| 639 | PyObject *v; |
| 640 | wchar_t *p = wcschr(*e, L'='); |
| 641 | if (p == NULL) |
| 642 | continue; |
| 643 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 644 | if (k == NULL) { |
| 645 | PyErr_Clear(); |
| 646 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 647 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 648 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 649 | if (v == NULL) { |
| 650 | PyErr_Clear(); |
| 651 | Py_DECREF(k); |
| 652 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 653 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 654 | if (PyDict_GetItem(d, k) == NULL) { |
| 655 | if (PyDict_SetItem(d, k, v) != 0) |
| 656 | PyErr_Clear(); |
| 657 | } |
| 658 | Py_DECREF(k); |
| 659 | Py_DECREF(v); |
| 660 | } |
| 661 | #else |
| 662 | if (environ == NULL) |
| 663 | return d; |
| 664 | /* This part ignores errors */ |
| 665 | for (e = environ; *e != NULL; e++) { |
| 666 | PyObject *k; |
| 667 | PyObject *v; |
| 668 | char *p = strchr(*e, '='); |
| 669 | if (p == NULL) |
| 670 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 671 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 672 | if (k == NULL) { |
| 673 | PyErr_Clear(); |
| 674 | continue; |
| 675 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 676 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 677 | if (v == NULL) { |
| 678 | PyErr_Clear(); |
| 679 | Py_DECREF(k); |
| 680 | continue; |
| 681 | } |
| 682 | if (PyDict_GetItem(d, k) == NULL) { |
| 683 | if (PyDict_SetItem(d, k, v) != 0) |
| 684 | PyErr_Clear(); |
| 685 | } |
| 686 | Py_DECREF(k); |
| 687 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 688 | } |
| 689 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 690 | #if defined(PYOS_OS2) |
| 691 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 692 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 693 | PyObject *v = PyBytes_FromString(buffer); |
| 694 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 695 | Py_DECREF(v); |
| 696 | } |
| 697 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 698 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 699 | PyObject *v = PyBytes_FromString(buffer); |
| 700 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 701 | Py_DECREF(v); |
| 702 | } |
| 703 | #endif |
| 704 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 707 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 708 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 709 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 710 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 711 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 712 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 713 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 714 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 715 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 716 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 717 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 720 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 721 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 722 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 723 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 724 | PyObject *name_str, *rc; |
| 725 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 726 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 727 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 728 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 729 | name_str); |
| 730 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 731 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 734 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 735 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 736 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 737 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 738 | /* XXX We should pass the function name along in the future. |
| 739 | (winreg.c also wants to pass the function name.) |
| 740 | This would however require an additional param to the |
| 741 | Windows error object, which is non-trivial. |
| 742 | */ |
| 743 | errno = GetLastError(); |
| 744 | if (filename) |
| 745 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 746 | else |
| 747 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 748 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 749 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 750 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 751 | win32_error_unicode(char* function, wchar_t* filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 752 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 753 | /* XXX - see win32_error for comments on 'function' */ |
| 754 | errno = GetLastError(); |
| 755 | if (filename) |
| 756 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 757 | else |
| 758 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 761 | static PyObject * |
| 762 | win32_error_object(char* function, PyObject* filename) |
| 763 | { |
| 764 | /* XXX - see win32_error for comments on 'function' */ |
| 765 | errno = GetLastError(); |
| 766 | if (filename) |
| 767 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 768 | PyExc_WindowsError, |
| 769 | errno, |
| 770 | filename); |
| 771 | else |
| 772 | return PyErr_SetFromWindowsErr(errno); |
| 773 | } |
| 774 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 775 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 776 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 777 | #if defined(PYOS_OS2) |
| 778 | /********************************************************************** |
| 779 | * Helper Function to Trim and Format OS/2 Messages |
| 780 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 781 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 782 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 783 | { |
| 784 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 785 | |
| 786 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 787 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 788 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 789 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 790 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 791 | } |
| 792 | |
| 793 | /* Add Optional Reason Text */ |
| 794 | if (reason) { |
| 795 | strcat(msgbuf, " : "); |
| 796 | strcat(msgbuf, reason); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /********************************************************************** |
| 801 | * Decode an OS/2 Operating System Error Code |
| 802 | * |
| 803 | * A convenience function to lookup an OS/2 error code and return a |
| 804 | * text message we can use to raise a Python exception. |
| 805 | * |
| 806 | * Notes: |
| 807 | * The messages for errors returned from the OS/2 kernel reside in |
| 808 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 809 | * |
| 810 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 811 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 812 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 813 | { |
| 814 | APIRET rc; |
| 815 | ULONG msglen; |
| 816 | |
| 817 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 818 | Py_BEGIN_ALLOW_THREADS |
| 819 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 820 | errorcode, "oso001.msg", &msglen); |
| 821 | Py_END_ALLOW_THREADS |
| 822 | |
| 823 | if (rc == NO_ERROR) |
| 824 | os2_formatmsg(msgbuf, msglen, reason); |
| 825 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 826 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 827 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 828 | |
| 829 | return msgbuf; |
| 830 | } |
| 831 | |
| 832 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 833 | errors are not in a global variable e.g. 'errno' nor are |
| 834 | they congruent with posix error numbers. */ |
| 835 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 836 | static PyObject * |
| 837 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 838 | { |
| 839 | char text[1024]; |
| 840 | PyObject *v; |
| 841 | |
| 842 | os2_strerror(text, sizeof(text), code, ""); |
| 843 | |
| 844 | v = Py_BuildValue("(is)", code, text); |
| 845 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 846 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 847 | Py_DECREF(v); |
| 848 | } |
| 849 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 850 | } |
| 851 | |
| 852 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 853 | |
| 854 | /* POSIX generic methods */ |
| 855 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 856 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 857 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 858 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 859 | int fd; |
| 860 | int res; |
| 861 | fd = PyObject_AsFileDescriptor(fdobj); |
| 862 | if (fd < 0) |
| 863 | return NULL; |
| 864 | if (!_PyVerify_fd(fd)) |
| 865 | return posix_error(); |
| 866 | Py_BEGIN_ALLOW_THREADS |
| 867 | res = (*func)(fd); |
| 868 | Py_END_ALLOW_THREADS |
| 869 | if (res < 0) |
| 870 | return posix_error(); |
| 871 | Py_INCREF(Py_None); |
| 872 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 873 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 874 | |
| 875 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 876 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 877 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 878 | PyObject *opath1 = NULL; |
| 879 | char *path1; |
| 880 | int res; |
| 881 | if (!PyArg_ParseTuple(args, format, |
| 882 | PyUnicode_FSConverter, &opath1)) |
| 883 | return NULL; |
| 884 | path1 = PyBytes_AsString(opath1); |
| 885 | Py_BEGIN_ALLOW_THREADS |
| 886 | res = (*func)(path1); |
| 887 | Py_END_ALLOW_THREADS |
| 888 | if (res < 0) |
| 889 | return posix_error_with_allocated_filename(opath1); |
| 890 | Py_DECREF(opath1); |
| 891 | Py_INCREF(Py_None); |
| 892 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 895 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 896 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 897 | char *format, |
| 898 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 899 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 900 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 901 | char *path1, *path2; |
| 902 | int res; |
| 903 | if (!PyArg_ParseTuple(args, format, |
| 904 | PyUnicode_FSConverter, &opath1, |
| 905 | PyUnicode_FSConverter, &opath2)) { |
| 906 | return NULL; |
| 907 | } |
| 908 | path1 = PyBytes_AsString(opath1); |
| 909 | path2 = PyBytes_AsString(opath2); |
| 910 | Py_BEGIN_ALLOW_THREADS |
| 911 | res = (*func)(path1, path2); |
| 912 | Py_END_ALLOW_THREADS |
| 913 | Py_DECREF(opath1); |
| 914 | Py_DECREF(opath2); |
| 915 | if (res != 0) |
| 916 | /* XXX how to report both path1 and path2??? */ |
| 917 | return posix_error(); |
| 918 | Py_INCREF(Py_None); |
| 919 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 922 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 923 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 924 | win32_1str(PyObject* args, char* func, |
| 925 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 926 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 927 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 928 | PyObject *uni; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 929 | const char *ansi; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 930 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 931 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 932 | if (PyArg_ParseTuple(args, wformat, &uni)) |
| 933 | { |
| 934 | wchar_t *wstr = PyUnicode_AsUnicode(uni); |
| 935 | if (wstr == NULL) |
| 936 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 937 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 938 | result = funcW(wstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 939 | Py_END_ALLOW_THREADS |
| 940 | if (!result) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 941 | return win32_error_object(func, uni); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 942 | Py_INCREF(Py_None); |
| 943 | return Py_None; |
| 944 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 945 | PyErr_Clear(); |
| 946 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 947 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 948 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 949 | if (win32_warn_bytes_api()) |
| 950 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 951 | Py_BEGIN_ALLOW_THREADS |
| 952 | result = funcA(ansi); |
| 953 | Py_END_ALLOW_THREADS |
| 954 | if (!result) |
| 955 | return win32_error(func, ansi); |
| 956 | Py_INCREF(Py_None); |
| 957 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 958 | |
| 959 | } |
| 960 | |
| 961 | /* This is a reimplementation of the C library's chdir function, |
| 962 | but one that produces Win32 errors instead of DOS error codes. |
| 963 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 964 | it also needs to set "magic" environment variables indicating |
| 965 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 966 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 967 | win32_chdir(LPCSTR path) |
| 968 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 969 | char new_path[MAX_PATH+1]; |
| 970 | int result; |
| 971 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 972 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 973 | if(!SetCurrentDirectoryA(path)) |
| 974 | return FALSE; |
| 975 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 976 | if (!result) |
| 977 | return FALSE; |
| 978 | /* In the ANSI API, there should not be any paths longer |
| 979 | than MAX_PATH. */ |
| 980 | assert(result <= MAX_PATH+1); |
| 981 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 982 | strncmp(new_path, "//", 2) == 0) |
| 983 | /* UNC path, nothing to do. */ |
| 984 | return TRUE; |
| 985 | env[1] = new_path[0]; |
| 986 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* The Unicode version differs from the ANSI version |
| 990 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 991 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 992 | win32_wchdir(LPCWSTR path) |
| 993 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 994 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 995 | int result; |
| 996 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 997 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 998 | if(!SetCurrentDirectoryW(path)) |
| 999 | return FALSE; |
| 1000 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 1001 | if (!result) |
| 1002 | return FALSE; |
| 1003 | if (result > MAX_PATH+1) { |
| 1004 | new_path = malloc(result * sizeof(wchar_t)); |
| 1005 | if (!new_path) { |
| 1006 | SetLastError(ERROR_OUTOFMEMORY); |
| 1007 | return FALSE; |
| 1008 | } |
| 1009 | result = GetCurrentDirectoryW(result, new_path); |
| 1010 | if (!result) { |
| 1011 | free(new_path); |
| 1012 | return FALSE; |
| 1013 | } |
| 1014 | } |
| 1015 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1016 | wcsncmp(new_path, L"//", 2) == 0) |
| 1017 | /* UNC path, nothing to do. */ |
| 1018 | return TRUE; |
| 1019 | env[1] = new_path[0]; |
| 1020 | result = SetEnvironmentVariableW(env, new_path); |
| 1021 | if (new_path != _new_path) |
| 1022 | free(new_path); |
| 1023 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1024 | } |
| 1025 | #endif |
| 1026 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1027 | #ifdef MS_WINDOWS |
| 1028 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1029 | - time stamps are restricted to second resolution |
| 1030 | - file modification times suffer from forth-and-back conversions between |
| 1031 | UTC and local time |
| 1032 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1033 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1034 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1035 | |
| 1036 | struct win32_stat{ |
| 1037 | int st_dev; |
| 1038 | __int64 st_ino; |
| 1039 | unsigned short st_mode; |
| 1040 | int st_nlink; |
| 1041 | int st_uid; |
| 1042 | int st_gid; |
| 1043 | int st_rdev; |
| 1044 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1045 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1046 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1047 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1048 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1049 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1050 | int st_ctime_nsec; |
| 1051 | }; |
| 1052 | |
| 1053 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1054 | |
| 1055 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1056 | 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] | 1057 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1058 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1059 | /* Cannot simply cast and dereference in_ptr, |
| 1060 | since it might not be aligned properly */ |
| 1061 | __int64 in; |
| 1062 | memcpy(&in, in_ptr, sizeof(in)); |
| 1063 | *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] | 1064 | *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] | 1065 | } |
| 1066 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1067 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1068 | 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] | 1069 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1070 | /* XXX endianness */ |
| 1071 | __int64 out; |
| 1072 | out = time_in + secs_between_epochs; |
| 1073 | out = out * 10000000 + nsec_in / 100; |
| 1074 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1077 | /* Below, we *know* that ugo+r is 0444 */ |
| 1078 | #if _S_IREAD != 0400 |
| 1079 | #error Unsupported C library |
| 1080 | #endif |
| 1081 | static int |
| 1082 | attributes_to_mode(DWORD attr) |
| 1083 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1084 | int m = 0; |
| 1085 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1086 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1087 | else |
| 1088 | m |= _S_IFREG; |
| 1089 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1090 | m |= 0444; |
| 1091 | else |
| 1092 | m |= 0666; |
| 1093 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1097 | 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] | 1098 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1099 | memset(result, 0, sizeof(*result)); |
| 1100 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1101 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1102 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1103 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1104 | 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] | 1105 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1106 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1107 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1108 | /* first clear the S_IFMT bits */ |
| 1109 | result->st_mode ^= (result->st_mode & 0170000); |
| 1110 | /* now set the bits that make this a symlink */ |
| 1111 | result->st_mode |= 0120000; |
| 1112 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1114 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1117 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1118 | 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] | 1119 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1120 | HANDLE hFindFile; |
| 1121 | WIN32_FIND_DATAA FileData; |
| 1122 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1123 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1124 | return FALSE; |
| 1125 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1126 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1127 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1128 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1129 | info->ftCreationTime = FileData.ftCreationTime; |
| 1130 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1131 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1132 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1133 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1134 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1135 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1136 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1137 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1141 | 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] | 1142 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1143 | HANDLE hFindFile; |
| 1144 | WIN32_FIND_DATAW FileData; |
| 1145 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1146 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1147 | return FALSE; |
| 1148 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1149 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1150 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1151 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1152 | info->ftCreationTime = FileData.ftCreationTime; |
| 1153 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1154 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1155 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1156 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1157 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1158 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1159 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1160 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1163 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1164 | static int has_GetFinalPathNameByHandle = 0; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1165 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1166 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1167 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1168 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1169 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1170 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1171 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1172 | DWORD); |
| 1173 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1174 | /* only recheck */ |
| 1175 | if (!has_GetFinalPathNameByHandle) |
| 1176 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1177 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1178 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1179 | "GetFinalPathNameByHandleA"); |
| 1180 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1181 | "GetFinalPathNameByHandleW"); |
| 1182 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1183 | Py_GetFinalPathNameByHandleW; |
| 1184 | } |
| 1185 | return has_GetFinalPathNameByHandle; |
| 1186 | } |
| 1187 | |
| 1188 | static BOOL |
| 1189 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1190 | { |
| 1191 | int buf_size, result_length; |
| 1192 | wchar_t *buf; |
| 1193 | |
| 1194 | /* We have a good handle to the target, use it to determine |
| 1195 | the target path name (then we'll call lstat on it). */ |
| 1196 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1197 | VOLUME_NAME_DOS); |
| 1198 | if(!buf_size) |
| 1199 | return FALSE; |
| 1200 | |
| 1201 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1202 | if (!buf) { |
| 1203 | SetLastError(ERROR_OUTOFMEMORY); |
| 1204 | return FALSE; |
| 1205 | } |
| 1206 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1207 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1208 | buf, buf_size, VOLUME_NAME_DOS); |
| 1209 | |
| 1210 | if(!result_length) { |
| 1211 | free(buf); |
| 1212 | return FALSE; |
| 1213 | } |
| 1214 | |
| 1215 | if(!CloseHandle(hdl)) { |
| 1216 | free(buf); |
| 1217 | return FALSE; |
| 1218 | } |
| 1219 | |
| 1220 | buf[result_length] = 0; |
| 1221 | |
| 1222 | *target_path = buf; |
| 1223 | return TRUE; |
| 1224 | } |
| 1225 | |
| 1226 | static int |
| 1227 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1228 | BOOL traverse); |
| 1229 | static int |
| 1230 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1231 | BOOL traverse) |
| 1232 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1233 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1234 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1235 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1236 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1237 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1238 | const char *dot; |
| 1239 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1240 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1241 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1242 | traverse reparse point. */ |
| 1243 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1246 | hFile = CreateFileA( |
| 1247 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1248 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1249 | 0, /* share mode */ |
| 1250 | NULL, /* security attributes */ |
| 1251 | OPEN_EXISTING, |
| 1252 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1253 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1254 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1255 | the symlink path agin and not the actual final path. */ |
| 1256 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1257 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1258 | NULL); |
| 1259 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1260 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1261 | /* Either the target doesn't exist, or we don't have access to |
| 1262 | get a handle to it. If the former, we need to return an error. |
| 1263 | If the latter, we can use attributes_from_dir. */ |
| 1264 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1265 | return -1; |
| 1266 | /* Could not get attributes on open file. Fall back to |
| 1267 | reading the directory. */ |
| 1268 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1269 | /* Very strange. This should not fail now */ |
| 1270 | return -1; |
| 1271 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1272 | if (traverse) { |
| 1273 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1274 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1275 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1276 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1277 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1278 | } else { |
| 1279 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1280 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1281 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1282 | } |
| 1283 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1284 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1285 | return -1; |
| 1286 | |
| 1287 | /* Close the outer open file handle now that we're about to |
| 1288 | reopen it with different flags. */ |
| 1289 | if (!CloseHandle(hFile)) |
| 1290 | return -1; |
| 1291 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1292 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1293 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1294 | the file without the reparse handling flag set. */ |
| 1295 | hFile2 = CreateFileA( |
| 1296 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1297 | NULL, OPEN_EXISTING, |
| 1298 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1299 | NULL); |
| 1300 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1301 | return -1; |
| 1302 | |
| 1303 | if (!get_target_path(hFile2, &target_path)) |
| 1304 | return -1; |
| 1305 | |
| 1306 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1307 | free(target_path); |
| 1308 | return code; |
| 1309 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1310 | } else |
| 1311 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1312 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1313 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1314 | |
| 1315 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1316 | dot = strrchr(path, '.'); |
| 1317 | if (dot) { |
| 1318 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1319 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1320 | result->st_mode |= 0111; |
| 1321 | } |
| 1322 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1326 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1327 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1328 | { |
| 1329 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1330 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1331 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1332 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1333 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1334 | const wchar_t *dot; |
| 1335 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1336 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1337 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1338 | traverse reparse point. */ |
| 1339 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1342 | hFile = CreateFileW( |
| 1343 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1344 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1345 | 0, /* share mode */ |
| 1346 | NULL, /* security attributes */ |
| 1347 | OPEN_EXISTING, |
| 1348 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1349 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1350 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1351 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1352 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1353 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1354 | NULL); |
| 1355 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1356 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1357 | /* Either the target doesn't exist, or we don't have access to |
| 1358 | get a handle to it. If the former, we need to return an error. |
| 1359 | If the latter, we can use attributes_from_dir. */ |
| 1360 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1361 | return -1; |
| 1362 | /* Could not get attributes on open file. Fall back to |
| 1363 | reading the directory. */ |
| 1364 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1365 | /* Very strange. This should not fail now */ |
| 1366 | return -1; |
| 1367 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1368 | if (traverse) { |
| 1369 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1370 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1371 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1372 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1373 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1374 | } else { |
| 1375 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1376 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1377 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1378 | } |
| 1379 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1380 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1381 | return -1; |
| 1382 | |
| 1383 | /* Close the outer open file handle now that we're about to |
| 1384 | reopen it with different flags. */ |
| 1385 | if (!CloseHandle(hFile)) |
| 1386 | return -1; |
| 1387 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1388 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1389 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1390 | the file without the reparse handling flag set. */ |
| 1391 | hFile2 = CreateFileW( |
| 1392 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1393 | NULL, OPEN_EXISTING, |
| 1394 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1395 | NULL); |
| 1396 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1397 | return -1; |
| 1398 | |
| 1399 | if (!get_target_path(hFile2, &target_path)) |
| 1400 | return -1; |
| 1401 | |
| 1402 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1403 | free(target_path); |
| 1404 | return code; |
| 1405 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1406 | } else |
| 1407 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1408 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1409 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1410 | |
| 1411 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1412 | dot = wcsrchr(path, '.'); |
| 1413 | if (dot) { |
| 1414 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1415 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1416 | result->st_mode |= 0111; |
| 1417 | } |
| 1418 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1422 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1423 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1424 | /* Protocol violation: we explicitly clear errno, instead of |
| 1425 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1426 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1427 | errno = 0; |
| 1428 | return code; |
| 1429 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1430 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1431 | static int |
| 1432 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1433 | { |
| 1434 | /* Protocol violation: we explicitly clear errno, instead of |
| 1435 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1436 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1437 | errno = 0; |
| 1438 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1439 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1440 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1441 | |
| 1442 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1443 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1444 | default does not traverse symlinks and instead returns attributes for |
| 1445 | the symlink. |
| 1446 | |
| 1447 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1448 | win32_stat will first explicitly resolve the symlink target and then will |
| 1449 | call win32_lstat on that result. |
| 1450 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1451 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1452 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1453 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1454 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1455 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1456 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1459 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1460 | 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] | 1461 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1462 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | static int |
| 1466 | win32_stat(const char* path, struct win32_stat *result) |
| 1467 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1468 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1471 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1472 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1473 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1474 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | static int |
| 1478 | win32_fstat(int file_number, struct win32_stat *result) |
| 1479 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1480 | BY_HANDLE_FILE_INFORMATION info; |
| 1481 | HANDLE h; |
| 1482 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1483 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1484 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1485 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1486 | /* Protocol violation: we explicitly clear errno, instead of |
| 1487 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1488 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1489 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1490 | if (h == INVALID_HANDLE_VALUE) { |
| 1491 | /* This is really a C library error (invalid file handle). |
| 1492 | We set the Win32 error to the closes one matching. */ |
| 1493 | SetLastError(ERROR_INVALID_HANDLE); |
| 1494 | return -1; |
| 1495 | } |
| 1496 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1497 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1498 | type = GetFileType(h); |
| 1499 | if (type == FILE_TYPE_UNKNOWN) { |
| 1500 | DWORD error = GetLastError(); |
| 1501 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1502 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1503 | } |
| 1504 | /* else: valid but unknown file */ |
| 1505 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1506 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1507 | if (type != FILE_TYPE_DISK) { |
| 1508 | if (type == FILE_TYPE_CHAR) |
| 1509 | result->st_mode = _S_IFCHR; |
| 1510 | else if (type == FILE_TYPE_PIPE) |
| 1511 | result->st_mode = _S_IFIFO; |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | if (!GetFileInformationByHandle(h, &info)) { |
| 1516 | return -1; |
| 1517 | } |
| 1518 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1519 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1520 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1521 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1522 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | #endif /* MS_WINDOWS */ |
| 1526 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1527 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1528 | "stat_result: Result from stat or lstat.\n\n\ |
| 1529 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1530 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1531 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1532 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1533 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1534 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1535 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1536 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1537 | |
| 1538 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1539 | {"st_mode", "protection bits"}, |
| 1540 | {"st_ino", "inode"}, |
| 1541 | {"st_dev", "device"}, |
| 1542 | {"st_nlink", "number of hard links"}, |
| 1543 | {"st_uid", "user ID of owner"}, |
| 1544 | {"st_gid", "group ID of owner"}, |
| 1545 | {"st_size", "total size, in bytes"}, |
| 1546 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1547 | {NULL, "integer time of last access"}, |
| 1548 | {NULL, "integer time of last modification"}, |
| 1549 | {NULL, "integer time of last change"}, |
| 1550 | {"st_atime", "time of last access"}, |
| 1551 | {"st_mtime", "time of last modification"}, |
| 1552 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1553 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1554 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1555 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1556 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1557 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1558 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1559 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1560 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1561 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1562 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1563 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1564 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1565 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1566 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1567 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1568 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1569 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1570 | #endif |
| 1571 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1572 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1573 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1574 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1575 | }; |
| 1576 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1577 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1578 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1579 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1580 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1581 | #endif |
| 1582 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1583 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1584 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1585 | #else |
| 1586 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1587 | #endif |
| 1588 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1589 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1590 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1591 | #else |
| 1592 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1593 | #endif |
| 1594 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1595 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1596 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1597 | #else |
| 1598 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1599 | #endif |
| 1600 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1601 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1602 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1603 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1604 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1605 | #endif |
| 1606 | |
| 1607 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1608 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1609 | #else |
| 1610 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1611 | #endif |
| 1612 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1613 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1614 | "stat_result", /* name */ |
| 1615 | stat_result__doc__, /* doc */ |
| 1616 | stat_result_fields, |
| 1617 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1618 | }; |
| 1619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1620 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1621 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1622 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1623 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1624 | 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] | 1625 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1626 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1627 | |
| 1628 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1629 | {"f_bsize", }, |
| 1630 | {"f_frsize", }, |
| 1631 | {"f_blocks", }, |
| 1632 | {"f_bfree", }, |
| 1633 | {"f_bavail", }, |
| 1634 | {"f_files", }, |
| 1635 | {"f_ffree", }, |
| 1636 | {"f_favail", }, |
| 1637 | {"f_flag", }, |
| 1638 | {"f_namemax",}, |
| 1639 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1640 | }; |
| 1641 | |
| 1642 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1643 | "statvfs_result", /* name */ |
| 1644 | statvfs_result__doc__, /* doc */ |
| 1645 | statvfs_result_fields, |
| 1646 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1647 | }; |
| 1648 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1649 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1650 | PyDoc_STRVAR(waitid_result__doc__, |
| 1651 | "waitid_result: Result from waitid.\n\n\ |
| 1652 | This object may be accessed either as a tuple of\n\ |
| 1653 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1654 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1655 | \n\ |
| 1656 | See os.waitid for more information."); |
| 1657 | |
| 1658 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1659 | {"si_pid", }, |
| 1660 | {"si_uid", }, |
| 1661 | {"si_signo", }, |
| 1662 | {"si_status", }, |
| 1663 | {"si_code", }, |
| 1664 | {0} |
| 1665 | }; |
| 1666 | |
| 1667 | static PyStructSequence_Desc waitid_result_desc = { |
| 1668 | "waitid_result", /* name */ |
| 1669 | waitid_result__doc__, /* doc */ |
| 1670 | waitid_result_fields, |
| 1671 | 5 |
| 1672 | }; |
| 1673 | static PyTypeObject WaitidResultType; |
| 1674 | #endif |
| 1675 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1676 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1677 | static PyTypeObject StatResultType; |
| 1678 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1679 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1680 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1681 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1682 | static newfunc structseq_new; |
| 1683 | |
| 1684 | static PyObject * |
| 1685 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1686 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1687 | PyStructSequence *result; |
| 1688 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1689 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1690 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1691 | if (!result) |
| 1692 | return NULL; |
| 1693 | /* If we have been initialized from a tuple, |
| 1694 | st_?time might be set to None. Initialize it |
| 1695 | from the int slots. */ |
| 1696 | for (i = 7; i <= 9; i++) { |
| 1697 | if (result->ob_item[i+3] == Py_None) { |
| 1698 | Py_DECREF(Py_None); |
| 1699 | Py_INCREF(result->ob_item[i]); |
| 1700 | result->ob_item[i+3] = result->ob_item[i]; |
| 1701 | } |
| 1702 | } |
| 1703 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1709 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1710 | |
| 1711 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1712 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1713 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1714 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1715 | future calls return ints. \n\ |
| 1716 | If newval is omitted, return the current setting.\n"); |
| 1717 | |
| 1718 | static PyObject* |
| 1719 | stat_float_times(PyObject* self, PyObject *args) |
| 1720 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1721 | int newval = -1; |
| 1722 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1723 | return NULL; |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 1724 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1725 | "stat_float_times() is deprecated", |
| 1726 | 1)) |
| 1727 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1728 | if (newval == -1) |
| 1729 | /* Return old value */ |
| 1730 | return PyBool_FromLong(_stat_float_times); |
| 1731 | _stat_float_times = newval; |
| 1732 | Py_INCREF(Py_None); |
| 1733 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1734 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1735 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1736 | static PyObject *billion = NULL; |
| 1737 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1738 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1739 | 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] | 1740 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1741 | PyObject *s = _PyLong_FromTime_t(sec); |
| 1742 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 1743 | PyObject *s_in_ns = NULL; |
| 1744 | PyObject *ns_total = NULL; |
| 1745 | PyObject *float_s = NULL; |
| 1746 | |
| 1747 | if (!(s && ns_fractional)) |
| 1748 | goto exit; |
| 1749 | |
| 1750 | s_in_ns = PyNumber_Multiply(s, billion); |
| 1751 | if (!s_in_ns) |
| 1752 | goto exit; |
| 1753 | |
| 1754 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 1755 | if (!ns_total) |
| 1756 | goto exit; |
| 1757 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1758 | if (_stat_float_times) { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1759 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1760 | if (!float_s) |
| 1761 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1762 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1763 | else { |
| 1764 | float_s = s; |
| 1765 | Py_INCREF(float_s); |
| 1766 | } |
| 1767 | |
| 1768 | PyStructSequence_SET_ITEM(v, index, s); |
| 1769 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 1770 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 1771 | s = NULL; |
| 1772 | float_s = NULL; |
| 1773 | ns_total = NULL; |
| 1774 | exit: |
| 1775 | Py_XDECREF(s); |
| 1776 | Py_XDECREF(ns_fractional); |
| 1777 | Py_XDECREF(s_in_ns); |
| 1778 | Py_XDECREF(ns_total); |
| 1779 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1782 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1783 | (used by posix_stat() and posix_fstat()) */ |
| 1784 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1785 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1786 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1787 | unsigned long ansec, mnsec, cnsec; |
| 1788 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1789 | if (v == NULL) |
| 1790 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1791 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1792 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1793 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1794 | PyStructSequence_SET_ITEM(v, 1, |
| 1795 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1796 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1797 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1798 | #endif |
| 1799 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1800 | PyStructSequence_SET_ITEM(v, 2, |
| 1801 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1802 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1803 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1804 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1805 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1806 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1807 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1808 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1809 | PyStructSequence_SET_ITEM(v, 6, |
| 1810 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1811 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1812 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1813 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1814 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1815 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1816 | ansec = st->st_atim.tv_nsec; |
| 1817 | mnsec = st->st_mtim.tv_nsec; |
| 1818 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1819 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1820 | ansec = st->st_atimespec.tv_nsec; |
| 1821 | mnsec = st->st_mtimespec.tv_nsec; |
| 1822 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1823 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1824 | ansec = st->st_atime_nsec; |
| 1825 | mnsec = st->st_mtime_nsec; |
| 1826 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1827 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1828 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1829 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1830 | fill_time(v, 7, st->st_atime, ansec); |
| 1831 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1832 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1833 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1834 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1835 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1836 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1837 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1838 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1839 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1840 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1841 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1842 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1843 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1844 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1845 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1846 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1847 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1848 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1849 | #endif |
| 1850 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1851 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1852 | PyObject *val; |
| 1853 | unsigned long bsec,bnsec; |
| 1854 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1855 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1856 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1857 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1858 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1859 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1860 | if (_stat_float_times) { |
| 1861 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1862 | } else { |
| 1863 | val = PyLong_FromLong((long)bsec); |
| 1864 | } |
| 1865 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1866 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1867 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1868 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1869 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1870 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1871 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1872 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1873 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1874 | if (PyErr_Occurred()) { |
| 1875 | Py_DECREF(v); |
| 1876 | return NULL; |
| 1877 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1878 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1879 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1882 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1883 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1884 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1885 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1886 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1887 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1888 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1889 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1890 | char *wformat, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1891 | int (*wstatfunc)(const wchar_t *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1892 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1893 | STRUCT_STAT st; |
| 1894 | PyObject *opath; |
| 1895 | char *path; |
| 1896 | int res; |
| 1897 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1898 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1899 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1900 | PyObject *po; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1901 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1902 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 1903 | if (wpath == NULL) |
| 1904 | return NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1905 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1906 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1907 | res = wstatfunc(wpath, &st); |
| 1908 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1909 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1910 | if (res != 0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1911 | return win32_error_object("stat", po); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1912 | return _pystat_fromstructstat(&st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1913 | } |
| 1914 | /* Drop the argument parsing error as narrow strings |
| 1915 | are also valid. */ |
| 1916 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1917 | #endif |
| 1918 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1919 | if (!PyArg_ParseTuple(args, format, |
| 1920 | PyUnicode_FSConverter, &opath)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1921 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1922 | #ifdef MS_WINDOWS |
| 1923 | if (win32_warn_bytes_api()) { |
| 1924 | Py_DECREF(opath); |
| 1925 | return NULL; |
| 1926 | } |
| 1927 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1928 | path = PyBytes_AsString(opath); |
| 1929 | Py_BEGIN_ALLOW_THREADS |
| 1930 | res = (*statfunc)(path, &st); |
| 1931 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1933 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1934 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1935 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1936 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1937 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1938 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1939 | } |
| 1940 | else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1941 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1942 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1943 | Py_DECREF(opath); |
| 1944 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1947 | /* POSIX methods */ |
| 1948 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1949 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1950 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1951 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1952 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1953 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1954 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1955 | 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] | 1956 | |
| 1957 | static PyObject * |
Benjamin Peterson | eaa5eee | 2012-05-05 21:22:14 -0400 | [diff] [blame] | 1958 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1959 | { |
Benjamin Peterson | eaa5eee | 2012-05-05 21:22:14 -0400 | [diff] [blame] | 1960 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1961 | int mode; |
| 1962 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1963 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1964 | DWORD attr; |
Benjamin Peterson | eaa5eee | 2012-05-05 21:22:14 -0400 | [diff] [blame] | 1965 | PyObject *po; |
| 1966 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1967 | wchar_t* wpath = PyUnicode_AsUnicode(po); |
| 1968 | if (wpath == NULL) |
| 1969 | return NULL; |
| 1970 | Py_BEGIN_ALLOW_THREADS |
| 1971 | attr = GetFileAttributesW(wpath); |
| 1972 | Py_END_ALLOW_THREADS |
| 1973 | goto finish; |
| 1974 | } |
| 1975 | /* Drop the argument parsing error as narrow strings |
| 1976 | are also valid. */ |
| 1977 | PyErr_Clear(); |
| 1978 | if (!PyArg_ParseTuple(args, "yi:access", &path, &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1979 | return NULL; |
Benjamin Peterson | eaa5eee | 2012-05-05 21:22:14 -0400 | [diff] [blame] | 1980 | if (win32_warn_bytes_api()) |
| 1981 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1982 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | eaa5eee | 2012-05-05 21:22:14 -0400 | [diff] [blame] | 1983 | attr = GetFileAttributesA(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1984 | Py_END_ALLOW_THREADS |
Benjamin Peterson | eaa5eee | 2012-05-05 21:22:14 -0400 | [diff] [blame] | 1985 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1986 | if (attr == 0xFFFFFFFF) |
| 1987 | /* File does not exist, or cannot read attributes */ |
| 1988 | return PyBool_FromLong(0); |
| 1989 | /* Access is possible if either write access wasn't requested, or |
| 1990 | the file isn't read-only, or if it's a directory, as there are |
| 1991 | no read-only directories on Windows. */ |
| 1992 | return PyBool_FromLong(!(mode & 2) |
| 1993 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1994 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1995 | #else |
Benjamin Peterson | eaa5eee | 2012-05-05 21:22:14 -0400 | [diff] [blame] | 1996 | PyObject *opath; |
| 1997 | int res; |
| 1998 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1999 | PyUnicode_FSConverter, &opath, &mode)) |
| 2000 | return NULL; |
| 2001 | path = PyBytes_AsString(opath); |
| 2002 | Py_BEGIN_ALLOW_THREADS |
| 2003 | res = access(path, mode); |
| 2004 | Py_END_ALLOW_THREADS |
| 2005 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2006 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2007 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2010 | #ifndef F_OK |
| 2011 | #define F_OK 0 |
| 2012 | #endif |
| 2013 | #ifndef R_OK |
| 2014 | #define R_OK 4 |
| 2015 | #endif |
| 2016 | #ifndef W_OK |
| 2017 | #define W_OK 2 |
| 2018 | #endif |
| 2019 | #ifndef X_OK |
| 2020 | #define X_OK 1 |
| 2021 | #endif |
| 2022 | |
| 2023 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2024 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2025 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2026 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2027 | |
| 2028 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2029 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2030 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2031 | int id; |
| 2032 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2034 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 2035 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2036 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2037 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2038 | /* file descriptor 0 only, the default input device (stdin) */ |
| 2039 | if (id == 0) { |
| 2040 | ret = ttyname(); |
| 2041 | } |
| 2042 | else { |
| 2043 | ret = NULL; |
| 2044 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2045 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2046 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2047 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2048 | if (ret == NULL) |
| 2049 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2050 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2051 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2052 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2053 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2054 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2055 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2056 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2057 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2058 | |
| 2059 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2060 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2061 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2062 | char *ret; |
| 2063 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2064 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2065 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2066 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2067 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2068 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2069 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2070 | if (ret == NULL) |
| 2071 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2072 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2073 | } |
| 2074 | #endif |
| 2075 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2076 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2077 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2078 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2079 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2080 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2081 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2082 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2083 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2084 | 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] | 2085 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2086 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 2087 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2088 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2089 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2090 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2091 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2094 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2095 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2096 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2097 | 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] | 2098 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2099 | |
| 2100 | static PyObject * |
| 2101 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2102 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2103 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2104 | } |
| 2105 | #endif /* HAVE_FCHDIR */ |
| 2106 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2108 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2109 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2110 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2111 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2112 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2113 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2114 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2115 | PyObject *opath = NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2116 | const char *path = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2117 | int i; |
| 2118 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2119 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2120 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2121 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2122 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2123 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 2124 | if (wpath == NULL) |
| 2125 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2126 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2127 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2128 | if (attr != 0xFFFFFFFF) { |
| 2129 | if (i & _S_IWRITE) |
| 2130 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2131 | else |
| 2132 | attr |= FILE_ATTRIBUTE_READONLY; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2133 | res = SetFileAttributesW(wpath, attr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2134 | } |
| 2135 | else |
| 2136 | res = 0; |
| 2137 | Py_END_ALLOW_THREADS |
| 2138 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2139 | return win32_error_object("chmod", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2140 | Py_INCREF(Py_None); |
| 2141 | return Py_None; |
| 2142 | } |
| 2143 | /* Drop the argument parsing error as narrow strings |
| 2144 | are also valid. */ |
| 2145 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2146 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2147 | if (!PyArg_ParseTuple(args, "yi:chmod", &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2148 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2149 | if (win32_warn_bytes_api()) |
| 2150 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2151 | Py_BEGIN_ALLOW_THREADS |
| 2152 | attr = GetFileAttributesA(path); |
| 2153 | if (attr != 0xFFFFFFFF) { |
| 2154 | if (i & _S_IWRITE) |
| 2155 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2156 | else |
| 2157 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2158 | res = SetFileAttributesA(path, attr); |
| 2159 | } |
| 2160 | else |
| 2161 | res = 0; |
| 2162 | Py_END_ALLOW_THREADS |
| 2163 | if (!res) { |
| 2164 | win32_error("chmod", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2165 | return NULL; |
| 2166 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2167 | Py_INCREF(Py_None); |
| 2168 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2169 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2170 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2171 | &opath, &i)) |
| 2172 | return NULL; |
| 2173 | path = PyBytes_AsString(opath); |
| 2174 | Py_BEGIN_ALLOW_THREADS |
| 2175 | res = chmod(path, i); |
| 2176 | Py_END_ALLOW_THREADS |
| 2177 | if (res < 0) |
| 2178 | return posix_error_with_allocated_filename(opath); |
| 2179 | Py_DECREF(opath); |
| 2180 | Py_INCREF(Py_None); |
| 2181 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2182 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2185 | #ifdef HAVE_FCHMOD |
| 2186 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2187 | "fchmod(fd, mode)\n\n\ |
| 2188 | Change the access permissions of the file given by file\n\ |
| 2189 | descriptor fd."); |
| 2190 | |
| 2191 | static PyObject * |
| 2192 | posix_fchmod(PyObject *self, PyObject *args) |
| 2193 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2194 | int fd, mode, res; |
| 2195 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2196 | return NULL; |
| 2197 | Py_BEGIN_ALLOW_THREADS |
| 2198 | res = fchmod(fd, mode); |
| 2199 | Py_END_ALLOW_THREADS |
| 2200 | if (res < 0) |
| 2201 | return posix_error(); |
| 2202 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2203 | } |
| 2204 | #endif /* HAVE_FCHMOD */ |
| 2205 | |
| 2206 | #ifdef HAVE_LCHMOD |
| 2207 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2208 | "lchmod(path, mode)\n\n\ |
| 2209 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2210 | affects the link itself rather than the target."); |
| 2211 | |
| 2212 | static PyObject * |
| 2213 | posix_lchmod(PyObject *self, PyObject *args) |
| 2214 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2215 | PyObject *opath; |
| 2216 | char *path; |
| 2217 | int i; |
| 2218 | int res; |
| 2219 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2220 | &opath, &i)) |
| 2221 | return NULL; |
| 2222 | path = PyBytes_AsString(opath); |
| 2223 | Py_BEGIN_ALLOW_THREADS |
| 2224 | res = lchmod(path, i); |
| 2225 | Py_END_ALLOW_THREADS |
| 2226 | if (res < 0) |
| 2227 | return posix_error_with_allocated_filename(opath); |
| 2228 | Py_DECREF(opath); |
| 2229 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2230 | } |
| 2231 | #endif /* HAVE_LCHMOD */ |
| 2232 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2233 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2234 | #ifdef HAVE_CHFLAGS |
| 2235 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2236 | "chflags(path, flags)\n\n\ |
| 2237 | Set file flags."); |
| 2238 | |
| 2239 | static PyObject * |
| 2240 | posix_chflags(PyObject *self, PyObject *args) |
| 2241 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2242 | PyObject *opath; |
| 2243 | char *path; |
| 2244 | unsigned long flags; |
| 2245 | int res; |
| 2246 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2247 | PyUnicode_FSConverter, &opath, &flags)) |
| 2248 | return NULL; |
| 2249 | path = PyBytes_AsString(opath); |
| 2250 | Py_BEGIN_ALLOW_THREADS |
| 2251 | res = chflags(path, flags); |
| 2252 | Py_END_ALLOW_THREADS |
| 2253 | if (res < 0) |
| 2254 | return posix_error_with_allocated_filename(opath); |
| 2255 | Py_DECREF(opath); |
| 2256 | Py_INCREF(Py_None); |
| 2257 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2258 | } |
| 2259 | #endif /* HAVE_CHFLAGS */ |
| 2260 | |
| 2261 | #ifdef HAVE_LCHFLAGS |
| 2262 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2263 | "lchflags(path, flags)\n\n\ |
| 2264 | Set file flags.\n\ |
| 2265 | This function will not follow symbolic links."); |
| 2266 | |
| 2267 | static PyObject * |
| 2268 | posix_lchflags(PyObject *self, PyObject *args) |
| 2269 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2270 | PyObject *opath; |
| 2271 | char *path; |
| 2272 | unsigned long flags; |
| 2273 | int res; |
| 2274 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2275 | PyUnicode_FSConverter, &opath, &flags)) |
| 2276 | return NULL; |
| 2277 | path = PyBytes_AsString(opath); |
| 2278 | Py_BEGIN_ALLOW_THREADS |
| 2279 | res = lchflags(path, flags); |
| 2280 | Py_END_ALLOW_THREADS |
| 2281 | if (res < 0) |
| 2282 | return posix_error_with_allocated_filename(opath); |
| 2283 | Py_DECREF(opath); |
| 2284 | Py_INCREF(Py_None); |
| 2285 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2286 | } |
| 2287 | #endif /* HAVE_LCHFLAGS */ |
| 2288 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2289 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2290 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2291 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2292 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2293 | |
| 2294 | static PyObject * |
| 2295 | posix_chroot(PyObject *self, PyObject *args) |
| 2296 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2297 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2298 | } |
| 2299 | #endif |
| 2300 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2301 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2302 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2303 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2304 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2305 | |
| 2306 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2307 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2308 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2309 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2310 | } |
| 2311 | #endif /* HAVE_FSYNC */ |
| 2312 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2313 | #ifdef HAVE_SYNC |
| 2314 | PyDoc_STRVAR(posix_sync__doc__, |
| 2315 | "sync()\n\n\ |
| 2316 | Force write of everything to disk."); |
| 2317 | |
| 2318 | static PyObject * |
| 2319 | posix_sync(PyObject *self, PyObject *noargs) |
| 2320 | { |
| 2321 | Py_BEGIN_ALLOW_THREADS |
| 2322 | sync(); |
| 2323 | Py_END_ALLOW_THREADS |
| 2324 | Py_RETURN_NONE; |
| 2325 | } |
| 2326 | #endif |
| 2327 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2328 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2329 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2330 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2331 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2332 | #endif |
| 2333 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2334 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2335 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2336 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2337 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2338 | |
| 2339 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2340 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2341 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2342 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2343 | } |
| 2344 | #endif /* HAVE_FDATASYNC */ |
| 2345 | |
| 2346 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2347 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2348 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2349 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2350 | 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] | 2351 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2352 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2353 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2354 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2355 | PyObject *opath; |
| 2356 | char *path; |
| 2357 | long uid, gid; |
| 2358 | int res; |
| 2359 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2360 | PyUnicode_FSConverter, &opath, |
| 2361 | &uid, &gid)) |
| 2362 | return NULL; |
| 2363 | path = PyBytes_AsString(opath); |
| 2364 | Py_BEGIN_ALLOW_THREADS |
| 2365 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2366 | Py_END_ALLOW_THREADS |
| 2367 | if (res < 0) |
| 2368 | return posix_error_with_allocated_filename(opath); |
| 2369 | Py_DECREF(opath); |
| 2370 | Py_INCREF(Py_None); |
| 2371 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2372 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2373 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2374 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2375 | #ifdef HAVE_FCHOWN |
| 2376 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2377 | "fchown(fd, uid, gid)\n\n\ |
| 2378 | Change the owner and group id of the file given by file descriptor\n\ |
| 2379 | fd to the numeric uid and gid."); |
| 2380 | |
| 2381 | static PyObject * |
| 2382 | posix_fchown(PyObject *self, PyObject *args) |
| 2383 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2384 | int fd; |
| 2385 | long uid, gid; |
| 2386 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2387 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2388 | return NULL; |
| 2389 | Py_BEGIN_ALLOW_THREADS |
| 2390 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2391 | Py_END_ALLOW_THREADS |
| 2392 | if (res < 0) |
| 2393 | return posix_error(); |
| 2394 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2395 | } |
| 2396 | #endif /* HAVE_FCHOWN */ |
| 2397 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2398 | #ifdef HAVE_LCHOWN |
| 2399 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2400 | "lchown(path, uid, gid)\n\n\ |
| 2401 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2402 | This function will not follow symbolic links."); |
| 2403 | |
| 2404 | static PyObject * |
| 2405 | posix_lchown(PyObject *self, PyObject *args) |
| 2406 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2407 | PyObject *opath; |
| 2408 | char *path; |
| 2409 | long uid, gid; |
| 2410 | int res; |
| 2411 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2412 | PyUnicode_FSConverter, &opath, |
| 2413 | &uid, &gid)) |
| 2414 | return NULL; |
| 2415 | path = PyBytes_AsString(opath); |
| 2416 | Py_BEGIN_ALLOW_THREADS |
| 2417 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2418 | Py_END_ALLOW_THREADS |
| 2419 | if (res < 0) |
| 2420 | return posix_error_with_allocated_filename(opath); |
| 2421 | Py_DECREF(opath); |
| 2422 | Py_INCREF(Py_None); |
| 2423 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2424 | } |
| 2425 | #endif /* HAVE_LCHOWN */ |
| 2426 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2427 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2428 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2429 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2430 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2431 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2432 | char buf[1026]; |
| 2433 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2434 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2435 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2436 | if (!use_bytes) { |
| 2437 | wchar_t wbuf[1026]; |
| 2438 | wchar_t *wbuf2 = wbuf; |
| 2439 | PyObject *resobj; |
| 2440 | DWORD len; |
| 2441 | Py_BEGIN_ALLOW_THREADS |
| 2442 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2443 | /* If the buffer is large enough, len does not include the |
| 2444 | terminating \0. If the buffer is too small, len includes |
| 2445 | the space needed for the terminator. */ |
| 2446 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2447 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2448 | if (wbuf2) |
| 2449 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2450 | } |
| 2451 | Py_END_ALLOW_THREADS |
| 2452 | if (!wbuf2) { |
| 2453 | PyErr_NoMemory(); |
| 2454 | return NULL; |
| 2455 | } |
| 2456 | if (!len) { |
| 2457 | if (wbuf2 != wbuf) free(wbuf2); |
| 2458 | return win32_error("getcwdu", NULL); |
| 2459 | } |
| 2460 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2461 | if (wbuf2 != wbuf) free(wbuf2); |
| 2462 | return resobj; |
| 2463 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 2464 | |
| 2465 | if (win32_warn_bytes_api()) |
| 2466 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2467 | #endif |
| 2468 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2469 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2470 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2471 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2472 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2473 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2474 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2475 | Py_END_ALLOW_THREADS |
| 2476 | if (res == NULL) |
| 2477 | return posix_error(); |
| 2478 | if (use_bytes) |
| 2479 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2480 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2481 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2482 | |
| 2483 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2484 | "getcwd() -> path\n\n\ |
| 2485 | Return a unicode string representing the current working directory."); |
| 2486 | |
| 2487 | static PyObject * |
| 2488 | posix_getcwd_unicode(PyObject *self) |
| 2489 | { |
| 2490 | return posix_getcwd(0); |
| 2491 | } |
| 2492 | |
| 2493 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2494 | "getcwdb() -> path\n\n\ |
| 2495 | Return a bytes string representing the current working directory."); |
| 2496 | |
| 2497 | static PyObject * |
| 2498 | posix_getcwd_bytes(PyObject *self) |
| 2499 | { |
| 2500 | return posix_getcwd(1); |
| 2501 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2502 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2503 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2504 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2505 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2506 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2507 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2508 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2509 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2510 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2511 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2512 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2513 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2514 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2515 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2516 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2517 | #ifdef MS_WINDOWS |
| 2518 | PyDoc_STRVAR(win32_link__doc__, |
| 2519 | "link(src, dst)\n\n\ |
| 2520 | Create a hard link to a file."); |
| 2521 | |
| 2522 | static PyObject * |
| 2523 | win32_link(PyObject *self, PyObject *args) |
| 2524 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2525 | PyObject *src, *dst; |
| 2526 | BOOL ok; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2527 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2528 | if (PyArg_ParseTuple(args, "UU:link", &src, &dst)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2529 | { |
| 2530 | wchar_t *wsrc, *wdst; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2531 | |
| 2532 | wsrc = PyUnicode_AsUnicode(src); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2533 | if (wsrc == NULL) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2534 | goto error; |
| 2535 | wdst = PyUnicode_AsUnicode(dst); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2536 | if (wdst == NULL) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2537 | goto error; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2538 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2539 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2540 | ok = CreateHardLinkW(wdst, wsrc, NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2541 | Py_END_ALLOW_THREADS |
| 2542 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2543 | if (!ok) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2544 | return win32_error("link", NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2545 | Py_RETURN_NONE; |
| 2546 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2547 | else { |
| 2548 | PyErr_Clear(); |
| 2549 | if (!PyArg_ParseTuple(args, "O&O&:link", |
| 2550 | PyUnicode_FSConverter, &src, |
| 2551 | PyUnicode_FSConverter, &dst)) |
| 2552 | return NULL; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2553 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2554 | if (win32_warn_bytes_api()) |
| 2555 | goto error; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2556 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2557 | Py_BEGIN_ALLOW_THREADS |
| 2558 | ok = CreateHardLinkA(PyBytes_AS_STRING(dst), |
| 2559 | PyBytes_AS_STRING(src), |
| 2560 | NULL); |
| 2561 | Py_END_ALLOW_THREADS |
| 2562 | |
| 2563 | Py_XDECREF(src); |
| 2564 | Py_XDECREF(dst); |
| 2565 | |
| 2566 | if (!ok) |
| 2567 | return win32_error("link", NULL); |
| 2568 | Py_RETURN_NONE; |
| 2569 | |
| 2570 | error: |
| 2571 | Py_XDECREF(src); |
| 2572 | Py_XDECREF(dst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2573 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2574 | } |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2575 | } |
| 2576 | #endif /* MS_WINDOWS */ |
| 2577 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2578 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2579 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2580 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2581 | Return a list containing the names of the entries in the directory.\n\ |
| 2582 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2583 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2584 | \n\ |
| 2585 | 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] | 2586 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2587 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2588 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2589 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2590 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2591 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2592 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2593 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2594 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2595 | PyObject *d, *v; |
| 2596 | HANDLE hFindFile; |
| 2597 | BOOL result; |
| 2598 | WIN32_FIND_DATA FileData; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2599 | const char *path; |
| 2600 | Py_ssize_t pathlen; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2601 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2602 | char *bufptr = namebuf; |
| 2603 | 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] | 2604 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2605 | PyObject *po = NULL; |
| 2606 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2607 | WIN32_FIND_DATAW wFileData; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2608 | wchar_t *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2609 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2610 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2611 | po_wchars = L"."; |
| 2612 | len = 1; |
| 2613 | } else { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 2614 | po_wchars = PyUnicode_AsUnicodeAndSize(po, &len); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2615 | if (po_wchars == NULL) |
| 2616 | return NULL; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2617 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2618 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2619 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2620 | if (!wnamebuf) { |
| 2621 | PyErr_NoMemory(); |
| 2622 | return NULL; |
| 2623 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2624 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2625 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2626 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2627 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2628 | wnamebuf[len++] = L'\\'; |
| 2629 | wcscpy(wnamebuf + len, L"*.*"); |
| 2630 | } |
| 2631 | if ((d = PyList_New(0)) == NULL) { |
| 2632 | free(wnamebuf); |
| 2633 | return NULL; |
| 2634 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2635 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2636 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2637 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2638 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2639 | int error = GetLastError(); |
| 2640 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2641 | free(wnamebuf); |
| 2642 | return d; |
| 2643 | } |
| 2644 | Py_DECREF(d); |
| 2645 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2646 | free(wnamebuf); |
| 2647 | return NULL; |
| 2648 | } |
| 2649 | do { |
| 2650 | /* Skip over . and .. */ |
| 2651 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2652 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 2653 | v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2654 | if (v == NULL) { |
| 2655 | Py_DECREF(d); |
| 2656 | d = NULL; |
| 2657 | break; |
| 2658 | } |
| 2659 | if (PyList_Append(d, v) != 0) { |
| 2660 | Py_DECREF(v); |
| 2661 | Py_DECREF(d); |
| 2662 | d = NULL; |
| 2663 | break; |
| 2664 | } |
| 2665 | Py_DECREF(v); |
| 2666 | } |
| 2667 | Py_BEGIN_ALLOW_THREADS |
| 2668 | result = FindNextFileW(hFindFile, &wFileData); |
| 2669 | Py_END_ALLOW_THREADS |
| 2670 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2671 | it got to the end of the directory. */ |
| 2672 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2673 | Py_DECREF(d); |
| 2674 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2675 | FindClose(hFindFile); |
| 2676 | free(wnamebuf); |
| 2677 | return NULL; |
| 2678 | } |
| 2679 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2680 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2681 | if (FindClose(hFindFile) == FALSE) { |
| 2682 | Py_DECREF(d); |
| 2683 | win32_error_unicode("FindClose", wnamebuf); |
| 2684 | free(wnamebuf); |
| 2685 | return NULL; |
| 2686 | } |
| 2687 | free(wnamebuf); |
| 2688 | return d; |
| 2689 | } |
| 2690 | /* Drop the argument parsing error as narrow strings |
| 2691 | are also valid. */ |
| 2692 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2693 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2694 | if (!PyArg_ParseTuple(args, "y#:listdir", &path, &pathlen)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2695 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2696 | if (win32_warn_bytes_api()) |
| 2697 | return NULL; |
| 2698 | if (pathlen+1 > MAX_PATH) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2699 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2700 | return NULL; |
| 2701 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2702 | strcpy(namebuf, path); |
| 2703 | len = pathlen; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2704 | if (len > 0) { |
| 2705 | char ch = namebuf[len-1]; |
| 2706 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2707 | namebuf[len++] = '/'; |
| 2708 | strcpy(namebuf + len, "*.*"); |
| 2709 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2710 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2711 | if ((d = PyList_New(0)) == NULL) |
| 2712 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2713 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2714 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2715 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2716 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2717 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2718 | int error = GetLastError(); |
| 2719 | if (error == ERROR_FILE_NOT_FOUND) |
| 2720 | return d; |
| 2721 | Py_DECREF(d); |
| 2722 | return win32_error("FindFirstFile", namebuf); |
| 2723 | } |
| 2724 | do { |
| 2725 | /* Skip over . and .. */ |
| 2726 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2727 | strcmp(FileData.cFileName, "..") != 0) { |
| 2728 | v = PyBytes_FromString(FileData.cFileName); |
| 2729 | if (v == NULL) { |
| 2730 | Py_DECREF(d); |
| 2731 | d = NULL; |
| 2732 | break; |
| 2733 | } |
| 2734 | if (PyList_Append(d, v) != 0) { |
| 2735 | Py_DECREF(v); |
| 2736 | Py_DECREF(d); |
| 2737 | d = NULL; |
| 2738 | break; |
| 2739 | } |
| 2740 | Py_DECREF(v); |
| 2741 | } |
| 2742 | Py_BEGIN_ALLOW_THREADS |
| 2743 | result = FindNextFile(hFindFile, &FileData); |
| 2744 | Py_END_ALLOW_THREADS |
| 2745 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2746 | it got to the end of the directory. */ |
| 2747 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2748 | Py_DECREF(d); |
| 2749 | win32_error("FindNextFile", namebuf); |
| 2750 | FindClose(hFindFile); |
| 2751 | return NULL; |
| 2752 | } |
| 2753 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2754 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2755 | if (FindClose(hFindFile) == FALSE) { |
| 2756 | Py_DECREF(d); |
| 2757 | return win32_error("FindClose", namebuf); |
| 2758 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2759 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2760 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2761 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2762 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2763 | |
| 2764 | #ifndef MAX_PATH |
| 2765 | #define MAX_PATH CCHMAXPATH |
| 2766 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2767 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2768 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2769 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2770 | PyObject *d, *v; |
| 2771 | char namebuf[MAX_PATH+5]; |
| 2772 | HDIR hdir = 1; |
| 2773 | ULONG srchcnt = 1; |
| 2774 | FILEFINDBUF3 ep; |
| 2775 | APIRET rc; |
| 2776 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2777 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2778 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2779 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2780 | name = PyBytes_AsString(oname); |
| 2781 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2782 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2783 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2784 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2785 | return NULL; |
| 2786 | } |
| 2787 | strcpy(namebuf, name); |
| 2788 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2789 | if (*pt == ALTSEP) |
| 2790 | *pt = SEP; |
| 2791 | if (namebuf[len-1] != SEP) |
| 2792 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2793 | strcpy(namebuf + len, "*.*"); |
| 2794 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2795 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2796 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2797 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2798 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2799 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2800 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2801 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2802 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2803 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2804 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2805 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2806 | |
| 2807 | if (rc != NO_ERROR) { |
| 2808 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2809 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2812 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2813 | do { |
| 2814 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2815 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2816 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2817 | |
| 2818 | strcpy(namebuf, ep.achName); |
| 2819 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2820 | /* Leave Case of Name Alone -- In Native Form */ |
| 2821 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2822 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2823 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2824 | if (v == NULL) { |
| 2825 | Py_DECREF(d); |
| 2826 | d = NULL; |
| 2827 | break; |
| 2828 | } |
| 2829 | if (PyList_Append(d, v) != 0) { |
| 2830 | Py_DECREF(v); |
| 2831 | Py_DECREF(d); |
| 2832 | d = NULL; |
| 2833 | break; |
| 2834 | } |
| 2835 | Py_DECREF(v); |
| 2836 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2837 | } |
| 2838 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2839 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2840 | return d; |
| 2841 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2842 | PyObject *oname; |
| 2843 | char *name; |
| 2844 | PyObject *d, *v; |
| 2845 | DIR *dirp; |
| 2846 | struct dirent *ep; |
| 2847 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2848 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2849 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2850 | /* v is never read, so it does not need to be initialized yet. */ |
| 2851 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2852 | arg_is_unicode = 0; |
| 2853 | PyErr_Clear(); |
| 2854 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2855 | oname = NULL; |
| 2856 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2857 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2858 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2859 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2860 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2861 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2862 | Py_BEGIN_ALLOW_THREADS |
| 2863 | dirp = opendir(name); |
| 2864 | Py_END_ALLOW_THREADS |
| 2865 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2866 | return posix_error_with_allocated_filename(oname); |
| 2867 | } |
| 2868 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2869 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2870 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2871 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2872 | Py_DECREF(oname); |
| 2873 | return NULL; |
| 2874 | } |
| 2875 | for (;;) { |
| 2876 | errno = 0; |
| 2877 | Py_BEGIN_ALLOW_THREADS |
| 2878 | ep = readdir(dirp); |
| 2879 | Py_END_ALLOW_THREADS |
| 2880 | if (ep == NULL) { |
| 2881 | if (errno == 0) { |
| 2882 | break; |
| 2883 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2884 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2885 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2886 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2887 | Py_DECREF(d); |
| 2888 | return posix_error_with_allocated_filename(oname); |
| 2889 | } |
| 2890 | } |
| 2891 | if (ep->d_name[0] == '.' && |
| 2892 | (NAMLEN(ep) == 1 || |
| 2893 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2894 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2895 | if (arg_is_unicode) |
| 2896 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2897 | else |
| 2898 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2899 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2900 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2901 | break; |
| 2902 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2903 | if (PyList_Append(d, v) != 0) { |
| 2904 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2905 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2906 | break; |
| 2907 | } |
| 2908 | Py_DECREF(v); |
| 2909 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2910 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2911 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2912 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2913 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2914 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2915 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2916 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2917 | #endif /* which OS */ |
| 2918 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2919 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2920 | #ifdef HAVE_FDOPENDIR |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 2921 | PyDoc_STRVAR(posix_flistdir__doc__, |
| 2922 | "flistdir(fd) -> list_of_strings\n\n\ |
Charles-François Natali | 76961fa | 2012-01-10 20:25:09 +0100 | [diff] [blame] | 2923 | Like listdir(), but uses a file descriptor instead."); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2924 | |
| 2925 | static PyObject * |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 2926 | posix_flistdir(PyObject *self, PyObject *args) |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2927 | { |
| 2928 | PyObject *d, *v; |
| 2929 | DIR *dirp; |
| 2930 | struct dirent *ep; |
| 2931 | int fd; |
| 2932 | |
| 2933 | errno = 0; |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 2934 | if (!PyArg_ParseTuple(args, "i:flistdir", &fd)) |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2935 | return NULL; |
Charles-François Natali | 76961fa | 2012-01-10 20:25:09 +0100 | [diff] [blame] | 2936 | /* closedir() closes the FD, so we duplicate it */ |
| 2937 | fd = dup(fd); |
| 2938 | if (fd < 0) |
| 2939 | return posix_error(); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2940 | Py_BEGIN_ALLOW_THREADS |
| 2941 | dirp = fdopendir(fd); |
| 2942 | Py_END_ALLOW_THREADS |
| 2943 | if (dirp == NULL) { |
| 2944 | close(fd); |
| 2945 | return posix_error(); |
| 2946 | } |
| 2947 | if ((d = PyList_New(0)) == NULL) { |
| 2948 | Py_BEGIN_ALLOW_THREADS |
| 2949 | closedir(dirp); |
| 2950 | Py_END_ALLOW_THREADS |
| 2951 | return NULL; |
| 2952 | } |
| 2953 | for (;;) { |
| 2954 | errno = 0; |
| 2955 | Py_BEGIN_ALLOW_THREADS |
| 2956 | ep = readdir(dirp); |
| 2957 | Py_END_ALLOW_THREADS |
| 2958 | if (ep == NULL) { |
| 2959 | if (errno == 0) { |
| 2960 | break; |
| 2961 | } else { |
| 2962 | Py_BEGIN_ALLOW_THREADS |
Charles-François Natali | f2840a8 | 2012-01-08 20:30:47 +0100 | [diff] [blame] | 2963 | rewinddir(dirp); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2964 | closedir(dirp); |
| 2965 | Py_END_ALLOW_THREADS |
| 2966 | Py_DECREF(d); |
| 2967 | return posix_error(); |
| 2968 | } |
| 2969 | } |
| 2970 | if (ep->d_name[0] == '.' && |
| 2971 | (NAMLEN(ep) == 1 || |
| 2972 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2973 | continue; |
| 2974 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2975 | if (v == NULL) { |
| 2976 | Py_CLEAR(d); |
| 2977 | break; |
| 2978 | } |
| 2979 | if (PyList_Append(d, v) != 0) { |
| 2980 | Py_DECREF(v); |
| 2981 | Py_CLEAR(d); |
| 2982 | break; |
| 2983 | } |
| 2984 | Py_DECREF(v); |
| 2985 | } |
| 2986 | Py_BEGIN_ALLOW_THREADS |
Charles-François Natali | f2840a8 | 2012-01-08 20:30:47 +0100 | [diff] [blame] | 2987 | rewinddir(dirp); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2988 | closedir(dirp); |
| 2989 | Py_END_ALLOW_THREADS |
| 2990 | |
| 2991 | return d; |
| 2992 | } |
| 2993 | #endif |
| 2994 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2995 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2996 | /* A helper function for abspath on win32 */ |
| 2997 | static PyObject * |
| 2998 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2999 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3000 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3001 | char outbuf[MAX_PATH*2]; |
| 3002 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3003 | PyObject *po; |
| 3004 | |
| 3005 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 3006 | { |
| 3007 | wchar_t *wpath; |
| 3008 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 3009 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3010 | DWORD result; |
| 3011 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3012 | |
| 3013 | wpath = PyUnicode_AsUnicode(po); |
| 3014 | if (wpath == NULL) |
| 3015 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3016 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3017 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3018 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3019 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3020 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3021 | if (!woutbufp) |
| 3022 | return PyErr_NoMemory(); |
| 3023 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 3024 | } |
| 3025 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3026 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3027 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3028 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3029 | if (woutbufp != woutbuf) |
| 3030 | free(woutbufp); |
| 3031 | return v; |
| 3032 | } |
| 3033 | /* Drop the argument parsing error as narrow strings |
| 3034 | are also valid. */ |
| 3035 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3036 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3037 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 3038 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3039 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3040 | if (win32_warn_bytes_api()) |
| 3041 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 3042 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3043 | outbuf, &temp)) { |
| 3044 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3045 | return NULL; |
| 3046 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3047 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3048 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3049 | Py_FileSystemDefaultEncoding, NULL); |
| 3050 | } |
| 3051 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3052 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3053 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3054 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3055 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3056 | /* A helper function for samepath on windows */ |
| 3057 | static PyObject * |
| 3058 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3059 | { |
| 3060 | HANDLE hFile; |
| 3061 | int buf_size; |
| 3062 | wchar_t *target_path; |
| 3063 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3064 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3065 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3066 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3067 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3068 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3069 | path = PyUnicode_AsUnicode(po); |
| 3070 | if (path == NULL) |
| 3071 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3072 | |
| 3073 | if(!check_GetFinalPathNameByHandle()) { |
| 3074 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3075 | NotImplementedError. */ |
| 3076 | return PyErr_Format(PyExc_NotImplementedError, |
| 3077 | "GetFinalPathNameByHandle not available on this platform"); |
| 3078 | } |
| 3079 | |
| 3080 | hFile = CreateFileW( |
| 3081 | path, |
| 3082 | 0, /* desired access */ |
| 3083 | 0, /* share mode */ |
| 3084 | NULL, /* security attributes */ |
| 3085 | OPEN_EXISTING, |
| 3086 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3087 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3088 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3089 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3090 | if(hFile == INVALID_HANDLE_VALUE) |
| 3091 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3092 | |
| 3093 | /* We have a good handle to the target, use it to determine the |
| 3094 | target path name. */ |
| 3095 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3096 | |
| 3097 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3098 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3099 | |
| 3100 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3101 | if(!target_path) |
| 3102 | return PyErr_NoMemory(); |
| 3103 | |
| 3104 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3105 | buf_size, VOLUME_NAME_DOS); |
| 3106 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3107 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3108 | |
| 3109 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3110 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3111 | |
| 3112 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3113 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3114 | free(target_path); |
| 3115 | return result; |
| 3116 | |
| 3117 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3118 | |
| 3119 | static PyObject * |
| 3120 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3121 | { |
| 3122 | HANDLE hFile; |
| 3123 | BY_HANDLE_FILE_INFORMATION info; |
| 3124 | int fd; |
| 3125 | |
| 3126 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3127 | return NULL; |
| 3128 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3129 | if (!_PyVerify_fd(fd)) |
| 3130 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3131 | |
| 3132 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3133 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3134 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3135 | |
| 3136 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3137 | return win32_error("_getfileinformation", NULL); |
| 3138 | |
| 3139 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3140 | info.nFileIndexHigh, |
| 3141 | info.nFileIndexLow); |
| 3142 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3143 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3144 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3145 | "Return true if the pathname refers to an existing directory."); |
| 3146 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3147 | static PyObject * |
| 3148 | posix__isdir(PyObject *self, PyObject *args) |
| 3149 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3150 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3151 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3152 | DWORD attributes; |
| 3153 | |
| 3154 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3155 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3156 | if (wpath == NULL) |
| 3157 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3158 | |
| 3159 | attributes = GetFileAttributesW(wpath); |
| 3160 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3161 | Py_RETURN_FALSE; |
| 3162 | goto check; |
| 3163 | } |
| 3164 | /* Drop the argument parsing error as narrow strings |
| 3165 | are also valid. */ |
| 3166 | PyErr_Clear(); |
| 3167 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3168 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3169 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3170 | if (win32_warn_bytes_api()) |
| 3171 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3172 | attributes = GetFileAttributesA(path); |
| 3173 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3174 | Py_RETURN_FALSE; |
| 3175 | |
| 3176 | check: |
| 3177 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3178 | Py_RETURN_TRUE; |
| 3179 | else |
| 3180 | Py_RETURN_FALSE; |
| 3181 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3182 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3184 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3185 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3186 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3187 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3188 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3189 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3190 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3191 | int res; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3192 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3193 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3194 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3195 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3196 | PyObject *po; |
| 3197 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) |
| 3198 | { |
| 3199 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3200 | if (wpath == NULL) |
| 3201 | return NULL; |
| 3202 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3203 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3204 | res = CreateDirectoryW(wpath, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3205 | Py_END_ALLOW_THREADS |
| 3206 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3207 | return win32_error_object("mkdir", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3208 | Py_INCREF(Py_None); |
| 3209 | return Py_None; |
| 3210 | } |
| 3211 | /* Drop the argument parsing error as narrow strings |
| 3212 | are also valid. */ |
| 3213 | PyErr_Clear(); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3214 | if (!PyArg_ParseTuple(args, "y|i:mkdir", &path, &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3215 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3216 | if (win32_warn_bytes_api()) |
| 3217 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3218 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3219 | res = CreateDirectoryA(path, NULL); |
| 3220 | Py_END_ALLOW_THREADS |
| 3221 | if (!res) { |
| 3222 | win32_error("mkdir", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3223 | return NULL; |
| 3224 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3225 | Py_INCREF(Py_None); |
| 3226 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3227 | #else |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3228 | PyObject *opath; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3229 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3230 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3231 | PyUnicode_FSConverter, &opath, &mode)) |
| 3232 | return NULL; |
| 3233 | path = PyBytes_AsString(opath); |
| 3234 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3235 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3236 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3237 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3238 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3239 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3240 | Py_END_ALLOW_THREADS |
| 3241 | if (res < 0) |
| 3242 | return posix_error_with_allocated_filename(opath); |
| 3243 | Py_DECREF(opath); |
| 3244 | Py_INCREF(Py_None); |
| 3245 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3246 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3249 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3250 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3251 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3252 | #include <sys/resource.h> |
| 3253 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3254 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3255 | |
| 3256 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3257 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3258 | "nice(inc) -> new_priority\n\n\ |
| 3259 | 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] | 3260 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3261 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3262 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3263 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3264 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3265 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3266 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3267 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3268 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3269 | /* There are two flavours of 'nice': one that returns the new |
| 3270 | priority (as required by almost all standards out there) and the |
| 3271 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3272 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3273 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3274 | If we are of the nice family that returns the new priority, we |
| 3275 | need to clear errno before the call, and check if errno is filled |
| 3276 | before calling posix_error() on a returnvalue of -1, because the |
| 3277 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3278 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3279 | errno = 0; |
| 3280 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3281 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3282 | if (value == 0) |
| 3283 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3284 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3285 | if (value == -1 && errno != 0) |
| 3286 | /* either nice() or getpriority() returned an error */ |
| 3287 | return posix_error(); |
| 3288 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3289 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3290 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3291 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3292 | |
| 3293 | #ifdef HAVE_GETPRIORITY |
| 3294 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3295 | "getpriority(which, who) -> current_priority\n\n\ |
| 3296 | Get program scheduling priority."); |
| 3297 | |
| 3298 | static PyObject * |
| 3299 | posix_getpriority(PyObject *self, PyObject *args) |
| 3300 | { |
| 3301 | int which, who, retval; |
| 3302 | |
| 3303 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3304 | return NULL; |
| 3305 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3306 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3307 | if (errno != 0) |
| 3308 | return posix_error(); |
| 3309 | return PyLong_FromLong((long)retval); |
| 3310 | } |
| 3311 | #endif /* HAVE_GETPRIORITY */ |
| 3312 | |
| 3313 | |
| 3314 | #ifdef HAVE_SETPRIORITY |
| 3315 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3316 | "setpriority(which, who, prio) -> None\n\n\ |
| 3317 | Set program scheduling priority."); |
| 3318 | |
| 3319 | static PyObject * |
| 3320 | posix_setpriority(PyObject *self, PyObject *args) |
| 3321 | { |
| 3322 | int which, who, prio, retval; |
| 3323 | |
| 3324 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3325 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3326 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3327 | if (retval == -1) |
| 3328 | return posix_error(); |
| 3329 | Py_RETURN_NONE; |
| 3330 | } |
| 3331 | #endif /* HAVE_SETPRIORITY */ |
| 3332 | |
| 3333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3334 | static PyObject * |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3335 | internal_rename(PyObject *self, PyObject *args, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3336 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3337 | #ifdef MS_WINDOWS |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3338 | PyObject *src, *dst; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3339 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3340 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
| 3341 | if (PyArg_ParseTuple(args, |
| 3342 | is_replace ? "UU:replace" : "UU:rename", |
| 3343 | &src, &dst)) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3344 | { |
| 3345 | wchar_t *wsrc, *wdst; |
| 3346 | |
| 3347 | wsrc = PyUnicode_AsUnicode(src); |
| 3348 | if (wsrc == NULL) |
| 3349 | return NULL; |
| 3350 | wdst = PyUnicode_AsUnicode(dst); |
| 3351 | if (wdst == NULL) |
| 3352 | return NULL; |
| 3353 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3354 | result = MoveFileExW(wsrc, wdst, flags); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3355 | Py_END_ALLOW_THREADS |
| 3356 | if (!result) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3357 | return win32_error(is_replace ? "replace" : "rename", NULL); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3358 | Py_INCREF(Py_None); |
| 3359 | return Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3360 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3361 | else { |
| 3362 | PyErr_Clear(); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3363 | if (!PyArg_ParseTuple(args, |
| 3364 | is_replace ? "O&O&:replace" : "O&O&:rename", |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3365 | PyUnicode_FSConverter, &src, |
| 3366 | PyUnicode_FSConverter, &dst)) |
| 3367 | return NULL; |
| 3368 | |
| 3369 | if (win32_warn_bytes_api()) |
| 3370 | goto error; |
| 3371 | |
| 3372 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3373 | result = MoveFileExA(PyBytes_AS_STRING(src), |
| 3374 | PyBytes_AS_STRING(dst), flags); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3375 | Py_END_ALLOW_THREADS |
| 3376 | |
| 3377 | Py_XDECREF(src); |
| 3378 | Py_XDECREF(dst); |
| 3379 | |
| 3380 | if (!result) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3381 | return win32_error(is_replace ? "replace" : "rename", NULL); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3382 | Py_INCREF(Py_None); |
| 3383 | return Py_None; |
| 3384 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3385 | error: |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3386 | Py_XDECREF(src); |
| 3387 | Py_XDECREF(dst); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3388 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3389 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3390 | #else |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3391 | return posix_2str(args, |
| 3392 | is_replace ? "O&O&:replace" : "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3393 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3396 | PyDoc_STRVAR(posix_rename__doc__, |
| 3397 | "rename(old, new)\n\n\ |
| 3398 | Rename a file or directory."); |
| 3399 | |
| 3400 | static PyObject * |
| 3401 | posix_rename(PyObject *self, PyObject *args) |
| 3402 | { |
| 3403 | return internal_rename(self, args, 0); |
| 3404 | } |
| 3405 | |
| 3406 | PyDoc_STRVAR(posix_replace__doc__, |
| 3407 | "replace(old, new)\n\n\ |
| 3408 | Rename a file or directory, overwriting the destination."); |
| 3409 | |
| 3410 | static PyObject * |
| 3411 | posix_replace(PyObject *self, PyObject *args) |
| 3412 | { |
| 3413 | return internal_rename(self, args, 1); |
| 3414 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3415 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3416 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3417 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3418 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3419 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3420 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3421 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3422 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3423 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3424 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3425 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3426 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3427 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3430 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3431 | PyDoc_STRVAR(posix_stat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 3432 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3433 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3434 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3435 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 3436 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3437 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3438 | #ifdef MS_WINDOWS |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 3439 | 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] | 3440 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 3441 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3442 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3445 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3446 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3447 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3448 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3449 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3450 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3451 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3452 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3453 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3454 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3455 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3456 | wchar_t *command; |
| 3457 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3458 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3459 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3460 | Py_BEGIN_ALLOW_THREADS |
| 3461 | sts = _wsystem(command); |
| 3462 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3463 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3464 | PyObject *command_obj; |
| 3465 | char *command; |
| 3466 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3467 | PyUnicode_FSConverter, &command_obj)) |
| 3468 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3469 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3470 | command = PyBytes_AsString(command_obj); |
| 3471 | Py_BEGIN_ALLOW_THREADS |
| 3472 | sts = system(command); |
| 3473 | Py_END_ALLOW_THREADS |
| 3474 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3475 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3476 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3477 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3478 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3479 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3481 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3482 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3483 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3484 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3485 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3486 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3487 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3488 | int i; |
| 3489 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3490 | return NULL; |
| 3491 | i = (int)umask(i); |
| 3492 | if (i < 0) |
| 3493 | return posix_error(); |
| 3494 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3497 | #ifdef MS_WINDOWS |
| 3498 | |
| 3499 | /* override the default DeleteFileW behavior so that directory |
| 3500 | symlinks can be removed with this function, the same as with |
| 3501 | Unix symlinks */ |
| 3502 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3503 | { |
| 3504 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3505 | WIN32_FIND_DATAW find_data; |
| 3506 | HANDLE find_data_handle; |
| 3507 | int is_directory = 0; |
| 3508 | int is_link = 0; |
| 3509 | |
| 3510 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3511 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3512 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3513 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3514 | it is a symlink */ |
| 3515 | if(is_directory && |
| 3516 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3517 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3518 | |
| 3519 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3520 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3521 | FindClose(find_data_handle); |
| 3522 | } |
| 3523 | } |
| 3524 | } |
| 3525 | |
| 3526 | if (is_directory && is_link) |
| 3527 | return RemoveDirectoryW(lpFileName); |
| 3528 | |
| 3529 | return DeleteFileW(lpFileName); |
| 3530 | } |
| 3531 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3532 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3533 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3534 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3535 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3536 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3537 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3538 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3539 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3540 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3541 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3542 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3543 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3544 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3545 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3546 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3547 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3548 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3549 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3552 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3553 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3554 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3555 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3556 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3558 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3559 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3560 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3561 | struct utsname u; |
| 3562 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3563 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3564 | Py_BEGIN_ALLOW_THREADS |
| 3565 | res = uname(&u); |
| 3566 | Py_END_ALLOW_THREADS |
| 3567 | if (res < 0) |
| 3568 | return posix_error(); |
| 3569 | return Py_BuildValue("(sssss)", |
| 3570 | u.sysname, |
| 3571 | u.nodename, |
| 3572 | u.release, |
| 3573 | u.version, |
| 3574 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3575 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3576 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3577 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3578 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3579 | static int |
| 3580 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 3581 | { |
| 3582 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 3583 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3584 | divmod = PyNumber_Divmod(py_long, billion); |
| 3585 | if (!divmod) |
| 3586 | goto exit; |
| 3587 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 3588 | if ((*s == -1) && PyErr_Occurred()) |
| 3589 | goto exit; |
| 3590 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 3591 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3592 | goto exit; |
| 3593 | |
| 3594 | result = 1; |
| 3595 | exit: |
| 3596 | Py_XDECREF(divmod); |
| 3597 | return result; |
| 3598 | } |
| 3599 | |
| 3600 | |
| 3601 | typedef int (*parameter_converter_t)(PyObject *, void *); |
| 3602 | |
| 3603 | typedef struct { |
| 3604 | /* input only */ |
| 3605 | char path_format; |
| 3606 | parameter_converter_t converter; |
| 3607 | char *function_name; |
| 3608 | char *first_argument_name; |
| 3609 | PyObject *args; |
| 3610 | PyObject *kwargs; |
| 3611 | |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 3612 | /* input/output */ |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3613 | PyObject **path; |
| 3614 | |
| 3615 | /* output only */ |
| 3616 | int now; |
| 3617 | time_t atime_s; |
| 3618 | long atime_ns; |
| 3619 | time_t mtime_s; |
| 3620 | long mtime_ns; |
| 3621 | } utime_arguments; |
| 3622 | |
| 3623 | #define DECLARE_UA(ua, fname) \ |
| 3624 | utime_arguments ua; \ |
| 3625 | memset(&ua, 0, sizeof(ua)); \ |
| 3626 | ua.function_name = fname; \ |
| 3627 | ua.args = args; \ |
| 3628 | ua.kwargs = kwargs; \ |
| 3629 | ua.first_argument_name = "path"; \ |
| 3630 | |
| 3631 | /* UA_TO_FILETIME doesn't declare atime and mtime for you */ |
| 3632 | #define UA_TO_FILETIME(ua, atime, mtime) \ |
| 3633 | time_t_to_FILE_TIME(ua.atime_s, ua.atime_ns, &atime); \ |
| 3634 | time_t_to_FILE_TIME(ua.mtime_s, ua.mtime_ns, &mtime) |
| 3635 | |
| 3636 | /* the rest of these macros declare the output variable for you */ |
| 3637 | #define UA_TO_TIMESPEC(ua, ts) \ |
| 3638 | struct timespec ts[2]; \ |
| 3639 | ts[0].tv_sec = ua.atime_s; \ |
| 3640 | ts[0].tv_nsec = ua.atime_ns; \ |
| 3641 | ts[1].tv_sec = ua.mtime_s; \ |
| 3642 | ts[1].tv_nsec = ua.mtime_ns |
| 3643 | |
| 3644 | #define UA_TO_TIMEVAL(ua, tv) \ |
| 3645 | struct timeval tv[2]; \ |
| 3646 | tv[0].tv_sec = ua.atime_s; \ |
| 3647 | tv[0].tv_usec = ua.atime_ns / 1000; \ |
| 3648 | tv[1].tv_sec = ua.mtime_s; \ |
| 3649 | tv[1].tv_usec = ua.mtime_ns / 1000 |
| 3650 | |
| 3651 | #define UA_TO_UTIMBUF(ua, u) \ |
| 3652 | struct utimbuf u; \ |
| 3653 | utimbuf.actime = ua.atime_s; \ |
| 3654 | utimbuf.modtime = ua.mtime_s |
| 3655 | |
| 3656 | #define UA_TO_TIME_T(ua, timet) \ |
| 3657 | time_t timet[2]; \ |
| 3658 | timet[0] = ua.atime_s; \ |
| 3659 | timet[1] = ua.mtime_s |
| 3660 | |
| 3661 | |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 3662 | /* |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3663 | * utime_read_time_arguments() processes arguments for the utime |
| 3664 | * family of functions. |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3665 | */ |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3666 | |
| 3667 | typedef enum { |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3668 | UTIME_SUCCESS = 0, |
| 3669 | UTIME_PARSE_FAILURE = 1, |
| 3670 | UTIME_TIMES_AND_NS_COLLISION = 2, |
| 3671 | UTIME_TIMES_CONVERSION_FAILURE = 3, |
| 3672 | UTIME_NS_CONVERSION_FAILURE = 4, |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3673 | } utime_status; |
| 3674 | |
| 3675 | static utime_status |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3676 | utime_read_time_arguments(utime_arguments *ua) |
| 3677 | { |
| 3678 | PyObject *times = NULL; |
| 3679 | PyObject *ns = NULL; |
| 3680 | char format[24]; |
| 3681 | char *kwlist[4]; |
| 3682 | char **kw = kwlist; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3683 | utime_status return_value; |
| 3684 | int parse_result; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3685 | |
| 3686 | *kw++ = ua->first_argument_name; |
| 3687 | *kw++ = "times"; |
| 3688 | *kw++ = "ns"; |
| 3689 | *kw = NULL; |
| 3690 | |
| 3691 | sprintf(format, "%c%s|O$O:%s", |
| 3692 | ua->path_format, |
| 3693 | ua->converter ? "&" : "", |
| 3694 | ua->function_name); |
| 3695 | |
| 3696 | if (ua->converter) |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3697 | parse_result = PyArg_ParseTupleAndKeywords(ua->args, ua->kwargs, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3698 | format, kwlist, ua->converter, ua->path, ×, &ns); |
| 3699 | else |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3700 | parse_result = PyArg_ParseTupleAndKeywords(ua->args, ua->kwargs, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3701 | format, kwlist, ua->path, ×, &ns); |
| 3702 | |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3703 | if (!parse_result) |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3704 | return UTIME_PARSE_FAILURE; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3705 | |
| 3706 | if (times && ns) { |
| 3707 | PyErr_Format(PyExc_RuntimeError, |
| 3708 | "%s: you may specify either 'times'" |
| 3709 | " or 'ns' but not both", |
| 3710 | ua->function_name); |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3711 | return_value = UTIME_TIMES_AND_NS_COLLISION; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 3712 | goto fail; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | if (times && (times != Py_None)) { |
| 3716 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
| 3717 | PyErr_Format(PyExc_TypeError, |
Stefan Krah | 6b03f2c | 2012-05-05 22:37:05 +0200 | [diff] [blame] | 3718 | "%s: 'times' must be either" |
Benjamin Peterson | 9bd9d74 | 2012-05-04 01:42:41 -0400 | [diff] [blame] | 3719 | " a tuple of two ints or None", |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3720 | ua->function_name); |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3721 | return_value = UTIME_TIMES_CONVERSION_FAILURE; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 3722 | goto fail; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3723 | } |
| 3724 | ua->now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 3725 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
| 3726 | &ua->atime_s, &ua->atime_ns) == -1 || |
| 3727 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3728 | &ua->mtime_s, &ua->mtime_ns) == -1) { |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3729 | return_value = UTIME_TIMES_CONVERSION_FAILURE; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 3730 | goto fail; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3731 | } |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3732 | return UTIME_SUCCESS; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | if (ns) { |
| 3736 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
| 3737 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 9bd9d74 | 2012-05-04 01:42:41 -0400 | [diff] [blame] | 3738 | "%s: 'ns' must be a tuple of two ints", |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3739 | ua->function_name); |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3740 | return_value = UTIME_NS_CONVERSION_FAILURE; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 3741 | goto fail; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3742 | } |
| 3743 | ua->now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 3744 | if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0), |
| 3745 | &ua->atime_s, &ua->atime_ns) || |
| 3746 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3747 | &ua->mtime_s, &ua->mtime_ns)) { |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3748 | return_value = UTIME_NS_CONVERSION_FAILURE; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 3749 | goto fail; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3750 | } |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3751 | return UTIME_SUCCESS; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | /* either times=None, or neither times nor ns was specified. use "now". */ |
| 3755 | ua->now = 1; |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3756 | return UTIME_SUCCESS; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 3757 | |
| 3758 | fail: |
| 3759 | if (ua->converter) |
Richard Oudkerk | f072b45 | 2012-05-04 12:01:31 +0100 | [diff] [blame] | 3760 | Py_DECREF(*ua->path); |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3761 | return return_value; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3765 | PyDoc_STRVAR(posix_utime__doc__, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3766 | "utime(path[, times=(atime, mtime), *, ns=(atime_ns, mtime_ns)])\n\ |
| 3767 | Set the access and modified time of the file.\n\ |
| 3768 | If the second argument ('times') is specified,\n\ |
| 3769 | the values should be expressed as float seconds since the epoch.\n\ |
| 3770 | If the keyword argument 'ns' is specified,\n\ |
| 3771 | the values should be expressed as integer nanoseconds since the epoch.\n\ |
| 3772 | If neither the second nor the 'ns' argument is specified,\n\ |
| 3773 | utime uses the current time.\n\ |
| 3774 | Specifying both 'times' and 'ns' is an error."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3775 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3776 | static PyObject * |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3777 | posix_utime(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3778 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3779 | #ifdef MS_WINDOWS |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3780 | PyObject *upath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3781 | HANDLE hFile; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3782 | PyObject *result = NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3783 | FILETIME atime, mtime; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3784 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3785 | DECLARE_UA(ua, "utime"); |
| 3786 | |
| 3787 | ua.path_format = 'U'; |
| 3788 | ua.path = &upath; |
| 3789 | |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3790 | switch (utime_read_time_arguments(&ua)) { |
| 3791 | default: |
| 3792 | return NULL; |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3793 | case UTIME_SUCCESS: { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3794 | wchar_t *wpath = PyUnicode_AsUnicode(upath); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3795 | if (wpath == NULL) |
| 3796 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3797 | Py_BEGIN_ALLOW_THREADS |
| 3798 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3799 | NULL, OPEN_EXISTING, |
| 3800 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3801 | Py_END_ALLOW_THREADS |
| 3802 | if (hFile == INVALID_HANDLE_VALUE) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3803 | return win32_error_object("utime", upath); |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3804 | break; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3805 | } |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3806 | case UTIME_PARSE_FAILURE: { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3807 | const char *apath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3808 | /* Drop the argument parsing error as narrow strings |
| 3809 | are also valid. */ |
| 3810 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3811 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3812 | ua.path_format = 'y'; |
| 3813 | ua.path = (PyObject **)&apath; |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3814 | if (utime_read_time_arguments(&ua) != UTIME_SUCCESS) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3815 | return NULL; |
| 3816 | if (win32_warn_bytes_api()) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3817 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3818 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3819 | Py_BEGIN_ALLOW_THREADS |
| 3820 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3821 | NULL, OPEN_EXISTING, |
| 3822 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3823 | Py_END_ALLOW_THREADS |
| 3824 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3825 | win32_error("utime", apath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3826 | return NULL; |
| 3827 | } |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 3828 | break; |
| 3829 | } |
| 3830 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3833 | if (ua.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3834 | SYSTEMTIME now; |
| 3835 | GetSystemTime(&now); |
| 3836 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3837 | !SystemTimeToFileTime(&now, &atime)) { |
| 3838 | win32_error("utime", NULL); |
| 3839 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3840 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3841 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3842 | else { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3843 | UA_TO_FILETIME(ua, atime, mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3844 | } |
| 3845 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3846 | /* Avoid putting the file name into the error here, |
| 3847 | as that may confuse the user into believing that |
| 3848 | something is wrong with the file, when it also |
| 3849 | could be the time stamp that gives a problem. */ |
| 3850 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3851 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3852 | } |
| 3853 | Py_INCREF(Py_None); |
| 3854 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3855 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3856 | CloseHandle(hFile); |
| 3857 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3858 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3859 | PyObject *opath; |
| 3860 | char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3861 | int res; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3862 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3863 | DECLARE_UA(ua, "utime"); |
| 3864 | |
| 3865 | ua.path_format = 'O'; |
| 3866 | ua.path = &opath; |
| 3867 | ua.converter = PyUnicode_FSConverter; |
| 3868 | |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3869 | if (utime_read_time_arguments(&ua) != UTIME_SUCCESS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3870 | return NULL; |
| 3871 | path = PyBytes_AsString(opath); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3872 | if (ua.now) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3873 | Py_BEGIN_ALLOW_THREADS |
| 3874 | res = utime(path, NULL); |
| 3875 | Py_END_ALLOW_THREADS |
| 3876 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3877 | else { |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3878 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3879 | #ifdef HAVE_UTIMENSAT |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3880 | UA_TO_TIMESPEC(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3881 | res = utimensat(AT_FDCWD, path, buf, 0); |
| 3882 | #elif defined(HAVE_UTIMES) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3883 | UA_TO_TIMEVAL(ua, buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3884 | res = utimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3885 | #elif defined(HAVE_UTIME_H) |
| 3886 | /* XXX should define struct utimbuf instead, above */ |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3887 | UA_TO_UTIMBUF(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3888 | res = utime(path, &buf); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3889 | #else |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3890 | UA_TO_TIME_T(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3891 | res = utime(path, buf); |
| 3892 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3893 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3894 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3895 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3896 | if (res < 0) { |
| 3897 | return posix_error_with_allocated_filename(opath); |
| 3898 | } |
| 3899 | Py_DECREF(opath); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3900 | Py_RETURN_NONE; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3901 | #undef UTIME_EXTRACT |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3902 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3905 | #ifdef HAVE_FUTIMES |
| 3906 | PyDoc_STRVAR(posix_futimes__doc__, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3907 | "futimes(fd[, times=(atime, mtime), *, ns=(atime_ns, mtime_ns)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3908 | 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] | 3909 | 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] | 3910 | |
| 3911 | static PyObject * |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3912 | posix_futimes(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3913 | { |
| 3914 | int res, fd; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3915 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3916 | DECLARE_UA(ua, "futimes"); |
| 3917 | |
| 3918 | ua.path_format = 'i'; |
| 3919 | ua.path = (PyObject **)&fd; |
| 3920 | ua.first_argument_name = "fd"; |
| 3921 | |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3922 | if (utime_read_time_arguments(&ua) != UTIME_SUCCESS) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3923 | return NULL; |
| 3924 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3925 | if (ua.now) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3926 | Py_BEGIN_ALLOW_THREADS |
| 3927 | res = futimes(fd, NULL); |
| 3928 | Py_END_ALLOW_THREADS |
| 3929 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3930 | else { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3931 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3932 | { |
| 3933 | #ifdef HAVE_FUTIMENS |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3934 | UA_TO_TIMESPEC(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3935 | res = futimens(fd, buf); |
| 3936 | #else |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3937 | UA_TO_TIMEVAL(ua, buf); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3938 | res = futimes(fd, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3939 | #endif |
| 3940 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3941 | Py_END_ALLOW_THREADS |
| 3942 | } |
| 3943 | if (res < 0) |
| 3944 | return posix_error(); |
| 3945 | Py_RETURN_NONE; |
| 3946 | } |
| 3947 | #endif |
| 3948 | |
| 3949 | #ifdef HAVE_LUTIMES |
| 3950 | PyDoc_STRVAR(posix_lutimes__doc__, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3951 | "lutimes(path[, times=(atime, mtime), *, ns=(atime_ns, mtime_ns)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3952 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 3953 | |
| 3954 | static PyObject * |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3955 | posix_lutimes(PyObject *self, PyObject *args, PyObject *kwargs) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3956 | { |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3957 | PyObject *opath; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3958 | const char *path; |
| 3959 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3960 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3961 | DECLARE_UA(ua, "lutimes"); |
| 3962 | |
| 3963 | ua.path_format = 'O'; |
| 3964 | ua.path = &opath; |
| 3965 | ua.converter = PyUnicode_FSConverter; |
| 3966 | |
Benjamin Peterson | 9242c13 | 2012-05-25 10:26:47 -0700 | [diff] [blame] | 3967 | if (utime_read_time_arguments(&ua) != UTIME_SUCCESS) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3968 | return NULL; |
| 3969 | path = PyBytes_AsString(opath); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3970 | |
| 3971 | if (ua.now) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3972 | /* optional time values not given */ |
| 3973 | Py_BEGIN_ALLOW_THREADS |
| 3974 | res = lutimes(path, NULL); |
| 3975 | Py_END_ALLOW_THREADS |
| 3976 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3977 | else { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3978 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3979 | { |
| 3980 | #ifdef HAVE_UTIMENSAT |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3981 | UA_TO_TIMESPEC(ua, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3982 | res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); |
| 3983 | #else |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 3984 | UA_TO_TIMEVAL(ua, buf); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3985 | res = lutimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3986 | #endif |
| 3987 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3988 | Py_END_ALLOW_THREADS |
| 3989 | } |
| 3990 | Py_DECREF(opath); |
| 3991 | if (res < 0) |
| 3992 | return posix_error(); |
| 3993 | Py_RETURN_NONE; |
| 3994 | } |
| 3995 | #endif |
| 3996 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3997 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3998 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3999 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4000 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4001 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4002 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4003 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4004 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4005 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4006 | int sts; |
| 4007 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 4008 | return NULL; |
| 4009 | _exit(sts); |
| 4010 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4013 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 4014 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4015 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4016 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4017 | Py_ssize_t i; |
| 4018 | for (i = 0; i < count; i++) |
| 4019 | PyMem_Free(array[i]); |
| 4020 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4021 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4022 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 4023 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4024 | int fsconvert_strdup(PyObject *o, char**out) |
| 4025 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4026 | PyObject *bytes; |
| 4027 | Py_ssize_t size; |
| 4028 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 4029 | return 0; |
| 4030 | size = PyBytes_GET_SIZE(bytes); |
| 4031 | *out = PyMem_Malloc(size+1); |
| 4032 | if (!*out) |
| 4033 | return 0; |
| 4034 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 4035 | Py_DECREF(bytes); |
| 4036 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4037 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4038 | #endif |
| 4039 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4040 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4041 | static char** |
| 4042 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 4043 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4044 | char **envlist; |
| 4045 | Py_ssize_t i, pos, envc; |
| 4046 | PyObject *keys=NULL, *vals=NULL; |
| 4047 | PyObject *key, *val, *key2, *val2; |
| 4048 | char *p, *k, *v; |
| 4049 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4051 | i = PyMapping_Size(env); |
| 4052 | if (i < 0) |
| 4053 | return NULL; |
| 4054 | envlist = PyMem_NEW(char *, i + 1); |
| 4055 | if (envlist == NULL) { |
| 4056 | PyErr_NoMemory(); |
| 4057 | return NULL; |
| 4058 | } |
| 4059 | envc = 0; |
| 4060 | keys = PyMapping_Keys(env); |
| 4061 | vals = PyMapping_Values(env); |
| 4062 | if (!keys || !vals) |
| 4063 | goto error; |
| 4064 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 4065 | PyErr_Format(PyExc_TypeError, |
| 4066 | "env.keys() or env.values() is not a list"); |
| 4067 | goto error; |
| 4068 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4069 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4070 | for (pos = 0; pos < i; pos++) { |
| 4071 | key = PyList_GetItem(keys, pos); |
| 4072 | val = PyList_GetItem(vals, pos); |
| 4073 | if (!key || !val) |
| 4074 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4075 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4076 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4077 | goto error; |
| 4078 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4079 | Py_DECREF(key2); |
| 4080 | goto error; |
| 4081 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4082 | |
| 4083 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4084 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 4085 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4086 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4087 | k = PyBytes_AsString(key2); |
| 4088 | v = PyBytes_AsString(val2); |
| 4089 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4090 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4091 | p = PyMem_NEW(char, len); |
| 4092 | if (p == NULL) { |
| 4093 | PyErr_NoMemory(); |
| 4094 | Py_DECREF(key2); |
| 4095 | Py_DECREF(val2); |
| 4096 | goto error; |
| 4097 | } |
| 4098 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4099 | envlist[envc++] = p; |
| 4100 | Py_DECREF(key2); |
| 4101 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4102 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4103 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4104 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4105 | } |
| 4106 | Py_DECREF(vals); |
| 4107 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4108 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4109 | envlist[envc] = 0; |
| 4110 | *envc_ptr = envc; |
| 4111 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4112 | |
| 4113 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4114 | Py_XDECREF(keys); |
| 4115 | Py_XDECREF(vals); |
| 4116 | while (--envc >= 0) |
| 4117 | PyMem_DEL(envlist[envc]); |
| 4118 | PyMem_DEL(envlist); |
| 4119 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4120 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4121 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4122 | static char** |
| 4123 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4124 | { |
| 4125 | int i; |
| 4126 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4127 | if (argvlist == NULL) { |
| 4128 | PyErr_NoMemory(); |
| 4129 | return NULL; |
| 4130 | } |
| 4131 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4132 | PyObject* item = PySequence_ITEM(argv, i); |
| 4133 | if (item == NULL) |
| 4134 | goto fail; |
| 4135 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4136 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4137 | goto fail; |
| 4138 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4139 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4140 | } |
| 4141 | argvlist[*argc] = NULL; |
| 4142 | return argvlist; |
| 4143 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4144 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4145 | free_string_array(argvlist, *argc); |
| 4146 | return NULL; |
| 4147 | } |
| 4148 | #endif |
| 4149 | |
| 4150 | #ifdef HAVE_EXECV |
| 4151 | PyDoc_STRVAR(posix_execv__doc__, |
| 4152 | "execv(path, args)\n\n\ |
| 4153 | Execute an executable path with arguments, replacing current process.\n\ |
| 4154 | \n\ |
| 4155 | path: path of executable file\n\ |
| 4156 | args: tuple or list of strings"); |
| 4157 | |
| 4158 | static PyObject * |
| 4159 | posix_execv(PyObject *self, PyObject *args) |
| 4160 | { |
| 4161 | PyObject *opath; |
| 4162 | char *path; |
| 4163 | PyObject *argv; |
| 4164 | char **argvlist; |
| 4165 | Py_ssize_t argc; |
| 4166 | |
| 4167 | /* execv has two arguments: (path, argv), where |
| 4168 | argv is a list or tuple of strings. */ |
| 4169 | |
| 4170 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4171 | PyUnicode_FSConverter, |
| 4172 | &opath, &argv)) |
| 4173 | return NULL; |
| 4174 | path = PyBytes_AsString(opath); |
| 4175 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4176 | PyErr_SetString(PyExc_TypeError, |
| 4177 | "execv() arg 2 must be a tuple or list"); |
| 4178 | Py_DECREF(opath); |
| 4179 | return NULL; |
| 4180 | } |
| 4181 | argc = PySequence_Size(argv); |
| 4182 | if (argc < 1) { |
| 4183 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4184 | Py_DECREF(opath); |
| 4185 | return NULL; |
| 4186 | } |
| 4187 | |
| 4188 | argvlist = parse_arglist(argv, &argc); |
| 4189 | if (argvlist == NULL) { |
| 4190 | Py_DECREF(opath); |
| 4191 | return NULL; |
| 4192 | } |
| 4193 | |
| 4194 | execv(path, argvlist); |
| 4195 | |
| 4196 | /* If we get here it's definitely an error */ |
| 4197 | |
| 4198 | free_string_array(argvlist, argc); |
| 4199 | Py_DECREF(opath); |
| 4200 | return posix_error(); |
| 4201 | } |
| 4202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4203 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4204 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4205 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4206 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4207 | path: path of executable file\n\ |
| 4208 | args: tuple or list of arguments\n\ |
| 4209 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4210 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4211 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4212 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4213 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4214 | PyObject *opath; |
| 4215 | char *path; |
| 4216 | PyObject *argv, *env; |
| 4217 | char **argvlist; |
| 4218 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4219 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4221 | /* execve has three arguments: (path, argv, env), where |
| 4222 | argv is a list or tuple of strings and env is a dictionary |
| 4223 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4224 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4225 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 4226 | PyUnicode_FSConverter, |
| 4227 | &opath, &argv, &env)) |
| 4228 | return NULL; |
| 4229 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4230 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4231 | PyErr_SetString(PyExc_TypeError, |
| 4232 | "execve() arg 2 must be a tuple or list"); |
| 4233 | goto fail_0; |
| 4234 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4235 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4236 | if (!PyMapping_Check(env)) { |
| 4237 | PyErr_SetString(PyExc_TypeError, |
| 4238 | "execve() arg 3 must be a mapping object"); |
| 4239 | goto fail_0; |
| 4240 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4241 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4242 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4243 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4244 | goto fail_0; |
| 4245 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4246 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4247 | envlist = parse_envlist(env, &envc); |
| 4248 | if (envlist == NULL) |
| 4249 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4250 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4251 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4252 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4253 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4254 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4255 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4256 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4257 | while (--envc >= 0) |
| 4258 | PyMem_DEL(envlist[envc]); |
| 4259 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4260 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4261 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4262 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4263 | Py_DECREF(opath); |
| 4264 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4265 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4266 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4267 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4268 | #ifdef HAVE_FEXECVE |
| 4269 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4270 | "fexecve(fd, args, env)\n\n\ |
| 4271 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4272 | environment, replacing the current process.\n\ |
| 4273 | \n\ |
| 4274 | fd: file descriptor of executable\n\ |
| 4275 | args: tuple or list of arguments\n\ |
| 4276 | env: dictionary of strings mapping to strings"); |
| 4277 | |
| 4278 | static PyObject * |
| 4279 | posix_fexecve(PyObject *self, PyObject *args) |
| 4280 | { |
| 4281 | int fd; |
| 4282 | PyObject *argv, *env; |
| 4283 | char **argvlist; |
| 4284 | char **envlist; |
| 4285 | Py_ssize_t argc, envc; |
| 4286 | |
| 4287 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4288 | &fd, &argv, &env)) |
| 4289 | return NULL; |
| 4290 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4291 | PyErr_SetString(PyExc_TypeError, |
| 4292 | "fexecve() arg 2 must be a tuple or list"); |
| 4293 | return NULL; |
| 4294 | } |
| 4295 | argc = PySequence_Size(argv); |
| 4296 | if (!PyMapping_Check(env)) { |
| 4297 | PyErr_SetString(PyExc_TypeError, |
| 4298 | "fexecve() arg 3 must be a mapping object"); |
| 4299 | return NULL; |
| 4300 | } |
| 4301 | |
| 4302 | argvlist = parse_arglist(argv, &argc); |
| 4303 | if (argvlist == NULL) |
| 4304 | return NULL; |
| 4305 | |
| 4306 | envlist = parse_envlist(env, &envc); |
| 4307 | if (envlist == NULL) |
| 4308 | goto fail; |
| 4309 | |
| 4310 | fexecve(fd, argvlist, envlist); |
| 4311 | |
| 4312 | /* If we get here it's definitely an error */ |
| 4313 | |
| 4314 | (void) posix_error(); |
| 4315 | |
| 4316 | while (--envc >= 0) |
| 4317 | PyMem_DEL(envlist[envc]); |
| 4318 | PyMem_DEL(envlist); |
| 4319 | fail: |
| 4320 | free_string_array(argvlist, argc); |
| 4321 | return NULL; |
| 4322 | } |
| 4323 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4324 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4325 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4326 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4327 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4328 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4329 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4330 | mode: mode of process creation\n\ |
| 4331 | path: path of executable file\n\ |
| 4332 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4333 | |
| 4334 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4335 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4336 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4337 | PyObject *opath; |
| 4338 | char *path; |
| 4339 | PyObject *argv; |
| 4340 | char **argvlist; |
| 4341 | int mode, i; |
| 4342 | Py_ssize_t argc; |
| 4343 | Py_intptr_t spawnval; |
| 4344 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4345 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4346 | /* spawnv has three arguments: (mode, path, argv), where |
| 4347 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4348 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4349 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4350 | PyUnicode_FSConverter, |
| 4351 | &opath, &argv)) |
| 4352 | return NULL; |
| 4353 | path = PyBytes_AsString(opath); |
| 4354 | if (PyList_Check(argv)) { |
| 4355 | argc = PyList_Size(argv); |
| 4356 | getitem = PyList_GetItem; |
| 4357 | } |
| 4358 | else if (PyTuple_Check(argv)) { |
| 4359 | argc = PyTuple_Size(argv); |
| 4360 | getitem = PyTuple_GetItem; |
| 4361 | } |
| 4362 | else { |
| 4363 | PyErr_SetString(PyExc_TypeError, |
| 4364 | "spawnv() arg 2 must be a tuple or list"); |
| 4365 | Py_DECREF(opath); |
| 4366 | return NULL; |
| 4367 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4368 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4369 | argvlist = PyMem_NEW(char *, argc+1); |
| 4370 | if (argvlist == NULL) { |
| 4371 | Py_DECREF(opath); |
| 4372 | return PyErr_NoMemory(); |
| 4373 | } |
| 4374 | for (i = 0; i < argc; i++) { |
| 4375 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4376 | &argvlist[i])) { |
| 4377 | free_string_array(argvlist, i); |
| 4378 | PyErr_SetString( |
| 4379 | PyExc_TypeError, |
| 4380 | "spawnv() arg 2 must contain only strings"); |
| 4381 | Py_DECREF(opath); |
| 4382 | return NULL; |
| 4383 | } |
| 4384 | } |
| 4385 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4386 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4387 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4388 | Py_BEGIN_ALLOW_THREADS |
| 4389 | spawnval = spawnv(mode, path, argvlist); |
| 4390 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4391 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4392 | if (mode == _OLD_P_OVERLAY) |
| 4393 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4394 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4395 | Py_BEGIN_ALLOW_THREADS |
| 4396 | spawnval = _spawnv(mode, path, argvlist); |
| 4397 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4398 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4399 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4400 | free_string_array(argvlist, argc); |
| 4401 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4402 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4403 | if (spawnval == -1) |
| 4404 | return posix_error(); |
| 4405 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4406 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4407 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4408 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4409 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4410 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4414 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4415 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4416 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4417 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4418 | mode: mode of process creation\n\ |
| 4419 | path: path of executable file\n\ |
| 4420 | args: tuple or list of arguments\n\ |
| 4421 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4422 | |
| 4423 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4424 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4425 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4426 | PyObject *opath; |
| 4427 | char *path; |
| 4428 | PyObject *argv, *env; |
| 4429 | char **argvlist; |
| 4430 | char **envlist; |
| 4431 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4432 | int mode; |
| 4433 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4434 | Py_intptr_t spawnval; |
| 4435 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4436 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4437 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4438 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4439 | argv is a list or tuple of strings and env is a dictionary |
| 4440 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4441 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4442 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4443 | PyUnicode_FSConverter, |
| 4444 | &opath, &argv, &env)) |
| 4445 | return NULL; |
| 4446 | path = PyBytes_AsString(opath); |
| 4447 | if (PyList_Check(argv)) { |
| 4448 | argc = PyList_Size(argv); |
| 4449 | getitem = PyList_GetItem; |
| 4450 | } |
| 4451 | else if (PyTuple_Check(argv)) { |
| 4452 | argc = PyTuple_Size(argv); |
| 4453 | getitem = PyTuple_GetItem; |
| 4454 | } |
| 4455 | else { |
| 4456 | PyErr_SetString(PyExc_TypeError, |
| 4457 | "spawnve() arg 2 must be a tuple or list"); |
| 4458 | goto fail_0; |
| 4459 | } |
| 4460 | if (!PyMapping_Check(env)) { |
| 4461 | PyErr_SetString(PyExc_TypeError, |
| 4462 | "spawnve() arg 3 must be a mapping object"); |
| 4463 | goto fail_0; |
| 4464 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4465 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4466 | argvlist = PyMem_NEW(char *, argc+1); |
| 4467 | if (argvlist == NULL) { |
| 4468 | PyErr_NoMemory(); |
| 4469 | goto fail_0; |
| 4470 | } |
| 4471 | for (i = 0; i < argc; i++) { |
| 4472 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4473 | &argvlist[i])) |
| 4474 | { |
| 4475 | lastarg = i; |
| 4476 | goto fail_1; |
| 4477 | } |
| 4478 | } |
| 4479 | lastarg = argc; |
| 4480 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4481 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4482 | envlist = parse_envlist(env, &envc); |
| 4483 | if (envlist == NULL) |
| 4484 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4485 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4486 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4487 | Py_BEGIN_ALLOW_THREADS |
| 4488 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4489 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4490 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4491 | if (mode == _OLD_P_OVERLAY) |
| 4492 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4493 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4494 | Py_BEGIN_ALLOW_THREADS |
| 4495 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4496 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4497 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4498 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4499 | if (spawnval == -1) |
| 4500 | (void) posix_error(); |
| 4501 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4502 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4503 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4504 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4505 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4506 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4508 | while (--envc >= 0) |
| 4509 | PyMem_DEL(envlist[envc]); |
| 4510 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4511 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4512 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4513 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4514 | Py_DECREF(opath); |
| 4515 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4516 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4517 | |
| 4518 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4519 | #if defined(PYOS_OS2) |
| 4520 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4521 | "spawnvp(mode, file, args)\n\n\ |
| 4522 | Execute the program 'file' in a new process, using the environment\n\ |
| 4523 | search path to find the file.\n\ |
| 4524 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4525 | mode: mode of process creation\n\ |
| 4526 | file: executable file name\n\ |
| 4527 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4528 | |
| 4529 | static PyObject * |
| 4530 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4531 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4532 | PyObject *opath; |
| 4533 | char *path; |
| 4534 | PyObject *argv; |
| 4535 | char **argvlist; |
| 4536 | int mode, i, argc; |
| 4537 | Py_intptr_t spawnval; |
| 4538 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4539 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4540 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4541 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4542 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4543 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4544 | PyUnicode_FSConverter, |
| 4545 | &opath, &argv)) |
| 4546 | return NULL; |
| 4547 | path = PyBytes_AsString(opath); |
| 4548 | if (PyList_Check(argv)) { |
| 4549 | argc = PyList_Size(argv); |
| 4550 | getitem = PyList_GetItem; |
| 4551 | } |
| 4552 | else if (PyTuple_Check(argv)) { |
| 4553 | argc = PyTuple_Size(argv); |
| 4554 | getitem = PyTuple_GetItem; |
| 4555 | } |
| 4556 | else { |
| 4557 | PyErr_SetString(PyExc_TypeError, |
| 4558 | "spawnvp() arg 2 must be a tuple or list"); |
| 4559 | Py_DECREF(opath); |
| 4560 | return NULL; |
| 4561 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4562 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4563 | argvlist = PyMem_NEW(char *, argc+1); |
| 4564 | if (argvlist == NULL) { |
| 4565 | Py_DECREF(opath); |
| 4566 | return PyErr_NoMemory(); |
| 4567 | } |
| 4568 | for (i = 0; i < argc; i++) { |
| 4569 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4570 | &argvlist[i])) { |
| 4571 | free_string_array(argvlist, i); |
| 4572 | PyErr_SetString( |
| 4573 | PyExc_TypeError, |
| 4574 | "spawnvp() arg 2 must contain only strings"); |
| 4575 | Py_DECREF(opath); |
| 4576 | return NULL; |
| 4577 | } |
| 4578 | } |
| 4579 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4580 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4581 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4582 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4583 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4584 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4585 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4586 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4587 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4588 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4589 | free_string_array(argvlist, argc); |
| 4590 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4591 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4592 | if (spawnval == -1) |
| 4593 | return posix_error(); |
| 4594 | else |
| 4595 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | |
| 4599 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4600 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4601 | Execute the program 'file' in a new process, using the environment\n\ |
| 4602 | search path to find the file.\n\ |
| 4603 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4604 | mode: mode of process creation\n\ |
| 4605 | file: executable file name\n\ |
| 4606 | args: tuple or list of arguments\n\ |
| 4607 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4608 | |
| 4609 | static PyObject * |
| 4610 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4611 | { |
Ross Lagerwall | dcfde5a | 2011-11-04 07:09:14 +0200 | [diff] [blame] | 4612 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4613 | char *path; |
| 4614 | PyObject *argv, *env; |
| 4615 | char **argvlist; |
| 4616 | char **envlist; |
| 4617 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4618 | int mode; |
| 4619 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4620 | Py_intptr_t spawnval; |
| 4621 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4622 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4623 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4624 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4625 | argv is a list or tuple of strings and env is a dictionary |
| 4626 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4627 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4628 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4629 | PyUnicode_FSConverter, |
| 4630 | &opath, &argv, &env)) |
| 4631 | return NULL; |
| 4632 | path = PyBytes_AsString(opath); |
| 4633 | if (PyList_Check(argv)) { |
| 4634 | argc = PyList_Size(argv); |
| 4635 | getitem = PyList_GetItem; |
| 4636 | } |
| 4637 | else if (PyTuple_Check(argv)) { |
| 4638 | argc = PyTuple_Size(argv); |
| 4639 | getitem = PyTuple_GetItem; |
| 4640 | } |
| 4641 | else { |
| 4642 | PyErr_SetString(PyExc_TypeError, |
| 4643 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4644 | goto fail_0; |
| 4645 | } |
| 4646 | if (!PyMapping_Check(env)) { |
| 4647 | PyErr_SetString(PyExc_TypeError, |
| 4648 | "spawnvpe() arg 3 must be a mapping object"); |
| 4649 | goto fail_0; |
| 4650 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4651 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4652 | argvlist = PyMem_NEW(char *, argc+1); |
| 4653 | if (argvlist == NULL) { |
| 4654 | PyErr_NoMemory(); |
| 4655 | goto fail_0; |
| 4656 | } |
| 4657 | for (i = 0; i < argc; i++) { |
| 4658 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4659 | &argvlist[i])) |
| 4660 | { |
| 4661 | lastarg = i; |
| 4662 | goto fail_1; |
| 4663 | } |
| 4664 | } |
| 4665 | lastarg = argc; |
| 4666 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4667 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4668 | envlist = parse_envlist(env, &envc); |
| 4669 | if (envlist == NULL) |
| 4670 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4671 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4672 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4673 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4674 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4675 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4676 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4677 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4678 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4679 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4680 | if (spawnval == -1) |
| 4681 | (void) posix_error(); |
| 4682 | else |
| 4683 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4684 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4685 | while (--envc >= 0) |
| 4686 | PyMem_DEL(envlist[envc]); |
| 4687 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4688 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4689 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4690 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4691 | Py_DECREF(opath); |
| 4692 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4693 | } |
| 4694 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4695 | #endif /* HAVE_SPAWNV */ |
| 4696 | |
| 4697 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4698 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4699 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4700 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4701 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4702 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4703 | 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] | 4704 | |
| 4705 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4706 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4707 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4708 | pid_t pid; |
| 4709 | int result = 0; |
| 4710 | _PyImport_AcquireLock(); |
| 4711 | pid = fork1(); |
| 4712 | if (pid == 0) { |
| 4713 | /* child: this clobbers and resets the import lock. */ |
| 4714 | PyOS_AfterFork(); |
| 4715 | } else { |
| 4716 | /* parent: release the import lock. */ |
| 4717 | result = _PyImport_ReleaseLock(); |
| 4718 | } |
| 4719 | if (pid == -1) |
| 4720 | return posix_error(); |
| 4721 | if (result < 0) { |
| 4722 | /* Don't clobber the OSError if the fork failed. */ |
| 4723 | PyErr_SetString(PyExc_RuntimeError, |
| 4724 | "not holding the import lock"); |
| 4725 | return NULL; |
| 4726 | } |
| 4727 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4728 | } |
| 4729 | #endif |
| 4730 | |
| 4731 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4732 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4733 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4734 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4735 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4736 | 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] | 4737 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4738 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4739 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4740 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4741 | pid_t pid; |
| 4742 | int result = 0; |
| 4743 | _PyImport_AcquireLock(); |
| 4744 | pid = fork(); |
| 4745 | if (pid == 0) { |
| 4746 | /* child: this clobbers and resets the import lock. */ |
| 4747 | PyOS_AfterFork(); |
| 4748 | } else { |
| 4749 | /* parent: release the import lock. */ |
| 4750 | result = _PyImport_ReleaseLock(); |
| 4751 | } |
| 4752 | if (pid == -1) |
| 4753 | return posix_error(); |
| 4754 | if (result < 0) { |
| 4755 | /* Don't clobber the OSError if the fork failed. */ |
| 4756 | PyErr_SetString(PyExc_RuntimeError, |
| 4757 | "not holding the import lock"); |
| 4758 | return NULL; |
| 4759 | } |
| 4760 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4761 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4762 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4763 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4764 | #ifdef HAVE_SCHED_H |
| 4765 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4766 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 4767 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4768 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 4769 | "sched_get_priority_max(policy)\n\n\ |
| 4770 | Get the maximum scheduling priority for *policy*."); |
| 4771 | |
| 4772 | static PyObject * |
| 4773 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 4774 | { |
| 4775 | int policy, max; |
| 4776 | |
| 4777 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 4778 | return NULL; |
| 4779 | max = sched_get_priority_max(policy); |
| 4780 | if (max < 0) |
| 4781 | return posix_error(); |
| 4782 | return PyLong_FromLong(max); |
| 4783 | } |
| 4784 | |
| 4785 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 4786 | "sched_get_priority_min(policy)\n\n\ |
| 4787 | Get the minimum scheduling priority for *policy*."); |
| 4788 | |
| 4789 | static PyObject * |
| 4790 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 4791 | { |
| 4792 | int policy, min; |
| 4793 | |
| 4794 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 4795 | return NULL; |
| 4796 | min = sched_get_priority_min(policy); |
| 4797 | if (min < 0) |
| 4798 | return posix_error(); |
| 4799 | return PyLong_FromLong(min); |
| 4800 | } |
| 4801 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4802 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 4803 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4804 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4805 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4806 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 4807 | "sched_getscheduler(pid)\n\n\ |
| 4808 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 4809 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 4810 | |
| 4811 | static PyObject * |
| 4812 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 4813 | { |
| 4814 | pid_t pid; |
| 4815 | int policy; |
| 4816 | |
| 4817 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 4818 | return NULL; |
| 4819 | policy = sched_getscheduler(pid); |
| 4820 | if (policy < 0) |
| 4821 | return posix_error(); |
| 4822 | return PyLong_FromLong(policy); |
| 4823 | } |
| 4824 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4825 | #endif |
| 4826 | |
| 4827 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 4828 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4829 | static PyObject * |
| 4830 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4831 | { |
| 4832 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 4833 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4834 | |
| 4835 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 4836 | return NULL; |
| 4837 | res = PyStructSequence_New(type); |
| 4838 | if (!res) |
| 4839 | return NULL; |
| 4840 | Py_INCREF(priority); |
| 4841 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4842 | return res; |
| 4843 | } |
| 4844 | |
| 4845 | PyDoc_STRVAR(sched_param__doc__, |
| 4846 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 4847 | Current has only one field: sched_priority"); |
| 4848 | |
| 4849 | static PyStructSequence_Field sched_param_fields[] = { |
| 4850 | {"sched_priority", "the scheduling priority"}, |
| 4851 | {0} |
| 4852 | }; |
| 4853 | |
| 4854 | static PyStructSequence_Desc sched_param_desc = { |
| 4855 | "sched_param", /* name */ |
| 4856 | sched_param__doc__, /* doc */ |
| 4857 | sched_param_fields, |
| 4858 | 1 |
| 4859 | }; |
| 4860 | |
| 4861 | static int |
| 4862 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 4863 | { |
| 4864 | long priority; |
| 4865 | |
| 4866 | if (Py_TYPE(param) != &SchedParamType) { |
| 4867 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 4868 | return 0; |
| 4869 | } |
| 4870 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 4871 | if (priority == -1 && PyErr_Occurred()) |
| 4872 | return 0; |
| 4873 | if (priority > INT_MAX || priority < INT_MIN) { |
| 4874 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 4875 | return 0; |
| 4876 | } |
| 4877 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 4878 | return 1; |
| 4879 | } |
| 4880 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4881 | #endif |
| 4882 | |
| 4883 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4884 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4885 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 4886 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 4887 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 4888 | If *pid* is 0, the calling process is changed.\n\ |
| 4889 | *param* is an instance of sched_param."); |
| 4890 | |
| 4891 | static PyObject * |
| 4892 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 4893 | { |
| 4894 | pid_t pid; |
| 4895 | int policy; |
| 4896 | struct sched_param param; |
| 4897 | |
| 4898 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 4899 | &pid, &policy, &convert_sched_param, ¶m)) |
| 4900 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4901 | |
| 4902 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 4903 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 4904 | ** scheduling policy under Solaris/Illumos, and others. |
| 4905 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4906 | */ |
| 4907 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4908 | return posix_error(); |
| 4909 | Py_RETURN_NONE; |
| 4910 | } |
| 4911 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4912 | #endif |
| 4913 | |
| 4914 | #ifdef HAVE_SCHED_SETPARAM |
| 4915 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4916 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 4917 | "sched_getparam(pid) -> sched_param\n\n\ |
| 4918 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 4919 | sched_param class. A PID of 0 means the calling process."); |
| 4920 | |
| 4921 | static PyObject * |
| 4922 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 4923 | { |
| 4924 | pid_t pid; |
| 4925 | struct sched_param param; |
| 4926 | PyObject *res, *priority; |
| 4927 | |
| 4928 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 4929 | return NULL; |
| 4930 | if (sched_getparam(pid, ¶m)) |
| 4931 | return posix_error(); |
| 4932 | res = PyStructSequence_New(&SchedParamType); |
| 4933 | if (!res) |
| 4934 | return NULL; |
| 4935 | priority = PyLong_FromLong(param.sched_priority); |
| 4936 | if (!priority) { |
| 4937 | Py_DECREF(res); |
| 4938 | return NULL; |
| 4939 | } |
| 4940 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4941 | return res; |
| 4942 | } |
| 4943 | |
| 4944 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 4945 | "sched_setparam(pid, param)\n\n\ |
| 4946 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 4947 | A PID of 0 means the calling process."); |
| 4948 | |
| 4949 | static PyObject * |
| 4950 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 4951 | { |
| 4952 | pid_t pid; |
| 4953 | struct sched_param param; |
| 4954 | |
| 4955 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 4956 | &pid, &convert_sched_param, ¶m)) |
| 4957 | return NULL; |
| 4958 | if (sched_setparam(pid, ¶m)) |
| 4959 | return posix_error(); |
| 4960 | Py_RETURN_NONE; |
| 4961 | } |
| 4962 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4963 | #endif |
| 4964 | |
| 4965 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 4966 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4967 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 4968 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 4969 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 4970 | |
| 4971 | static PyObject * |
| 4972 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 4973 | { |
| 4974 | pid_t pid; |
| 4975 | struct timespec interval; |
| 4976 | |
| 4977 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 4978 | return NULL; |
| 4979 | if (sched_rr_get_interval(pid, &interval)) |
| 4980 | return posix_error(); |
| 4981 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 4982 | } |
| 4983 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4984 | #endif |
| 4985 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4986 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 4987 | "sched_yield()\n\n\ |
| 4988 | Voluntarily relinquish the CPU."); |
| 4989 | |
| 4990 | static PyObject * |
| 4991 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 4992 | { |
| 4993 | if (sched_yield()) |
| 4994 | return posix_error(); |
| 4995 | Py_RETURN_NONE; |
| 4996 | } |
| 4997 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 4998 | #ifdef HAVE_SCHED_SETAFFINITY |
| 4999 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5000 | typedef struct { |
| 5001 | PyObject_HEAD; |
| 5002 | Py_ssize_t size; |
| 5003 | int ncpus; |
| 5004 | cpu_set_t *set; |
| 5005 | } Py_cpu_set; |
| 5006 | |
| 5007 | static PyTypeObject cpu_set_type; |
| 5008 | |
| 5009 | static void |
| 5010 | cpu_set_dealloc(Py_cpu_set *set) |
| 5011 | { |
| 5012 | assert(set->set); |
| 5013 | CPU_FREE(set->set); |
| 5014 | Py_TYPE(set)->tp_free(set); |
| 5015 | } |
| 5016 | |
| 5017 | static Py_cpu_set * |
| 5018 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 5019 | { |
| 5020 | Py_cpu_set *set; |
| 5021 | |
| 5022 | if (size < 0) { |
| 5023 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 5024 | return NULL; |
| 5025 | } |
| 5026 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 5027 | if (!set) |
| 5028 | return NULL; |
| 5029 | set->ncpus = size; |
| 5030 | set->size = CPU_ALLOC_SIZE(size); |
| 5031 | set->set = CPU_ALLOC(size); |
| 5032 | if (!set->set) { |
| 5033 | type->tp_free(set); |
| 5034 | PyErr_NoMemory(); |
| 5035 | return NULL; |
| 5036 | } |
| 5037 | CPU_ZERO_S(set->size, set->set); |
| 5038 | return set; |
| 5039 | } |
| 5040 | |
| 5041 | static PyObject * |
| 5042 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5043 | { |
| 5044 | int size; |
| 5045 | |
| 5046 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 5047 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 5048 | return NULL; |
| 5049 | return (PyObject *)make_new_cpu_set(type, size); |
| 5050 | } |
| 5051 | |
| 5052 | static PyObject * |
| 5053 | cpu_set_repr(Py_cpu_set *set) |
| 5054 | { |
| 5055 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 5056 | } |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5057 | |
| 5058 | static Py_ssize_t |
| 5059 | cpu_set_len(Py_cpu_set *set) |
| 5060 | { |
| 5061 | return set->ncpus; |
| 5062 | } |
| 5063 | |
| 5064 | static int |
| 5065 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 5066 | { |
| 5067 | int cpu; |
| 5068 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 5069 | return -1; |
| 5070 | if (cpu < 0) { |
| 5071 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 5072 | return -1; |
| 5073 | } |
| 5074 | if (cpu >= set->ncpus) { |
| 5075 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 5076 | return -1; |
| 5077 | } |
| 5078 | return cpu; |
| 5079 | } |
| 5080 | |
| 5081 | PyDoc_STRVAR(cpu_set_set_doc, |
| 5082 | "cpu_set.set(i)\n\n\ |
| 5083 | Add CPU *i* to the set."); |
| 5084 | |
| 5085 | static PyObject * |
| 5086 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 5087 | { |
| 5088 | int cpu = _get_cpu(set, "i|set", args); |
| 5089 | if (cpu == -1) |
| 5090 | return NULL; |
| 5091 | CPU_SET_S(cpu, set->size, set->set); |
| 5092 | Py_RETURN_NONE; |
| 5093 | } |
| 5094 | |
| 5095 | PyDoc_STRVAR(cpu_set_count_doc, |
| 5096 | "cpu_set.count() -> int\n\n\ |
| 5097 | Return the number of CPUs active in the set."); |
| 5098 | |
| 5099 | static PyObject * |
| 5100 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 5101 | { |
| 5102 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 5103 | } |
| 5104 | |
| 5105 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 5106 | "cpu_set.clear(i)\n\n\ |
| 5107 | Remove CPU *i* from the set."); |
| 5108 | |
| 5109 | static PyObject * |
| 5110 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 5111 | { |
| 5112 | int cpu = _get_cpu(set, "i|clear", args); |
| 5113 | if (cpu == -1) |
| 5114 | return NULL; |
| 5115 | CPU_CLR_S(cpu, set->size, set->set); |
| 5116 | Py_RETURN_NONE; |
| 5117 | } |
| 5118 | |
| 5119 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 5120 | "cpu_set.isset(i) -> bool\n\n\ |
| 5121 | Test if CPU *i* is in the set."); |
| 5122 | |
| 5123 | static PyObject * |
| 5124 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 5125 | { |
| 5126 | int cpu = _get_cpu(set, "i|isset", args); |
| 5127 | if (cpu == -1) |
| 5128 | return NULL; |
| 5129 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 5130 | Py_RETURN_TRUE; |
| 5131 | Py_RETURN_FALSE; |
| 5132 | } |
| 5133 | |
| 5134 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 5135 | "cpu_set.zero()\n\n\ |
| 5136 | Clear the cpu_set."); |
| 5137 | |
| 5138 | static PyObject * |
| 5139 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 5140 | { |
| 5141 | CPU_ZERO_S(set->size, set->set); |
| 5142 | Py_RETURN_NONE; |
| 5143 | } |
| 5144 | |
| 5145 | static PyObject * |
| 5146 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 5147 | { |
| 5148 | int eq; |
| 5149 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5150 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) |
| 5151 | Py_RETURN_NOTIMPLEMENTED; |
| 5152 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5153 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 5154 | if ((op == Py_EQ) ? eq : !eq) |
| 5155 | Py_RETURN_TRUE; |
| 5156 | else |
| 5157 | Py_RETURN_FALSE; |
| 5158 | } |
| 5159 | |
| 5160 | #define CPU_SET_BINOP(name, op) \ |
| 5161 | static PyObject * \ |
| 5162 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 5163 | if (res) { \ |
| 5164 | Py_INCREF(res); \ |
| 5165 | } \ |
| 5166 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 5167 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5168 | if (!res) \ |
| 5169 | return NULL; \ |
| 5170 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 5171 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5172 | Py_DECREF(res); \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5173 | Py_RETURN_NOTIMPLEMENTED; \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5174 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 5175 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5176 | op(res->size, res->set, left->set, right->set); \ |
| 5177 | return (PyObject *)res; \ |
| 5178 | } \ |
| 5179 | static PyObject * \ |
| 5180 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5181 | return do_cpu_set_##name(left, right, NULL); \ |
| 5182 | } \ |
| 5183 | static PyObject * \ |
| 5184 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5185 | return do_cpu_set_##name(left, right, left); \ |
| 5186 | } \ |
| 5187 | |
| 5188 | CPU_SET_BINOP(and, CPU_AND_S) |
| 5189 | CPU_SET_BINOP(or, CPU_OR_S) |
| 5190 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 5191 | #undef CPU_SET_BINOP |
| 5192 | |
| 5193 | PyDoc_STRVAR(cpu_set_doc, |
| 5194 | "cpu_set(size)\n\n\ |
| 5195 | Create an empty mask of CPUs."); |
| 5196 | |
| 5197 | static PyNumberMethods cpu_set_as_number = { |
| 5198 | 0, /*nb_add*/ |
| 5199 | 0, /*nb_subtract*/ |
| 5200 | 0, /*nb_multiply*/ |
| 5201 | 0, /*nb_remainder*/ |
| 5202 | 0, /*nb_divmod*/ |
| 5203 | 0, /*nb_power*/ |
| 5204 | 0, /*nb_negative*/ |
| 5205 | 0, /*nb_positive*/ |
| 5206 | 0, /*nb_absolute*/ |
| 5207 | 0, /*nb_bool*/ |
| 5208 | 0, /*nb_invert*/ |
| 5209 | 0, /*nb_lshift*/ |
| 5210 | 0, /*nb_rshift*/ |
| 5211 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 5212 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 5213 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 5214 | 0, /*nb_int*/ |
| 5215 | 0, /*nb_reserved*/ |
| 5216 | 0, /*nb_float*/ |
| 5217 | 0, /*nb_inplace_add*/ |
| 5218 | 0, /*nb_inplace_subtract*/ |
| 5219 | 0, /*nb_inplace_multiply*/ |
| 5220 | 0, /*nb_inplace_remainder*/ |
| 5221 | 0, /*nb_inplace_power*/ |
| 5222 | 0, /*nb_inplace_lshift*/ |
| 5223 | 0, /*nb_inplace_rshift*/ |
| 5224 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5225 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5226 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5227 | }; |
| 5228 | |
| 5229 | static PySequenceMethods cpu_set_as_sequence = { |
| 5230 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5231 | }; |
| 5232 | |
| 5233 | static PyMethodDef cpu_set_methods[] = { |
| 5234 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5235 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5236 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5237 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5238 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5239 | {NULL, NULL} /* sentinel */ |
| 5240 | }; |
| 5241 | |
| 5242 | static PyTypeObject cpu_set_type = { |
| 5243 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5244 | "posix.cpu_set", /* tp_name */ |
| 5245 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5246 | 0, /* tp_itemsize */ |
| 5247 | /* methods */ |
| 5248 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5249 | 0, /* tp_print */ |
| 5250 | 0, /* tp_getattr */ |
| 5251 | 0, /* tp_setattr */ |
| 5252 | 0, /* tp_reserved */ |
| 5253 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5254 | &cpu_set_as_number, /* tp_as_number */ |
| 5255 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5256 | 0, /* tp_as_mapping */ |
| 5257 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5258 | 0, /* tp_call */ |
| 5259 | 0, /* tp_str */ |
| 5260 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5261 | 0, /* tp_setattro */ |
| 5262 | 0, /* tp_as_buffer */ |
| 5263 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5264 | cpu_set_doc, /* tp_doc */ |
| 5265 | 0, /* tp_traverse */ |
| 5266 | 0, /* tp_clear */ |
| 5267 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5268 | 0, /* tp_weaklistoffset */ |
| 5269 | 0, /* tp_iter */ |
| 5270 | 0, /* tp_iternext */ |
| 5271 | cpu_set_methods, /* tp_methods */ |
| 5272 | 0, /* tp_members */ |
| 5273 | 0, /* tp_getset */ |
| 5274 | 0, /* tp_base */ |
| 5275 | 0, /* tp_dict */ |
| 5276 | 0, /* tp_descr_get */ |
| 5277 | 0, /* tp_descr_set */ |
| 5278 | 0, /* tp_dictoffset */ |
| 5279 | 0, /* tp_init */ |
| 5280 | PyType_GenericAlloc, /* tp_alloc */ |
| 5281 | cpu_set_new, /* tp_new */ |
| 5282 | PyObject_Del, /* tp_free */ |
| 5283 | }; |
| 5284 | |
| 5285 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5286 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5287 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5288 | |
| 5289 | static PyObject * |
| 5290 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5291 | { |
| 5292 | pid_t pid; |
| 5293 | Py_cpu_set *cpu_set; |
| 5294 | |
Benjamin Peterson | a17a5d6 | 2011-08-09 16:49:13 -0500 | [diff] [blame] | 5295 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5296 | &pid, &cpu_set_type, &cpu_set)) |
| 5297 | return NULL; |
| 5298 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5299 | return posix_error(); |
| 5300 | Py_RETURN_NONE; |
| 5301 | } |
| 5302 | |
| 5303 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5304 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5305 | Return the affinity of the process with PID *pid*.\n\ |
| 5306 | The returned cpu_set will be of size *ncpus*."); |
| 5307 | |
| 5308 | static PyObject * |
| 5309 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5310 | { |
| 5311 | pid_t pid; |
| 5312 | int ncpus; |
| 5313 | Py_cpu_set *res; |
| 5314 | |
Benjamin Peterson | 7ac9214 | 2011-08-03 08:54:26 -0500 | [diff] [blame] | 5315 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5316 | &pid, &ncpus)) |
| 5317 | return NULL; |
| 5318 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5319 | if (!res) |
| 5320 | return NULL; |
| 5321 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5322 | Py_DECREF(res); |
| 5323 | return posix_error(); |
| 5324 | } |
| 5325 | return (PyObject *)res; |
| 5326 | } |
| 5327 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5328 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5329 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5330 | #endif /* HAVE_SCHED_H */ |
| 5331 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5332 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5333 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5334 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5335 | #define DEV_PTY_FILE "/dev/ptc" |
| 5336 | #define HAVE_DEV_PTMX |
| 5337 | #else |
| 5338 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5339 | #endif |
| 5340 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5341 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5342 | #ifdef HAVE_PTY_H |
| 5343 | #include <pty.h> |
| 5344 | #else |
| 5345 | #ifdef HAVE_LIBUTIL_H |
| 5346 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5347 | #else |
| 5348 | #ifdef HAVE_UTIL_H |
| 5349 | #include <util.h> |
| 5350 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5351 | #endif /* HAVE_LIBUTIL_H */ |
| 5352 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5353 | #ifdef HAVE_STROPTS_H |
| 5354 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5355 | #endif |
| 5356 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5357 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5358 | #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] | 5359 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5360 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5361 | 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] | 5362 | |
| 5363 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5364 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5365 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5366 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5367 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5368 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5369 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5370 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5371 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5372 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5373 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5374 | #endif |
| 5375 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5376 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5377 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5378 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5379 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5380 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5381 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5382 | if (slave_name == NULL) |
| 5383 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5384 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5385 | slave_fd = open(slave_name, O_RDWR); |
| 5386 | if (slave_fd < 0) |
| 5387 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5388 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5389 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5390 | if (master_fd < 0) |
| 5391 | return posix_error(); |
| 5392 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5393 | /* change permission of slave */ |
| 5394 | if (grantpt(master_fd) < 0) { |
| 5395 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5396 | return posix_error(); |
| 5397 | } |
| 5398 | /* unlock slave */ |
| 5399 | if (unlockpt(master_fd) < 0) { |
| 5400 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5401 | return posix_error(); |
| 5402 | } |
| 5403 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5404 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5405 | if (slave_name == NULL) |
| 5406 | return posix_error(); |
| 5407 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5408 | if (slave_fd < 0) |
| 5409 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5410 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5411 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5412 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5413 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5414 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5415 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5416 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5417 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5418 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5419 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5420 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5421 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5422 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5423 | |
| 5424 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5425 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5426 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5427 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5428 | 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] | 5429 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5430 | |
| 5431 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5432 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5433 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5434 | int master_fd = -1, result = 0; |
| 5435 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5436 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5437 | _PyImport_AcquireLock(); |
| 5438 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5439 | if (pid == 0) { |
| 5440 | /* child: this clobbers and resets the import lock. */ |
| 5441 | PyOS_AfterFork(); |
| 5442 | } else { |
| 5443 | /* parent: release the import lock. */ |
| 5444 | result = _PyImport_ReleaseLock(); |
| 5445 | } |
| 5446 | if (pid == -1) |
| 5447 | return posix_error(); |
| 5448 | if (result < 0) { |
| 5449 | /* Don't clobber the OSError if the fork failed. */ |
| 5450 | PyErr_SetString(PyExc_RuntimeError, |
| 5451 | "not holding the import lock"); |
| 5452 | return NULL; |
| 5453 | } |
| 5454 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5455 | } |
| 5456 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5457 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5458 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5459 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5460 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5461 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5462 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5463 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5464 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5465 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5466 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5467 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5468 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5469 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5470 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5471 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5472 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5473 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5474 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5475 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5476 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5477 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5478 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5479 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5480 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5481 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5482 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5483 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5484 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5485 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5486 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5487 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5488 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5489 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5490 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5491 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5492 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5493 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5494 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5495 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5496 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5497 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5498 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5499 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5500 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5501 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5502 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5503 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5504 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5505 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5508 | #ifdef HAVE_GETGROUPLIST |
| 5509 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5510 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5511 | Returns a list of groups to which a user belongs.\n\n\ |
| 5512 | user: username to lookup\n\ |
| 5513 | group: base group id of the user"); |
| 5514 | |
| 5515 | static PyObject * |
| 5516 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5517 | { |
| 5518 | #ifdef NGROUPS_MAX |
| 5519 | #define MAX_GROUPS NGROUPS_MAX |
| 5520 | #else |
| 5521 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5522 | #define MAX_GROUPS 64 |
| 5523 | #endif |
| 5524 | |
| 5525 | const char *user; |
| 5526 | int i, ngroups; |
| 5527 | PyObject *list; |
| 5528 | #ifdef __APPLE__ |
| 5529 | int *groups, basegid; |
| 5530 | #else |
| 5531 | gid_t *groups, basegid; |
| 5532 | #endif |
| 5533 | ngroups = MAX_GROUPS; |
| 5534 | |
| 5535 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5536 | return NULL; |
| 5537 | |
| 5538 | #ifdef __APPLE__ |
| 5539 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5540 | #else |
| 5541 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5542 | #endif |
| 5543 | if (groups == NULL) |
| 5544 | return PyErr_NoMemory(); |
| 5545 | |
| 5546 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5547 | PyMem_Del(groups); |
| 5548 | return posix_error(); |
| 5549 | } |
| 5550 | |
| 5551 | list = PyList_New(ngroups); |
| 5552 | if (list == NULL) { |
| 5553 | PyMem_Del(groups); |
| 5554 | return NULL; |
| 5555 | } |
| 5556 | |
| 5557 | for (i = 0; i < ngroups; i++) { |
| 5558 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5559 | if (o == NULL) { |
| 5560 | Py_DECREF(list); |
| 5561 | PyMem_Del(groups); |
| 5562 | return NULL; |
| 5563 | } |
| 5564 | PyList_SET_ITEM(list, i, o); |
| 5565 | } |
| 5566 | |
| 5567 | PyMem_Del(groups); |
| 5568 | |
| 5569 | return list; |
| 5570 | } |
| 5571 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5572 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5573 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5574 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5575 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5576 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5577 | |
| 5578 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5579 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5580 | { |
| 5581 | PyObject *result = NULL; |
| 5582 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5583 | #ifdef NGROUPS_MAX |
| 5584 | #define MAX_GROUPS NGROUPS_MAX |
| 5585 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5586 | /* 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] | 5587 | #define MAX_GROUPS 64 |
| 5588 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5589 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5590 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5591 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5592 | * This is a helper variable to store the intermediate result when |
| 5593 | * that happens. |
| 5594 | * |
| 5595 | * To keep the code readable the OSX behaviour is unconditional, |
| 5596 | * according to the POSIX spec this should be safe on all unix-y |
| 5597 | * systems. |
| 5598 | */ |
| 5599 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5600 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5601 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5602 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5603 | if (n < 0) { |
| 5604 | if (errno == EINVAL) { |
| 5605 | n = getgroups(0, NULL); |
| 5606 | if (n == -1) { |
| 5607 | return posix_error(); |
| 5608 | } |
| 5609 | if (n == 0) { |
| 5610 | /* Avoid malloc(0) */ |
| 5611 | alt_grouplist = grouplist; |
| 5612 | } else { |
| 5613 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5614 | if (alt_grouplist == NULL) { |
| 5615 | errno = EINVAL; |
| 5616 | return posix_error(); |
| 5617 | } |
| 5618 | n = getgroups(n, alt_grouplist); |
| 5619 | if (n == -1) { |
| 5620 | PyMem_Free(alt_grouplist); |
| 5621 | return posix_error(); |
| 5622 | } |
| 5623 | } |
| 5624 | } else { |
| 5625 | return posix_error(); |
| 5626 | } |
| 5627 | } |
| 5628 | result = PyList_New(n); |
| 5629 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5630 | int i; |
| 5631 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5632 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5633 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5634 | Py_DECREF(result); |
| 5635 | result = NULL; |
| 5636 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5637 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5638 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5639 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
| 5642 | if (alt_grouplist != grouplist) { |
| 5643 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5644 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5645 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5646 | return result; |
| 5647 | } |
| 5648 | #endif |
| 5649 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5650 | #ifdef HAVE_INITGROUPS |
| 5651 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5652 | "initgroups(username, gid) -> None\n\n\ |
| 5653 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5654 | the groups of which the specified username is a member, plus the specified\n\ |
| 5655 | group id."); |
| 5656 | |
| 5657 | static PyObject * |
| 5658 | posix_initgroups(PyObject *self, PyObject *args) |
| 5659 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5660 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5661 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5662 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5663 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5664 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5665 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5666 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5667 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5668 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5669 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5670 | res = initgroups(username, (gid_t) gid); |
| 5671 | Py_DECREF(oname); |
| 5672 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5673 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5674 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5675 | Py_INCREF(Py_None); |
| 5676 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5677 | } |
| 5678 | #endif |
| 5679 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5680 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5681 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5682 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5683 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5684 | |
| 5685 | static PyObject * |
| 5686 | posix_getpgid(PyObject *self, PyObject *args) |
| 5687 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5688 | pid_t pid, pgid; |
| 5689 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5690 | return NULL; |
| 5691 | pgid = getpgid(pid); |
| 5692 | if (pgid < 0) |
| 5693 | return posix_error(); |
| 5694 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5695 | } |
| 5696 | #endif /* HAVE_GETPGID */ |
| 5697 | |
| 5698 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5699 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5700 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5701 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5702 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5703 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5704 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5705 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5706 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5707 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5708 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5709 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5710 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5711 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5712 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5713 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5714 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5715 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5716 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5717 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5718 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5719 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5721 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5722 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5723 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5724 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5725 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5726 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5727 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5728 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5729 | return posix_error(); |
| 5730 | Py_INCREF(Py_None); |
| 5731 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5734 | #endif /* HAVE_SETPGRP */ |
| 5735 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5736 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5737 | |
| 5738 | #ifdef MS_WINDOWS |
| 5739 | #include <tlhelp32.h> |
| 5740 | |
| 5741 | static PyObject* |
| 5742 | win32_getppid() |
| 5743 | { |
| 5744 | HANDLE snapshot; |
| 5745 | pid_t mypid; |
| 5746 | PyObject* result = NULL; |
| 5747 | BOOL have_record; |
| 5748 | PROCESSENTRY32 pe; |
| 5749 | |
| 5750 | mypid = getpid(); /* This function never fails */ |
| 5751 | |
| 5752 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 5753 | if (snapshot == INVALID_HANDLE_VALUE) |
| 5754 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5755 | |
| 5756 | pe.dwSize = sizeof(pe); |
| 5757 | have_record = Process32First(snapshot, &pe); |
| 5758 | while (have_record) { |
| 5759 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 5760 | /* We could cache the ulong value in a static variable. */ |
| 5761 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 5762 | break; |
| 5763 | } |
| 5764 | |
| 5765 | have_record = Process32Next(snapshot, &pe); |
| 5766 | } |
| 5767 | |
| 5768 | /* If our loop exits and our pid was not found (result will be NULL) |
| 5769 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 5770 | * error anyway, so let's raise it. */ |
| 5771 | if (!result) |
| 5772 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5773 | |
| 5774 | CloseHandle(snapshot); |
| 5775 | |
| 5776 | return result; |
| 5777 | } |
| 5778 | #endif /*MS_WINDOWS*/ |
| 5779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5780 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5781 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5782 | Return the parent's process id. If the parent process has already exited,\n\ |
| 5783 | Windows machines will still return its id; others systems will return the id\n\ |
| 5784 | of the 'init' process (1)."); |
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_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5788 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5789 | #ifdef MS_WINDOWS |
| 5790 | return win32_getppid(); |
| 5791 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5792 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5793 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5794 | } |
| 5795 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5796 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5797 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5798 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5799 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5800 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5801 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5802 | |
| 5803 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5804 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5805 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5806 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5807 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5808 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 5809 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5810 | |
| 5811 | if (GetUserNameW(user_name, &num_chars)) { |
| 5812 | /* num_chars is the number of unicode chars plus null terminator */ |
| 5813 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5814 | } |
| 5815 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5816 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5817 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5818 | char *name; |
| 5819 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5821 | errno = 0; |
| 5822 | name = getlogin(); |
| 5823 | if (name == NULL) { |
| 5824 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5825 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5826 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5827 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5828 | } |
| 5829 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5830 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5831 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5832 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5833 | return result; |
| 5834 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5835 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5836 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5837 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5838 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5839 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5840 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5841 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5842 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5843 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5844 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5845 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5846 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5847 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5848 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5849 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5850 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5851 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5852 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5853 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5854 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5855 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5856 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5857 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5858 | pid_t pid; |
| 5859 | int sig; |
| 5860 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 5861 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5862 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5863 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 5864 | APIRET rc; |
| 5865 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5866 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5867 | |
| 5868 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 5869 | APIRET rc; |
| 5870 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5871 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5872 | |
| 5873 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5874 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5875 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5876 | if (kill(pid, sig) == -1) |
| 5877 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5878 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5879 | Py_INCREF(Py_None); |
| 5880 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5881 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5882 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5883 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5884 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5885 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5886 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5887 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5888 | |
| 5889 | static PyObject * |
| 5890 | posix_killpg(PyObject *self, PyObject *args) |
| 5891 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5892 | int sig; |
| 5893 | pid_t pgid; |
| 5894 | /* XXX some man pages make the `pgid` parameter an int, others |
| 5895 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 5896 | take the same type. Moreover, pid_t is always at least as wide as |
| 5897 | int (else compilation of this module fails), which is safe. */ |
| 5898 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5899 | return NULL; |
| 5900 | if (killpg(pgid, sig) == -1) |
| 5901 | return posix_error(); |
| 5902 | Py_INCREF(Py_None); |
| 5903 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5904 | } |
| 5905 | #endif |
| 5906 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5907 | #ifdef MS_WINDOWS |
| 5908 | PyDoc_STRVAR(win32_kill__doc__, |
| 5909 | "kill(pid, sig)\n\n\ |
| 5910 | Kill a process with a signal."); |
| 5911 | |
| 5912 | static PyObject * |
| 5913 | win32_kill(PyObject *self, PyObject *args) |
| 5914 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 5915 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5916 | DWORD pid, sig, err; |
| 5917 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5918 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5919 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 5920 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5921 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5922 | /* Console processes which share a common console can be sent CTRL+C or |
| 5923 | CTRL+BREAK events, provided they handle said events. */ |
| 5924 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 5925 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 5926 | err = GetLastError(); |
| 5927 | PyErr_SetFromWindowsErr(err); |
| 5928 | } |
| 5929 | else |
| 5930 | Py_RETURN_NONE; |
| 5931 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5933 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 5934 | attempt to open and terminate the process. */ |
| 5935 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 5936 | if (handle == NULL) { |
| 5937 | err = GetLastError(); |
| 5938 | return PyErr_SetFromWindowsErr(err); |
| 5939 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5940 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5941 | if (TerminateProcess(handle, sig) == 0) { |
| 5942 | err = GetLastError(); |
| 5943 | result = PyErr_SetFromWindowsErr(err); |
| 5944 | } else { |
| 5945 | Py_INCREF(Py_None); |
| 5946 | result = Py_None; |
| 5947 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5948 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5949 | CloseHandle(handle); |
| 5950 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5951 | } |
| 5952 | #endif /* MS_WINDOWS */ |
| 5953 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5954 | #ifdef HAVE_PLOCK |
| 5955 | |
| 5956 | #ifdef HAVE_SYS_LOCK_H |
| 5957 | #include <sys/lock.h> |
| 5958 | #endif |
| 5959 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5960 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5961 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5962 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5963 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5964 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5965 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5966 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5967 | int op; |
| 5968 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 5969 | return NULL; |
| 5970 | if (plock(op) == -1) |
| 5971 | return posix_error(); |
| 5972 | Py_INCREF(Py_None); |
| 5973 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5974 | } |
| 5975 | #endif |
| 5976 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5977 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5978 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5979 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5980 | Set the current process's user id."); |
| 5981 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5982 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5983 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5984 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5985 | long uid_arg; |
| 5986 | uid_t uid; |
| 5987 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5988 | return NULL; |
| 5989 | uid = uid_arg; |
| 5990 | if (uid != uid_arg) { |
| 5991 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5992 | return NULL; |
| 5993 | } |
| 5994 | if (setuid(uid) < 0) |
| 5995 | return posix_error(); |
| 5996 | Py_INCREF(Py_None); |
| 5997 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5998 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5999 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6000 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6001 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6002 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6003 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6004 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6005 | Set the current process's effective user id."); |
| 6006 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6007 | static PyObject * |
| 6008 | posix_seteuid (PyObject *self, PyObject *args) |
| 6009 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6010 | long euid_arg; |
| 6011 | uid_t euid; |
| 6012 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 6013 | return NULL; |
| 6014 | euid = euid_arg; |
| 6015 | if (euid != euid_arg) { |
| 6016 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6017 | return NULL; |
| 6018 | } |
| 6019 | if (seteuid(euid) < 0) { |
| 6020 | return posix_error(); |
| 6021 | } else { |
| 6022 | Py_INCREF(Py_None); |
| 6023 | return Py_None; |
| 6024 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6025 | } |
| 6026 | #endif /* HAVE_SETEUID */ |
| 6027 | |
| 6028 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6029 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6030 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6031 | Set the current process's effective group id."); |
| 6032 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6033 | static PyObject * |
| 6034 | posix_setegid (PyObject *self, PyObject *args) |
| 6035 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6036 | long egid_arg; |
| 6037 | gid_t egid; |
| 6038 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 6039 | return NULL; |
| 6040 | egid = egid_arg; |
| 6041 | if (egid != egid_arg) { |
| 6042 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6043 | return NULL; |
| 6044 | } |
| 6045 | if (setegid(egid) < 0) { |
| 6046 | return posix_error(); |
| 6047 | } else { |
| 6048 | Py_INCREF(Py_None); |
| 6049 | return Py_None; |
| 6050 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6051 | } |
| 6052 | #endif /* HAVE_SETEGID */ |
| 6053 | |
| 6054 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6055 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6056 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6057 | Set the current process's real and effective user ids."); |
| 6058 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6059 | static PyObject * |
| 6060 | posix_setreuid (PyObject *self, PyObject *args) |
| 6061 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6062 | long ruid_arg, euid_arg; |
| 6063 | uid_t ruid, euid; |
| 6064 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 6065 | return NULL; |
| 6066 | if (ruid_arg == -1) |
| 6067 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 6068 | else |
| 6069 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 6070 | if (euid_arg == -1) |
| 6071 | euid = (uid_t)-1; |
| 6072 | else |
| 6073 | euid = euid_arg; |
| 6074 | if ((euid_arg != -1 && euid != euid_arg) || |
| 6075 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 6076 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6077 | return NULL; |
| 6078 | } |
| 6079 | if (setreuid(ruid, euid) < 0) { |
| 6080 | return posix_error(); |
| 6081 | } else { |
| 6082 | Py_INCREF(Py_None); |
| 6083 | return Py_None; |
| 6084 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6085 | } |
| 6086 | #endif /* HAVE_SETREUID */ |
| 6087 | |
| 6088 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6089 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6090 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6091 | Set the current process's real and effective group ids."); |
| 6092 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6093 | static PyObject * |
| 6094 | posix_setregid (PyObject *self, PyObject *args) |
| 6095 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6096 | long rgid_arg, egid_arg; |
| 6097 | gid_t rgid, egid; |
| 6098 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 6099 | return NULL; |
| 6100 | if (rgid_arg == -1) |
| 6101 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 6102 | else |
| 6103 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6104 | if (egid_arg == -1) |
| 6105 | egid = (gid_t)-1; |
| 6106 | else |
| 6107 | egid = egid_arg; |
| 6108 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6109 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6110 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6111 | return NULL; |
| 6112 | } |
| 6113 | if (setregid(rgid, egid) < 0) { |
| 6114 | return posix_error(); |
| 6115 | } else { |
| 6116 | Py_INCREF(Py_None); |
| 6117 | return Py_None; |
| 6118 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6119 | } |
| 6120 | #endif /* HAVE_SETREGID */ |
| 6121 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6122 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6123 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6124 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6125 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6127 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6128 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6129 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6130 | long gid_arg; |
| 6131 | gid_t gid; |
| 6132 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6133 | return NULL; |
| 6134 | gid = gid_arg; |
| 6135 | if (gid != gid_arg) { |
| 6136 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6137 | return NULL; |
| 6138 | } |
| 6139 | if (setgid(gid) < 0) |
| 6140 | return posix_error(); |
| 6141 | Py_INCREF(Py_None); |
| 6142 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6143 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6144 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6145 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6146 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6147 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6148 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6149 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6150 | |
| 6151 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6152 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6153 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6154 | int i, len; |
| 6155 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6156 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6157 | if (!PySequence_Check(groups)) { |
| 6158 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6159 | return NULL; |
| 6160 | } |
| 6161 | len = PySequence_Size(groups); |
| 6162 | if (len > MAX_GROUPS) { |
| 6163 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6164 | return NULL; |
| 6165 | } |
| 6166 | for(i = 0; i < len; i++) { |
| 6167 | PyObject *elem; |
| 6168 | elem = PySequence_GetItem(groups, i); |
| 6169 | if (!elem) |
| 6170 | return NULL; |
| 6171 | if (!PyLong_Check(elem)) { |
| 6172 | PyErr_SetString(PyExc_TypeError, |
| 6173 | "groups must be integers"); |
| 6174 | Py_DECREF(elem); |
| 6175 | return NULL; |
| 6176 | } else { |
| 6177 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6178 | if (PyErr_Occurred()) { |
| 6179 | PyErr_SetString(PyExc_TypeError, |
| 6180 | "group id too big"); |
| 6181 | Py_DECREF(elem); |
| 6182 | return NULL; |
| 6183 | } |
| 6184 | grouplist[i] = x; |
| 6185 | /* read back the value to see if it fitted in gid_t */ |
| 6186 | if (grouplist[i] != x) { |
| 6187 | PyErr_SetString(PyExc_TypeError, |
| 6188 | "group id too big"); |
| 6189 | Py_DECREF(elem); |
| 6190 | return NULL; |
| 6191 | } |
| 6192 | } |
| 6193 | Py_DECREF(elem); |
| 6194 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6195 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6196 | if (setgroups(len, grouplist) < 0) |
| 6197 | return posix_error(); |
| 6198 | Py_INCREF(Py_None); |
| 6199 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6200 | } |
| 6201 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6202 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6203 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6204 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6205 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6206 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6207 | PyObject *result; |
| 6208 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6209 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6210 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6211 | if (pid == -1) |
| 6212 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6213 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6214 | if (struct_rusage == NULL) { |
| 6215 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6216 | if (m == NULL) |
| 6217 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6218 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6219 | Py_DECREF(m); |
| 6220 | if (struct_rusage == NULL) |
| 6221 | return NULL; |
| 6222 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6223 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6224 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6225 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6226 | if (!result) |
| 6227 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6228 | |
| 6229 | #ifndef doubletime |
| 6230 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6231 | #endif |
| 6232 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6233 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6234 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6235 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6236 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6237 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6238 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6239 | SET_INT(result, 2, ru->ru_maxrss); |
| 6240 | SET_INT(result, 3, ru->ru_ixrss); |
| 6241 | SET_INT(result, 4, ru->ru_idrss); |
| 6242 | SET_INT(result, 5, ru->ru_isrss); |
| 6243 | SET_INT(result, 6, ru->ru_minflt); |
| 6244 | SET_INT(result, 7, ru->ru_majflt); |
| 6245 | SET_INT(result, 8, ru->ru_nswap); |
| 6246 | SET_INT(result, 9, ru->ru_inblock); |
| 6247 | SET_INT(result, 10, ru->ru_oublock); |
| 6248 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6249 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6250 | SET_INT(result, 13, ru->ru_nsignals); |
| 6251 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6252 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6253 | #undef SET_INT |
| 6254 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6255 | if (PyErr_Occurred()) { |
| 6256 | Py_DECREF(result); |
| 6257 | return NULL; |
| 6258 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6259 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6260 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6261 | } |
| 6262 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6263 | |
| 6264 | #ifdef HAVE_WAIT3 |
| 6265 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6266 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6267 | Wait for completion of a child process."); |
| 6268 | |
| 6269 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6270 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6271 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6272 | pid_t pid; |
| 6273 | int options; |
| 6274 | struct rusage ru; |
| 6275 | WAIT_TYPE status; |
| 6276 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6277 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6278 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6279 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6280 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6281 | Py_BEGIN_ALLOW_THREADS |
| 6282 | pid = wait3(&status, options, &ru); |
| 6283 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6284 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6285 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6286 | } |
| 6287 | #endif /* HAVE_WAIT3 */ |
| 6288 | |
| 6289 | #ifdef HAVE_WAIT4 |
| 6290 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6291 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6292 | Wait for completion of a given child process."); |
| 6293 | |
| 6294 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6295 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6296 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6297 | pid_t pid; |
| 6298 | int options; |
| 6299 | struct rusage ru; |
| 6300 | WAIT_TYPE status; |
| 6301 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6302 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6303 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6304 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6305 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6306 | Py_BEGIN_ALLOW_THREADS |
| 6307 | pid = wait4(pid, &status, options, &ru); |
| 6308 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6309 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6310 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6311 | } |
| 6312 | #endif /* HAVE_WAIT4 */ |
| 6313 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6314 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6315 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6316 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6317 | Wait for the completion of one or more child processes.\n\n\ |
| 6318 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6319 | id specifies the pid to wait on.\n\ |
| 6320 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6321 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6322 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6323 | no children in a waitable state."); |
| 6324 | |
| 6325 | static PyObject * |
| 6326 | posix_waitid(PyObject *self, PyObject *args) |
| 6327 | { |
| 6328 | PyObject *result; |
| 6329 | idtype_t idtype; |
| 6330 | id_t id; |
| 6331 | int options, res; |
| 6332 | siginfo_t si; |
| 6333 | si.si_pid = 0; |
| 6334 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6335 | return NULL; |
| 6336 | Py_BEGIN_ALLOW_THREADS |
| 6337 | res = waitid(idtype, id, &si, options); |
| 6338 | Py_END_ALLOW_THREADS |
| 6339 | if (res == -1) |
| 6340 | return posix_error(); |
| 6341 | |
| 6342 | if (si.si_pid == 0) |
| 6343 | Py_RETURN_NONE; |
| 6344 | |
| 6345 | result = PyStructSequence_New(&WaitidResultType); |
| 6346 | if (!result) |
| 6347 | return NULL; |
| 6348 | |
| 6349 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6350 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6351 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6352 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6353 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6354 | if (PyErr_Occurred()) { |
| 6355 | Py_DECREF(result); |
| 6356 | return NULL; |
| 6357 | } |
| 6358 | |
| 6359 | return result; |
| 6360 | } |
| 6361 | #endif |
| 6362 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6363 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6364 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6365 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6366 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6367 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6368 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6369 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6370 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6371 | pid_t pid; |
| 6372 | int options; |
| 6373 | WAIT_TYPE status; |
| 6374 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6375 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6376 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6377 | return NULL; |
| 6378 | Py_BEGIN_ALLOW_THREADS |
| 6379 | pid = waitpid(pid, &status, options); |
| 6380 | Py_END_ALLOW_THREADS |
| 6381 | if (pid == -1) |
| 6382 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6383 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6384 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6387 | #elif defined(HAVE_CWAIT) |
| 6388 | |
| 6389 | /* 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] | 6390 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6391 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6392 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6393 | |
| 6394 | static PyObject * |
| 6395 | posix_waitpid(PyObject *self, PyObject *args) |
| 6396 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6397 | Py_intptr_t pid; |
| 6398 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6399 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6400 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6401 | return NULL; |
| 6402 | Py_BEGIN_ALLOW_THREADS |
| 6403 | pid = _cwait(&status, pid, options); |
| 6404 | Py_END_ALLOW_THREADS |
| 6405 | if (pid == -1) |
| 6406 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6407 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6408 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6409 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6410 | } |
| 6411 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6412 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6413 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6414 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6415 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6416 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6417 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6418 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6419 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6420 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6421 | pid_t pid; |
| 6422 | WAIT_TYPE status; |
| 6423 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6424 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6425 | Py_BEGIN_ALLOW_THREADS |
| 6426 | pid = wait(&status); |
| 6427 | Py_END_ALLOW_THREADS |
| 6428 | if (pid == -1) |
| 6429 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6430 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6431 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6432 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6433 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6434 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6435 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6436 | PyDoc_STRVAR(posix_lstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6437 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6438 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6439 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6440 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6441 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6442 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6443 | #ifdef HAVE_LSTAT |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6444 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6445 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6446 | #ifdef MS_WINDOWS |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6447 | return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6448 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6449 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 6450 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6451 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6452 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6453 | } |
| 6454 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6455 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6456 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6457 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6458 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6459 | 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] | 6460 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6461 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6462 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6463 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6464 | PyObject* v; |
| 6465 | char buf[MAXPATHLEN]; |
| 6466 | PyObject *opath; |
| 6467 | char *path; |
| 6468 | int n; |
| 6469 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6470 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6471 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 6472 | PyUnicode_FSConverter, &opath)) |
| 6473 | return NULL; |
| 6474 | path = PyBytes_AsString(opath); |
| 6475 | v = PySequence_GetItem(args, 0); |
| 6476 | if (v == NULL) { |
| 6477 | Py_DECREF(opath); |
| 6478 | return NULL; |
| 6479 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6480 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6481 | if (PyUnicode_Check(v)) { |
| 6482 | arg_is_unicode = 1; |
| 6483 | } |
| 6484 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6485 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6486 | Py_BEGIN_ALLOW_THREADS |
| 6487 | n = readlink(path, buf, (int) sizeof buf); |
| 6488 | Py_END_ALLOW_THREADS |
| 6489 | if (n < 0) |
| 6490 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6491 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6492 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6493 | if (arg_is_unicode) |
| 6494 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 6495 | else |
| 6496 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6497 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6498 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6499 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6500 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6501 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6502 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6503 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6504 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6505 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6506 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6507 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6508 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6509 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6510 | } |
| 6511 | #endif /* HAVE_SYMLINK */ |
| 6512 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6513 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6514 | |
| 6515 | PyDoc_STRVAR(win_readlink__doc__, |
| 6516 | "readlink(path) -> path\n\n\ |
| 6517 | Return a string representing the path to which the symbolic link points."); |
| 6518 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6519 | /* Windows readlink implementation */ |
| 6520 | static PyObject * |
| 6521 | win_readlink(PyObject *self, PyObject *args) |
| 6522 | { |
| 6523 | wchar_t *path; |
| 6524 | DWORD n_bytes_returned; |
| 6525 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6526 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6527 | HANDLE reparse_point_handle; |
| 6528 | |
| 6529 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6530 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6531 | wchar_t *print_name; |
| 6532 | |
| 6533 | if (!PyArg_ParseTuple(args, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6534 | "U:readlink", |
| 6535 | &po)) |
| 6536 | return NULL; |
| 6537 | path = PyUnicode_AsUnicode(po); |
| 6538 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6539 | return NULL; |
| 6540 | |
| 6541 | /* First get a handle to the reparse point */ |
| 6542 | Py_BEGIN_ALLOW_THREADS |
| 6543 | reparse_point_handle = CreateFileW( |
| 6544 | path, |
| 6545 | 0, |
| 6546 | 0, |
| 6547 | 0, |
| 6548 | OPEN_EXISTING, |
| 6549 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6550 | 0); |
| 6551 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6552 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6553 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6554 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6555 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6556 | Py_BEGIN_ALLOW_THREADS |
| 6557 | /* New call DeviceIoControl to read the reparse point */ |
| 6558 | io_result = DeviceIoControl( |
| 6559 | reparse_point_handle, |
| 6560 | FSCTL_GET_REPARSE_POINT, |
| 6561 | 0, 0, /* in buffer */ |
| 6562 | target_buffer, sizeof(target_buffer), |
| 6563 | &n_bytes_returned, |
| 6564 | 0 /* we're not using OVERLAPPED_IO */ |
| 6565 | ); |
| 6566 | CloseHandle(reparse_point_handle); |
| 6567 | Py_END_ALLOW_THREADS |
| 6568 | |
| 6569 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6570 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6571 | |
| 6572 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6573 | { |
| 6574 | PyErr_SetString(PyExc_ValueError, |
| 6575 | "not a symbolic link"); |
| 6576 | return NULL; |
| 6577 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6578 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6579 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6580 | |
| 6581 | result = PyUnicode_FromWideChar(print_name, |
| 6582 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6583 | return result; |
| 6584 | } |
| 6585 | |
| 6586 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6587 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6588 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6589 | |
| 6590 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6591 | static int has_CreateSymbolicLinkW = 0; |
| 6592 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 6593 | static int |
| 6594 | check_CreateSymbolicLinkW() |
| 6595 | { |
| 6596 | HINSTANCE hKernel32; |
| 6597 | /* only recheck */ |
| 6598 | if (has_CreateSymbolicLinkW) |
| 6599 | return has_CreateSymbolicLinkW; |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 6600 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6601 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6602 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6603 | if (Py_CreateSymbolicLinkW) |
| 6604 | has_CreateSymbolicLinkW = 1; |
| 6605 | return has_CreateSymbolicLinkW; |
| 6606 | } |
| 6607 | |
| 6608 | PyDoc_STRVAR(win_symlink__doc__, |
| 6609 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 6610 | Create a symbolic link pointing to src named dst.\n\ |
| 6611 | target_is_directory is required if the target is to be interpreted as\n\ |
| 6612 | a directory.\n\ |
| 6613 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 6614 | NotImplementedError otherwise."); |
| 6615 | |
| 6616 | static PyObject * |
| 6617 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6618 | { |
| 6619 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6620 | PyObject *osrc, *odest; |
| 6621 | PyObject *usrc = NULL, *udest = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6622 | wchar_t *wsrc, *wdest; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6623 | int target_is_directory = 0; |
| 6624 | DWORD res; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6625 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6626 | if (!check_CreateSymbolicLinkW()) |
| 6627 | { |
| 6628 | /* raise NotImplementedError */ |
| 6629 | return PyErr_Format(PyExc_NotImplementedError, |
| 6630 | "CreateSymbolicLinkW not found"); |
| 6631 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6632 | if (!PyArg_ParseTupleAndKeywords( |
| 6633 | args, kwargs, "OO|i:symlink", kwlist, |
| 6634 | &osrc, &odest, &target_is_directory)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6635 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6636 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6637 | usrc = win32_decode_filename(osrc); |
| 6638 | if (!usrc) |
| 6639 | return NULL; |
| 6640 | udest = win32_decode_filename(odest); |
| 6641 | if (!udest) |
| 6642 | goto error; |
| 6643 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6644 | if (win32_can_symlink == 0) |
| 6645 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 6646 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6647 | wsrc = PyUnicode_AsUnicode(usrc); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6648 | if (wsrc == NULL) |
| 6649 | goto error; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6650 | wdest = PyUnicode_AsUnicode(udest); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6651 | if (wsrc == NULL) |
| 6652 | goto error; |
| 6653 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6654 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6655 | res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6656 | Py_END_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6657 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6658 | Py_DECREF(usrc); |
| 6659 | Py_DECREF(udest); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6660 | if (!res) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6661 | return win32_error_object("symlink", osrc); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6662 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6663 | Py_INCREF(Py_None); |
| 6664 | return Py_None; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6665 | |
| 6666 | error: |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6667 | Py_XDECREF(usrc); |
| 6668 | Py_XDECREF(udest); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6669 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6670 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6671 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6672 | |
| 6673 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6674 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6675 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6676 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6677 | { |
| 6678 | ULONG value = 0; |
| 6679 | |
| 6680 | Py_BEGIN_ALLOW_THREADS |
| 6681 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6682 | Py_END_ALLOW_THREADS |
| 6683 | |
| 6684 | return value; |
| 6685 | } |
| 6686 | |
| 6687 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6688 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6689 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6690 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6691 | return Py_BuildValue("ddddd", |
| 6692 | (double)0 /* t.tms_utime / HZ */, |
| 6693 | (double)0 /* t.tms_stime / HZ */, |
| 6694 | (double)0 /* t.tms_cutime / HZ */, |
| 6695 | (double)0 /* t.tms_cstime / HZ */, |
| 6696 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6697 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6698 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 6699 | #define NEED_TICKS_PER_SECOND |
| 6700 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6701 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6702 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6703 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6704 | struct tms t; |
| 6705 | clock_t c; |
| 6706 | errno = 0; |
| 6707 | c = times(&t); |
| 6708 | if (c == (clock_t) -1) |
| 6709 | return posix_error(); |
| 6710 | return Py_BuildValue("ddddd", |
| 6711 | (double)t.tms_utime / ticks_per_second, |
| 6712 | (double)t.tms_stime / ticks_per_second, |
| 6713 | (double)t.tms_cutime / ticks_per_second, |
| 6714 | (double)t.tms_cstime / ticks_per_second, |
| 6715 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6716 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6717 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6718 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6719 | |
| 6720 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6721 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6722 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6723 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6724 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6725 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6726 | FILETIME create, exit, kernel, user; |
| 6727 | HANDLE hProc; |
| 6728 | hProc = GetCurrentProcess(); |
| 6729 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6730 | /* The fields of a FILETIME structure are the hi and lo part |
| 6731 | of a 64-bit value expressed in 100 nanosecond units. |
| 6732 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6733 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6734 | */ |
| 6735 | return Py_BuildValue( |
| 6736 | "ddddd", |
| 6737 | (double)(user.dwHighDateTime*429.4967296 + |
| 6738 | user.dwLowDateTime*1e-7), |
| 6739 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6740 | kernel.dwLowDateTime*1e-7), |
| 6741 | (double)0, |
| 6742 | (double)0, |
| 6743 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6744 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6745 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6746 | |
| 6747 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6748 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6749 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6750 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6751 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6752 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6753 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6754 | #ifdef HAVE_GETSID |
| 6755 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6756 | "getsid(pid) -> sid\n\n\ |
| 6757 | Call the system call getsid()."); |
| 6758 | |
| 6759 | static PyObject * |
| 6760 | posix_getsid(PyObject *self, PyObject *args) |
| 6761 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6762 | pid_t pid; |
| 6763 | int sid; |
| 6764 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 6765 | return NULL; |
| 6766 | sid = getsid(pid); |
| 6767 | if (sid < 0) |
| 6768 | return posix_error(); |
| 6769 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6770 | } |
| 6771 | #endif /* HAVE_GETSID */ |
| 6772 | |
| 6773 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6774 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6775 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6776 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6777 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6778 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6779 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6780 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6781 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6782 | if (setsid() < 0) |
| 6783 | return posix_error(); |
| 6784 | Py_INCREF(Py_None); |
| 6785 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6786 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6787 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6788 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6789 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6790 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6791 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6792 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6793 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6794 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6795 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6796 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6797 | pid_t pid; |
| 6798 | int pgrp; |
| 6799 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6800 | return NULL; |
| 6801 | if (setpgid(pid, pgrp) < 0) |
| 6802 | return posix_error(); |
| 6803 | Py_INCREF(Py_None); |
| 6804 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6805 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6806 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6807 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6808 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6809 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6810 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6811 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6812 | 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] | 6813 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6814 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6815 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6816 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6817 | int fd; |
| 6818 | pid_t pgid; |
| 6819 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6820 | return NULL; |
| 6821 | pgid = tcgetpgrp(fd); |
| 6822 | if (pgid < 0) |
| 6823 | return posix_error(); |
| 6824 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6825 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6826 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6827 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6828 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6829 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6830 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6831 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6832 | 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] | 6833 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6834 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6835 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6836 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6837 | int fd; |
| 6838 | pid_t pgid; |
| 6839 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6840 | return NULL; |
| 6841 | if (tcsetpgrp(fd, pgid) < 0) |
| 6842 | return posix_error(); |
| 6843 | Py_INCREF(Py_None); |
| 6844 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6845 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6846 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6847 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6848 | /* Functions acting on file descriptors */ |
| 6849 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6850 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6851 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6852 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6853 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6854 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6855 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6856 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6857 | PyObject *ofile; |
| 6858 | char *file; |
| 6859 | int flag; |
| 6860 | int mode = 0777; |
| 6861 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6862 | |
| 6863 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6864 | PyObject *po; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6865 | if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6866 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 6867 | if (wpath == NULL) |
| 6868 | return NULL; |
| 6869 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6870 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6871 | fd = _wopen(wpath, flag, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6872 | Py_END_ALLOW_THREADS |
| 6873 | if (fd < 0) |
| 6874 | return posix_error(); |
| 6875 | return PyLong_FromLong((long)fd); |
| 6876 | } |
| 6877 | /* Drop the argument parsing error as narrow strings |
| 6878 | are also valid. */ |
| 6879 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6880 | #endif |
| 6881 | |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6882 | if (!PyArg_ParseTuple(args, "O&i|i:open", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6883 | PyUnicode_FSConverter, &ofile, |
| 6884 | &flag, &mode)) |
| 6885 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6886 | #ifdef MS_WINDOWS |
| 6887 | if (win32_warn_bytes_api()) { |
| 6888 | Py_DECREF(ofile); |
| 6889 | return NULL; |
| 6890 | } |
| 6891 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6892 | file = PyBytes_AsString(ofile); |
| 6893 | Py_BEGIN_ALLOW_THREADS |
| 6894 | fd = open(file, flag, mode); |
| 6895 | Py_END_ALLOW_THREADS |
| 6896 | if (fd < 0) |
| 6897 | return posix_error_with_allocated_filename(ofile); |
| 6898 | Py_DECREF(ofile); |
| 6899 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6900 | } |
| 6901 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6902 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6903 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6904 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6905 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6906 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6907 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6908 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6909 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6910 | int fd, res; |
| 6911 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6912 | return NULL; |
| 6913 | if (!_PyVerify_fd(fd)) |
| 6914 | return posix_error(); |
| 6915 | Py_BEGIN_ALLOW_THREADS |
| 6916 | res = close(fd); |
| 6917 | Py_END_ALLOW_THREADS |
| 6918 | if (res < 0) |
| 6919 | return posix_error(); |
| 6920 | Py_INCREF(Py_None); |
| 6921 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6922 | } |
| 6923 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6924 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6925 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6926 | "closerange(fd_low, fd_high)\n\n\ |
| 6927 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6928 | |
| 6929 | static PyObject * |
| 6930 | posix_closerange(PyObject *self, PyObject *args) |
| 6931 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6932 | int fd_from, fd_to, i; |
| 6933 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6934 | return NULL; |
| 6935 | Py_BEGIN_ALLOW_THREADS |
| 6936 | for (i = fd_from; i < fd_to; i++) |
| 6937 | if (_PyVerify_fd(i)) |
| 6938 | close(i); |
| 6939 | Py_END_ALLOW_THREADS |
| 6940 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6941 | } |
| 6942 | |
| 6943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6944 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6945 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6946 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6947 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6948 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6949 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6950 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6951 | int fd; |
| 6952 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6953 | return NULL; |
| 6954 | if (!_PyVerify_fd(fd)) |
| 6955 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6956 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6957 | if (fd < 0) |
| 6958 | return posix_error(); |
| 6959 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6960 | } |
| 6961 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6962 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6963 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6964 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6965 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6966 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6967 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6968 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6969 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6970 | int fd, fd2, res; |
| 6971 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6972 | return NULL; |
| 6973 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6974 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6975 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6976 | if (res < 0) |
| 6977 | return posix_error(); |
| 6978 | Py_INCREF(Py_None); |
| 6979 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6980 | } |
| 6981 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6982 | #ifdef HAVE_LOCKF |
| 6983 | PyDoc_STRVAR(posix_lockf__doc__, |
| 6984 | "lockf(fd, cmd, len)\n\n\ |
| 6985 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 6986 | fd is an open file descriptor.\n\ |
| 6987 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 6988 | F_TEST.\n\ |
| 6989 | len specifies the section of the file to lock."); |
| 6990 | |
| 6991 | static PyObject * |
| 6992 | posix_lockf(PyObject *self, PyObject *args) |
| 6993 | { |
| 6994 | int fd, cmd, res; |
| 6995 | off_t len; |
| 6996 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 6997 | &fd, &cmd, _parse_off_t, &len)) |
| 6998 | return NULL; |
| 6999 | |
| 7000 | Py_BEGIN_ALLOW_THREADS |
| 7001 | res = lockf(fd, cmd, len); |
| 7002 | Py_END_ALLOW_THREADS |
| 7003 | |
| 7004 | if (res < 0) |
| 7005 | return posix_error(); |
| 7006 | |
| 7007 | Py_RETURN_NONE; |
| 7008 | } |
| 7009 | #endif |
| 7010 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7011 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7012 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7013 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 7014 | Set the current position of a file descriptor.\n\ |
| 7015 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7016 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7017 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7018 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7019 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7020 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7021 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7022 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7023 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7024 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7025 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7026 | PyObject *posobj; |
| 7027 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7028 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7029 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7030 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 7031 | switch (how) { |
| 7032 | case 0: how = SEEK_SET; break; |
| 7033 | case 1: how = SEEK_CUR; break; |
| 7034 | case 2: how = SEEK_END; break; |
| 7035 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7036 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7037 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 7038 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7039 | pos = PyLong_AsLong(posobj); |
| 7040 | #else |
| 7041 | pos = PyLong_AsLongLong(posobj); |
| 7042 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7043 | if (PyErr_Occurred()) |
| 7044 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7045 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7046 | if (!_PyVerify_fd(fd)) |
| 7047 | return posix_error(); |
| 7048 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7049 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7050 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7051 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7052 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 7053 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7054 | Py_END_ALLOW_THREADS |
| 7055 | if (res < 0) |
| 7056 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7057 | |
| 7058 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7059 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7060 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7061 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7062 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7063 | } |
| 7064 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7066 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7067 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7068 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7069 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7070 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7071 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7072 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7073 | int fd, size; |
| 7074 | Py_ssize_t n; |
| 7075 | PyObject *buffer; |
| 7076 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7077 | return NULL; |
| 7078 | if (size < 0) { |
| 7079 | errno = EINVAL; |
| 7080 | return posix_error(); |
| 7081 | } |
| 7082 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7083 | if (buffer == NULL) |
| 7084 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7085 | if (!_PyVerify_fd(fd)) { |
| 7086 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7087 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7088 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7089 | Py_BEGIN_ALLOW_THREADS |
| 7090 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7091 | Py_END_ALLOW_THREADS |
| 7092 | if (n < 0) { |
| 7093 | Py_DECREF(buffer); |
| 7094 | return posix_error(); |
| 7095 | } |
| 7096 | if (n != size) |
| 7097 | _PyBytes_Resize(&buffer, n); |
| 7098 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7099 | } |
| 7100 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7101 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7102 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7103 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7104 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7105 | { |
| 7106 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7107 | Py_ssize_t blen, total = 0; |
| 7108 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7109 | *iov = PyMem_New(struct iovec, cnt); |
| 7110 | if (*iov == NULL) { |
| 7111 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7112 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7113 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7114 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7115 | *buf = PyMem_New(Py_buffer, cnt); |
| 7116 | if (*buf == NULL) { |
| 7117 | PyMem_Del(*iov); |
| 7118 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7119 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7120 | } |
| 7121 | |
| 7122 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7123 | PyObject *item = PySequence_GetItem(seq, i); |
| 7124 | if (item == NULL) |
| 7125 | goto fail; |
| 7126 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7127 | Py_DECREF(item); |
| 7128 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7129 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7130 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7131 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7132 | blen = (*buf)[i].len; |
| 7133 | (*iov)[i].iov_len = blen; |
| 7134 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7135 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7136 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7137 | |
| 7138 | fail: |
| 7139 | PyMem_Del(*iov); |
| 7140 | for (j = 0; j < i; j++) { |
| 7141 | PyBuffer_Release(&(*buf)[j]); |
| 7142 | } |
| 7143 | PyMem_Del(*buf); |
| 7144 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7145 | } |
| 7146 | |
| 7147 | static void |
| 7148 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7149 | { |
| 7150 | int i; |
| 7151 | PyMem_Del(iov); |
| 7152 | for (i = 0; i < cnt; i++) { |
| 7153 | PyBuffer_Release(&buf[i]); |
| 7154 | } |
| 7155 | PyMem_Del(buf); |
| 7156 | } |
| 7157 | #endif |
| 7158 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7159 | #ifdef HAVE_READV |
| 7160 | PyDoc_STRVAR(posix_readv__doc__, |
| 7161 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7162 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7163 | is an arbitrary sequence of writable buffers.\n\ |
| 7164 | Returns the total number of bytes read."); |
| 7165 | |
| 7166 | static PyObject * |
| 7167 | posix_readv(PyObject *self, PyObject *args) |
| 7168 | { |
| 7169 | int fd, cnt; |
| 7170 | Py_ssize_t n; |
| 7171 | PyObject *seq; |
| 7172 | struct iovec *iov; |
| 7173 | Py_buffer *buf; |
| 7174 | |
| 7175 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7176 | return NULL; |
| 7177 | if (!PySequence_Check(seq)) { |
| 7178 | PyErr_SetString(PyExc_TypeError, |
| 7179 | "readv() arg 2 must be a sequence"); |
| 7180 | return NULL; |
| 7181 | } |
| 7182 | cnt = PySequence_Size(seq); |
| 7183 | |
| 7184 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7185 | return NULL; |
| 7186 | |
| 7187 | Py_BEGIN_ALLOW_THREADS |
| 7188 | n = readv(fd, iov, cnt); |
| 7189 | Py_END_ALLOW_THREADS |
| 7190 | |
| 7191 | iov_cleanup(iov, buf, cnt); |
| 7192 | return PyLong_FromSsize_t(n); |
| 7193 | } |
| 7194 | #endif |
| 7195 | |
| 7196 | #ifdef HAVE_PREAD |
| 7197 | PyDoc_STRVAR(posix_pread__doc__, |
| 7198 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7199 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7200 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7201 | |
| 7202 | static PyObject * |
| 7203 | posix_pread(PyObject *self, PyObject *args) |
| 7204 | { |
| 7205 | int fd, size; |
| 7206 | off_t offset; |
| 7207 | Py_ssize_t n; |
| 7208 | PyObject *buffer; |
| 7209 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7210 | return NULL; |
| 7211 | |
| 7212 | if (size < 0) { |
| 7213 | errno = EINVAL; |
| 7214 | return posix_error(); |
| 7215 | } |
| 7216 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7217 | if (buffer == NULL) |
| 7218 | return NULL; |
| 7219 | if (!_PyVerify_fd(fd)) { |
| 7220 | Py_DECREF(buffer); |
| 7221 | return posix_error(); |
| 7222 | } |
| 7223 | Py_BEGIN_ALLOW_THREADS |
| 7224 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7225 | Py_END_ALLOW_THREADS |
| 7226 | if (n < 0) { |
| 7227 | Py_DECREF(buffer); |
| 7228 | return posix_error(); |
| 7229 | } |
| 7230 | if (n != size) |
| 7231 | _PyBytes_Resize(&buffer, n); |
| 7232 | return buffer; |
| 7233 | } |
| 7234 | #endif |
| 7235 | |
| 7236 | PyDoc_STRVAR(posix_write__doc__, |
| 7237 | "write(fd, string) -> byteswritten\n\n\ |
| 7238 | Write a string to a file descriptor."); |
| 7239 | |
| 7240 | static PyObject * |
| 7241 | posix_write(PyObject *self, PyObject *args) |
| 7242 | { |
| 7243 | Py_buffer pbuf; |
| 7244 | int fd; |
| 7245 | Py_ssize_t size, len; |
| 7246 | |
| 7247 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7248 | return NULL; |
| 7249 | if (!_PyVerify_fd(fd)) { |
| 7250 | PyBuffer_Release(&pbuf); |
| 7251 | return posix_error(); |
| 7252 | } |
| 7253 | len = pbuf.len; |
| 7254 | Py_BEGIN_ALLOW_THREADS |
| 7255 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7256 | if (len > INT_MAX) |
| 7257 | len = INT_MAX; |
| 7258 | size = write(fd, pbuf.buf, (int)len); |
| 7259 | #else |
| 7260 | size = write(fd, pbuf.buf, len); |
| 7261 | #endif |
| 7262 | Py_END_ALLOW_THREADS |
| 7263 | PyBuffer_Release(&pbuf); |
| 7264 | if (size < 0) |
| 7265 | return posix_error(); |
| 7266 | return PyLong_FromSsize_t(size); |
| 7267 | } |
| 7268 | |
| 7269 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7270 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7271 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7272 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7273 | -> byteswritten\n\ |
| 7274 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7275 | |
| 7276 | static PyObject * |
| 7277 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7278 | { |
| 7279 | int in, out; |
| 7280 | Py_ssize_t ret; |
| 7281 | off_t offset; |
| 7282 | |
| 7283 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7284 | #ifndef __APPLE__ |
| 7285 | Py_ssize_t len; |
| 7286 | #endif |
| 7287 | PyObject *headers = NULL, *trailers = NULL; |
| 7288 | Py_buffer *hbuf, *tbuf; |
| 7289 | off_t sbytes; |
| 7290 | struct sf_hdtr sf; |
| 7291 | int flags = 0; |
| 7292 | sf.headers = NULL; |
| 7293 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7294 | static char *keywords[] = {"out", "in", |
| 7295 | "offset", "count", |
| 7296 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7297 | |
| 7298 | #ifdef __APPLE__ |
| 7299 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7300 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7301 | #else |
| 7302 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7303 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7304 | #endif |
| 7305 | &headers, &trailers, &flags)) |
| 7306 | return NULL; |
| 7307 | if (headers != NULL) { |
| 7308 | if (!PySequence_Check(headers)) { |
| 7309 | PyErr_SetString(PyExc_TypeError, |
| 7310 | "sendfile() headers must be a sequence or None"); |
| 7311 | return NULL; |
| 7312 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7313 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7314 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7315 | if (sf.hdr_cnt > 0 && |
| 7316 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7317 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7318 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7319 | #ifdef __APPLE__ |
| 7320 | sbytes += i; |
| 7321 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7322 | } |
| 7323 | } |
| 7324 | if (trailers != NULL) { |
| 7325 | if (!PySequence_Check(trailers)) { |
| 7326 | PyErr_SetString(PyExc_TypeError, |
| 7327 | "sendfile() trailers must be a sequence or None"); |
| 7328 | return NULL; |
| 7329 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7330 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7331 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7332 | if (sf.trl_cnt > 0 && |
| 7333 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7334 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7335 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7336 | #ifdef __APPLE__ |
| 7337 | sbytes += i; |
| 7338 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7339 | } |
| 7340 | } |
| 7341 | |
| 7342 | Py_BEGIN_ALLOW_THREADS |
| 7343 | #ifdef __APPLE__ |
| 7344 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7345 | #else |
| 7346 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7347 | #endif |
| 7348 | Py_END_ALLOW_THREADS |
| 7349 | |
| 7350 | if (sf.headers != NULL) |
| 7351 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7352 | if (sf.trailers != NULL) |
| 7353 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7354 | |
| 7355 | if (ret < 0) { |
| 7356 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7357 | if (sbytes != 0) { |
| 7358 | // some data has been sent |
| 7359 | goto done; |
| 7360 | } |
| 7361 | else { |
| 7362 | // no data has been sent; upper application is supposed |
| 7363 | // to retry on EAGAIN or EBUSY |
| 7364 | return posix_error(); |
| 7365 | } |
| 7366 | } |
| 7367 | return posix_error(); |
| 7368 | } |
| 7369 | goto done; |
| 7370 | |
| 7371 | done: |
| 7372 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7373 | return Py_BuildValue("l", sbytes); |
| 7374 | #else |
| 7375 | return Py_BuildValue("L", sbytes); |
| 7376 | #endif |
| 7377 | |
| 7378 | #else |
| 7379 | Py_ssize_t count; |
| 7380 | PyObject *offobj; |
| 7381 | static char *keywords[] = {"out", "in", |
| 7382 | "offset", "count", NULL}; |
| 7383 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7384 | keywords, &out, &in, &offobj, &count)) |
| 7385 | return NULL; |
| 7386 | #ifdef linux |
| 7387 | if (offobj == Py_None) { |
| 7388 | Py_BEGIN_ALLOW_THREADS |
| 7389 | ret = sendfile(out, in, NULL, count); |
| 7390 | Py_END_ALLOW_THREADS |
| 7391 | if (ret < 0) |
| 7392 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7393 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7394 | } |
| 7395 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7396 | if (!_parse_off_t(offobj, &offset)) |
| 7397 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7398 | Py_BEGIN_ALLOW_THREADS |
| 7399 | ret = sendfile(out, in, &offset, count); |
| 7400 | Py_END_ALLOW_THREADS |
| 7401 | if (ret < 0) |
| 7402 | return posix_error(); |
| 7403 | return Py_BuildValue("n", ret); |
| 7404 | #endif |
| 7405 | } |
| 7406 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7407 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7408 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7409 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7410 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7411 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7412 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7413 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7414 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7415 | int fd; |
| 7416 | STRUCT_STAT st; |
| 7417 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7418 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7419 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7420 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7421 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7422 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7423 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7424 | if (!_PyVerify_fd(fd)) |
| 7425 | return posix_error(); |
| 7426 | Py_BEGIN_ALLOW_THREADS |
| 7427 | res = FSTAT(fd, &st); |
| 7428 | Py_END_ALLOW_THREADS |
| 7429 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7430 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7431 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7432 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7433 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7434 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7435 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7436 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7437 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7438 | } |
| 7439 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7440 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7441 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7442 | 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] | 7443 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7444 | |
| 7445 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7446 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7447 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7448 | int fd; |
| 7449 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7450 | return NULL; |
| 7451 | if (!_PyVerify_fd(fd)) |
| 7452 | return PyBool_FromLong(0); |
| 7453 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7454 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7455 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7456 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7457 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7458 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7459 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7460 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7461 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7462 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7463 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7464 | #if defined(PYOS_OS2) |
| 7465 | HFILE read, write; |
| 7466 | APIRET rc; |
| 7467 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7468 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7469 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7470 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7471 | |
| 7472 | return Py_BuildValue("(ii)", read, write); |
| 7473 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7474 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7475 | int fds[2]; |
| 7476 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7477 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7478 | if (res != 0) |
| 7479 | return posix_error(); |
| 7480 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7481 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7482 | HANDLE read, write; |
| 7483 | int read_fd, write_fd; |
| 7484 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7485 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7486 | if (!ok) |
| 7487 | return win32_error("CreatePipe", NULL); |
| 7488 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7489 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7490 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7491 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7492 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7493 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7494 | #endif /* HAVE_PIPE */ |
| 7495 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7496 | #ifdef HAVE_PIPE2 |
| 7497 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7498 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7499 | Create a pipe with flags set atomically.\n\ |
| 7500 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7501 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7502 | "); |
| 7503 | |
| 7504 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7505 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7506 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7507 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7508 | int fds[2]; |
| 7509 | int res; |
| 7510 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7511 | flags = PyLong_AsLong(arg); |
| 7512 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7513 | return NULL; |
| 7514 | |
| 7515 | res = pipe2(fds, flags); |
| 7516 | if (res != 0) |
| 7517 | return posix_error(); |
| 7518 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7519 | } |
| 7520 | #endif /* HAVE_PIPE2 */ |
| 7521 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7522 | #ifdef HAVE_WRITEV |
| 7523 | PyDoc_STRVAR(posix_writev__doc__, |
| 7524 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7525 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7526 | arbitrary sequence of buffers.\n\ |
| 7527 | Returns the total bytes written."); |
| 7528 | |
| 7529 | static PyObject * |
| 7530 | posix_writev(PyObject *self, PyObject *args) |
| 7531 | { |
| 7532 | int fd, cnt; |
| 7533 | Py_ssize_t res; |
| 7534 | PyObject *seq; |
| 7535 | struct iovec *iov; |
| 7536 | Py_buffer *buf; |
| 7537 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7538 | return NULL; |
| 7539 | if (!PySequence_Check(seq)) { |
| 7540 | PyErr_SetString(PyExc_TypeError, |
| 7541 | "writev() arg 2 must be a sequence"); |
| 7542 | return NULL; |
| 7543 | } |
| 7544 | cnt = PySequence_Size(seq); |
| 7545 | |
| 7546 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7547 | return NULL; |
| 7548 | } |
| 7549 | |
| 7550 | Py_BEGIN_ALLOW_THREADS |
| 7551 | res = writev(fd, iov, cnt); |
| 7552 | Py_END_ALLOW_THREADS |
| 7553 | |
| 7554 | iov_cleanup(iov, buf, cnt); |
| 7555 | return PyLong_FromSsize_t(res); |
| 7556 | } |
| 7557 | #endif |
| 7558 | |
| 7559 | #ifdef HAVE_PWRITE |
| 7560 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7561 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7562 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7563 | offset unchanged."); |
| 7564 | |
| 7565 | static PyObject * |
| 7566 | posix_pwrite(PyObject *self, PyObject *args) |
| 7567 | { |
| 7568 | Py_buffer pbuf; |
| 7569 | int fd; |
| 7570 | off_t offset; |
| 7571 | Py_ssize_t size; |
| 7572 | |
| 7573 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7574 | return NULL; |
| 7575 | |
| 7576 | if (!_PyVerify_fd(fd)) { |
| 7577 | PyBuffer_Release(&pbuf); |
| 7578 | return posix_error(); |
| 7579 | } |
| 7580 | Py_BEGIN_ALLOW_THREADS |
| 7581 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7582 | Py_END_ALLOW_THREADS |
| 7583 | PyBuffer_Release(&pbuf); |
| 7584 | if (size < 0) |
| 7585 | return posix_error(); |
| 7586 | return PyLong_FromSsize_t(size); |
| 7587 | } |
| 7588 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7589 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7590 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7591 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7592 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7593 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7594 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7595 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7596 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7597 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7598 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7599 | char *filename; |
| 7600 | int mode = 0666; |
| 7601 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7602 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 7603 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7604 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7605 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7606 | Py_BEGIN_ALLOW_THREADS |
| 7607 | res = mkfifo(filename, mode); |
| 7608 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7609 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7610 | if (res < 0) |
| 7611 | return posix_error(); |
| 7612 | Py_INCREF(Py_None); |
| 7613 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7614 | } |
| 7615 | #endif |
| 7616 | |
| 7617 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7618 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7619 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7620 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7621 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7622 | named filename. mode specifies both the permissions to use and the\n\ |
| 7623 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7624 | 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] | 7625 | device defines the newly created device special file (probably using\n\ |
| 7626 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7627 | |
| 7628 | |
| 7629 | static PyObject * |
| 7630 | posix_mknod(PyObject *self, PyObject *args) |
| 7631 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7632 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7633 | char *filename; |
| 7634 | int mode = 0600; |
| 7635 | int device = 0; |
| 7636 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7637 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 7638 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7639 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7640 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7641 | Py_BEGIN_ALLOW_THREADS |
| 7642 | res = mknod(filename, mode, device); |
| 7643 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7644 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7645 | if (res < 0) |
| 7646 | return posix_error(); |
| 7647 | Py_INCREF(Py_None); |
| 7648 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7649 | } |
| 7650 | #endif |
| 7651 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7652 | #ifdef HAVE_DEVICE_MACROS |
| 7653 | PyDoc_STRVAR(posix_major__doc__, |
| 7654 | "major(device) -> major number\n\ |
| 7655 | Extracts a device major number from a raw device number."); |
| 7656 | |
| 7657 | static PyObject * |
| 7658 | posix_major(PyObject *self, PyObject *args) |
| 7659 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7660 | int device; |
| 7661 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7662 | return NULL; |
| 7663 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7664 | } |
| 7665 | |
| 7666 | PyDoc_STRVAR(posix_minor__doc__, |
| 7667 | "minor(device) -> minor number\n\ |
| 7668 | Extracts a device minor number from a raw device number."); |
| 7669 | |
| 7670 | static PyObject * |
| 7671 | posix_minor(PyObject *self, PyObject *args) |
| 7672 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7673 | int device; |
| 7674 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7675 | return NULL; |
| 7676 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7677 | } |
| 7678 | |
| 7679 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7680 | "makedev(major, minor) -> device number\n\ |
| 7681 | Composes a raw device number from the major and minor device numbers."); |
| 7682 | |
| 7683 | static PyObject * |
| 7684 | posix_makedev(PyObject *self, PyObject *args) |
| 7685 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7686 | int major, minor; |
| 7687 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7688 | return NULL; |
| 7689 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7690 | } |
| 7691 | #endif /* device macros */ |
| 7692 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7693 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7694 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7695 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7696 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7697 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7698 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7699 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7700 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7701 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7702 | int fd; |
| 7703 | off_t length; |
| 7704 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7705 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7706 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7707 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7708 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7709 | Py_BEGIN_ALLOW_THREADS |
| 7710 | res = ftruncate(fd, length); |
| 7711 | Py_END_ALLOW_THREADS |
| 7712 | if (res < 0) |
| 7713 | return posix_error(); |
| 7714 | Py_INCREF(Py_None); |
| 7715 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7716 | } |
| 7717 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7718 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7719 | #ifdef HAVE_TRUNCATE |
| 7720 | PyDoc_STRVAR(posix_truncate__doc__, |
| 7721 | "truncate(path, length)\n\n\ |
| 7722 | Truncate the file given by path to length bytes."); |
| 7723 | |
| 7724 | static PyObject * |
| 7725 | posix_truncate(PyObject *self, PyObject *args) |
| 7726 | { |
| 7727 | PyObject *opath; |
| 7728 | const char *path; |
| 7729 | off_t length; |
| 7730 | int res; |
| 7731 | |
| 7732 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 7733 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 7734 | return NULL; |
| 7735 | path = PyBytes_AsString(opath); |
| 7736 | |
| 7737 | Py_BEGIN_ALLOW_THREADS |
| 7738 | res = truncate(path, length); |
| 7739 | Py_END_ALLOW_THREADS |
| 7740 | Py_DECREF(opath); |
| 7741 | if (res < 0) |
| 7742 | return posix_error(); |
| 7743 | Py_RETURN_NONE; |
| 7744 | } |
| 7745 | #endif |
| 7746 | |
| 7747 | #ifdef HAVE_POSIX_FALLOCATE |
| 7748 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 7749 | "posix_fallocate(fd, offset, len)\n\n\ |
| 7750 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 7751 | starting from offset and continuing for len bytes."); |
| 7752 | |
| 7753 | static PyObject * |
| 7754 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 7755 | { |
| 7756 | off_t len, offset; |
| 7757 | int res, fd; |
| 7758 | |
| 7759 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 7760 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 7761 | return NULL; |
| 7762 | |
| 7763 | Py_BEGIN_ALLOW_THREADS |
| 7764 | res = posix_fallocate(fd, offset, len); |
| 7765 | Py_END_ALLOW_THREADS |
| 7766 | if (res != 0) { |
| 7767 | errno = res; |
| 7768 | return posix_error(); |
| 7769 | } |
| 7770 | Py_RETURN_NONE; |
| 7771 | } |
| 7772 | #endif |
| 7773 | |
| 7774 | #ifdef HAVE_POSIX_FADVISE |
| 7775 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 7776 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 7777 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 7778 | the kernel to make optimizations.\n\ |
| 7779 | The advice applies to the region of the file specified by fd starting at\n\ |
| 7780 | offset and continuing for len bytes.\n\ |
| 7781 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 7782 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 7783 | POSIX_FADV_DONTNEED."); |
| 7784 | |
| 7785 | static PyObject * |
| 7786 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 7787 | { |
| 7788 | off_t len, offset; |
| 7789 | int res, fd, advice; |
| 7790 | |
| 7791 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 7792 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 7793 | return NULL; |
| 7794 | |
| 7795 | Py_BEGIN_ALLOW_THREADS |
| 7796 | res = posix_fadvise(fd, offset, len, advice); |
| 7797 | Py_END_ALLOW_THREADS |
| 7798 | if (res != 0) { |
| 7799 | errno = res; |
| 7800 | return posix_error(); |
| 7801 | } |
| 7802 | Py_RETURN_NONE; |
| 7803 | } |
| 7804 | #endif |
| 7805 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7806 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7807 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7808 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7809 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7810 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7811 | /* Save putenv() parameters as values here, so we can collect them when they |
| 7812 | * get re-set with another call for the same key. */ |
| 7813 | static PyObject *posix_putenv_garbage; |
| 7814 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7815 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7816 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7817 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7818 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7819 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7820 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7821 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7822 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7823 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 7824 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7825 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7826 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7827 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7828 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7829 | if (newstr == NULL) { |
| 7830 | PyErr_NoMemory(); |
| 7831 | goto error; |
| 7832 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7833 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 7834 | PyErr_Format(PyExc_ValueError, |
| 7835 | "the environment variable is longer than %u characters", |
| 7836 | _MAX_ENV); |
| 7837 | goto error; |
| 7838 | } |
| 7839 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7840 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7841 | if (newenv == NULL) |
| 7842 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7843 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7844 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7845 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7846 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7847 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7848 | PyObject *os1, *os2; |
| 7849 | char *s1, *s2; |
| 7850 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7851 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7852 | if (!PyArg_ParseTuple(args, |
| 7853 | "O&O&:putenv", |
| 7854 | PyUnicode_FSConverter, &os1, |
| 7855 | PyUnicode_FSConverter, &os2)) |
| 7856 | return NULL; |
| 7857 | s1 = PyBytes_AsString(os1); |
| 7858 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7859 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7860 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 7861 | if (newstr == NULL) { |
| 7862 | PyErr_NoMemory(); |
| 7863 | goto error; |
| 7864 | } |
| 7865 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7866 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7867 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7868 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7869 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7870 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7871 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7872 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7873 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 7874 | * this will cause previous value to be collected. This has to |
| 7875 | * happen after the real putenv() call because the old value |
| 7876 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7877 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7878 | /* really not much we can do; just leak */ |
| 7879 | PyErr_Clear(); |
| 7880 | } |
| 7881 | else { |
| 7882 | Py_DECREF(newstr); |
| 7883 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7884 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7885 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7886 | Py_DECREF(os1); |
| 7887 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7888 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7889 | Py_RETURN_NONE; |
| 7890 | |
| 7891 | error: |
| 7892 | #ifndef MS_WINDOWS |
| 7893 | Py_DECREF(os1); |
| 7894 | Py_DECREF(os2); |
| 7895 | #endif |
| 7896 | Py_XDECREF(newstr); |
| 7897 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7898 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7899 | #endif /* putenv */ |
| 7900 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7901 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7902 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7903 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7904 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7905 | |
| 7906 | static PyObject * |
| 7907 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7908 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7909 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7910 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 7911 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7912 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7913 | |
| 7914 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7915 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7916 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7917 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7918 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7919 | #ifdef HAVE_BROKEN_UNSETENV |
| 7920 | unsetenv(PyBytes_AS_STRING(name)); |
| 7921 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7922 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 7923 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 7924 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 7925 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 7926 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7927 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7928 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7929 | /* Remove the key from posix_putenv_garbage; |
| 7930 | * this will cause it to be collected. This has to |
| 7931 | * happen after the real unsetenv() call because the |
| 7932 | * old value was still accessible until then. |
| 7933 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7934 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7935 | /* really not much we can do; just leak */ |
| 7936 | PyErr_Clear(); |
| 7937 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7938 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7939 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7940 | } |
| 7941 | #endif /* unsetenv */ |
| 7942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7943 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7944 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7945 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7946 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7947 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7948 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7949 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7950 | int code; |
| 7951 | char *message; |
| 7952 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7953 | return NULL; |
| 7954 | message = strerror(code); |
| 7955 | if (message == NULL) { |
| 7956 | PyErr_SetString(PyExc_ValueError, |
| 7957 | "strerror() argument out of range"); |
| 7958 | return NULL; |
| 7959 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 7960 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7961 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7962 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7963 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7964 | #ifdef HAVE_SYS_WAIT_H |
| 7965 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7966 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7967 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7968 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7969 | 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] | 7970 | |
| 7971 | static PyObject * |
| 7972 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7973 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7974 | WAIT_TYPE status; |
| 7975 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7976 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7977 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7978 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7979 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7980 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7981 | } |
| 7982 | #endif /* WCOREDUMP */ |
| 7983 | |
| 7984 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7985 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7986 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7987 | 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] | 7988 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7989 | |
| 7990 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7991 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7992 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7993 | WAIT_TYPE status; |
| 7994 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7995 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7996 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7997 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7998 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7999 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8000 | } |
| 8001 | #endif /* WIFCONTINUED */ |
| 8002 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8003 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8004 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8005 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8006 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8007 | |
| 8008 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8009 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8010 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8011 | WAIT_TYPE status; |
| 8012 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8013 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8014 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 8015 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8016 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8017 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8018 | } |
| 8019 | #endif /* WIFSTOPPED */ |
| 8020 | |
| 8021 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8022 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8023 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8024 | 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] | 8025 | |
| 8026 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8027 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8028 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8029 | WAIT_TYPE status; |
| 8030 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8032 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 8033 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8034 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8035 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8036 | } |
| 8037 | #endif /* WIFSIGNALED */ |
| 8038 | |
| 8039 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8040 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8041 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8042 | 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] | 8043 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8044 | |
| 8045 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8046 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8047 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8048 | WAIT_TYPE status; |
| 8049 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8051 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 8052 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8053 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8054 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8055 | } |
| 8056 | #endif /* WIFEXITED */ |
| 8057 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8058 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8059 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8060 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8061 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8062 | |
| 8063 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8064 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8065 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8066 | WAIT_TYPE status; |
| 8067 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8068 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8069 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8070 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8071 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8072 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8073 | } |
| 8074 | #endif /* WEXITSTATUS */ |
| 8075 | |
| 8076 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8077 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8078 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8079 | 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] | 8080 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8081 | |
| 8082 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8083 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8084 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8085 | WAIT_TYPE status; |
| 8086 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8087 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8088 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8089 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8090 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8091 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8092 | } |
| 8093 | #endif /* WTERMSIG */ |
| 8094 | |
| 8095 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8096 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8097 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8098 | Return the signal that stopped the process that provided\n\ |
| 8099 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8100 | |
| 8101 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8102 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8103 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8104 | WAIT_TYPE status; |
| 8105 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8106 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8107 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8108 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8109 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8110 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8111 | } |
| 8112 | #endif /* WSTOPSIG */ |
| 8113 | |
| 8114 | #endif /* HAVE_SYS_WAIT_H */ |
| 8115 | |
| 8116 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8117 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8118 | #ifdef _SCO_DS |
| 8119 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8120 | needed definitions in sys/statvfs.h */ |
| 8121 | #define _SVID3 |
| 8122 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8123 | #include <sys/statvfs.h> |
| 8124 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8125 | static PyObject* |
| 8126 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8127 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8128 | if (v == NULL) |
| 8129 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8130 | |
| 8131 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8132 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8133 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8134 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8135 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8136 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8137 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8138 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8139 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8140 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8141 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8142 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8143 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8144 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8145 | PyStructSequence_SET_ITEM(v, 2, |
| 8146 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8147 | PyStructSequence_SET_ITEM(v, 3, |
| 8148 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8149 | PyStructSequence_SET_ITEM(v, 4, |
| 8150 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8151 | PyStructSequence_SET_ITEM(v, 5, |
| 8152 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8153 | PyStructSequence_SET_ITEM(v, 6, |
| 8154 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8155 | PyStructSequence_SET_ITEM(v, 7, |
| 8156 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8157 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8158 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8159 | #endif |
| 8160 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8161 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8162 | } |
| 8163 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8164 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8165 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8166 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8167 | |
| 8168 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8169 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8170 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8171 | int fd, res; |
| 8172 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8173 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8174 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8175 | return NULL; |
| 8176 | Py_BEGIN_ALLOW_THREADS |
| 8177 | res = fstatvfs(fd, &st); |
| 8178 | Py_END_ALLOW_THREADS |
| 8179 | if (res != 0) |
| 8180 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8181 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8182 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8183 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8184 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8185 | |
| 8186 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8187 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8188 | #include <sys/statvfs.h> |
| 8189 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8190 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8191 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8192 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8193 | |
| 8194 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8195 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8196 | { |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8197 | PyObject *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8198 | int res; |
| 8199 | struct statvfs st; |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8200 | if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8201 | return NULL; |
| 8202 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8203 | res = statvfs(PyBytes_AS_STRING(path), &st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8204 | Py_END_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8205 | if (res != 0) { |
| 8206 | posix_error_with_filename(PyBytes_AS_STRING(path)); |
| 8207 | Py_DECREF(path); |
| 8208 | return NULL; |
| 8209 | } |
| 8210 | Py_DECREF(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8211 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8212 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8213 | } |
| 8214 | #endif /* HAVE_STATVFS */ |
| 8215 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8216 | #ifdef MS_WINDOWS |
| 8217 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8218 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8219 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8220 | |
| 8221 | static PyObject * |
| 8222 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8223 | { |
| 8224 | BOOL retval; |
| 8225 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8226 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8227 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8228 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8229 | return NULL; |
| 8230 | |
| 8231 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8232 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8233 | Py_END_ALLOW_THREADS |
| 8234 | if (retval == 0) |
| 8235 | return PyErr_SetFromWindowsErr(0); |
| 8236 | |
| 8237 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8238 | } |
| 8239 | #endif |
| 8240 | |
| 8241 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8242 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8243 | * It maps strings representing configuration variable names to |
| 8244 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8245 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8246 | * rarely-used constants. There are three separate tables that use |
| 8247 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8248 | * |
| 8249 | * This code is always included, even if none of the interfaces that |
| 8250 | * need it are included. The #if hackery needed to avoid it would be |
| 8251 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8252 | */ |
| 8253 | struct constdef { |
| 8254 | char *name; |
| 8255 | long value; |
| 8256 | }; |
| 8257 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8258 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8259 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8260 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8261 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8262 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8263 | *valuep = PyLong_AS_LONG(arg); |
| 8264 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8265 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8266 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8267 | /* look up the value in the table using a binary search */ |
| 8268 | size_t lo = 0; |
| 8269 | size_t mid; |
| 8270 | size_t hi = tablesize; |
| 8271 | int cmp; |
| 8272 | const char *confname; |
| 8273 | if (!PyUnicode_Check(arg)) { |
| 8274 | PyErr_SetString(PyExc_TypeError, |
| 8275 | "configuration names must be strings or integers"); |
| 8276 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8277 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8278 | confname = _PyUnicode_AsString(arg); |
| 8279 | if (confname == NULL) |
| 8280 | return 0; |
| 8281 | while (lo < hi) { |
| 8282 | mid = (lo + hi) / 2; |
| 8283 | cmp = strcmp(confname, table[mid].name); |
| 8284 | if (cmp < 0) |
| 8285 | hi = mid; |
| 8286 | else if (cmp > 0) |
| 8287 | lo = mid + 1; |
| 8288 | else { |
| 8289 | *valuep = table[mid].value; |
| 8290 | return 1; |
| 8291 | } |
| 8292 | } |
| 8293 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8294 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8295 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8296 | } |
| 8297 | |
| 8298 | |
| 8299 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8300 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8301 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8302 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8303 | #endif |
| 8304 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8305 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8306 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8307 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8308 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8309 | #endif |
| 8310 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8311 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8312 | #endif |
| 8313 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8314 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8315 | #endif |
| 8316 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8317 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8318 | #endif |
| 8319 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8320 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8321 | #endif |
| 8322 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8323 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8324 | #endif |
| 8325 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8326 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8327 | #endif |
| 8328 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8329 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8330 | #endif |
| 8331 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8332 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8333 | #endif |
| 8334 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8335 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8336 | #endif |
| 8337 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8338 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8339 | #endif |
| 8340 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8341 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8342 | #endif |
| 8343 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8344 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8345 | #endif |
| 8346 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8347 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8348 | #endif |
| 8349 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8350 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8351 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8352 | #ifdef _PC_ACL_ENABLED |
| 8353 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8354 | #endif |
| 8355 | #ifdef _PC_MIN_HOLE_SIZE |
| 8356 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8357 | #endif |
| 8358 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8359 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8360 | #endif |
| 8361 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8362 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8363 | #endif |
| 8364 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8365 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8366 | #endif |
| 8367 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8368 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8369 | #endif |
| 8370 | #ifdef _PC_REC_XFER_ALIGN |
| 8371 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8372 | #endif |
| 8373 | #ifdef _PC_SYMLINK_MAX |
| 8374 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8375 | #endif |
| 8376 | #ifdef _PC_XATTR_ENABLED |
| 8377 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8378 | #endif |
| 8379 | #ifdef _PC_XATTR_EXISTS |
| 8380 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8381 | #endif |
| 8382 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8383 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8384 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8385 | }; |
| 8386 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8387 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8388 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8389 | { |
| 8390 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8391 | sizeof(posix_constants_pathconf) |
| 8392 | / sizeof(struct constdef)); |
| 8393 | } |
| 8394 | #endif |
| 8395 | |
| 8396 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8397 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8398 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8399 | 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] | 8400 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8401 | |
| 8402 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8403 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8404 | { |
| 8405 | PyObject *result = NULL; |
| 8406 | int name, fd; |
| 8407 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8408 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8409 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8410 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8411 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8412 | errno = 0; |
| 8413 | limit = fpathconf(fd, name); |
| 8414 | if (limit == -1 && errno != 0) |
| 8415 | posix_error(); |
| 8416 | else |
| 8417 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8418 | } |
| 8419 | return result; |
| 8420 | } |
| 8421 | #endif |
| 8422 | |
| 8423 | |
| 8424 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8425 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8426 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8427 | 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] | 8428 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8429 | |
| 8430 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8431 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8432 | { |
| 8433 | PyObject *result = NULL; |
| 8434 | int name; |
| 8435 | char *path; |
| 8436 | |
| 8437 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 8438 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8439 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8440 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8441 | errno = 0; |
| 8442 | limit = pathconf(path, name); |
| 8443 | if (limit == -1 && errno != 0) { |
| 8444 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8445 | /* could be a path or name problem */ |
| 8446 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8447 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8448 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8449 | } |
| 8450 | else |
| 8451 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8452 | } |
| 8453 | return result; |
| 8454 | } |
| 8455 | #endif |
| 8456 | |
| 8457 | #ifdef HAVE_CONFSTR |
| 8458 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8459 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8460 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8461 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8462 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8463 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8464 | #endif |
| 8465 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8466 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8467 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8468 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8469 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8470 | #endif |
| 8471 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8472 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8473 | #endif |
| 8474 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8475 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8476 | #endif |
| 8477 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8478 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8479 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8480 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8481 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8482 | #endif |
| 8483 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8484 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8485 | #endif |
| 8486 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8487 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8488 | #endif |
| 8489 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8490 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8491 | #endif |
| 8492 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8493 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8494 | #endif |
| 8495 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8496 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8497 | #endif |
| 8498 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8499 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8500 | #endif |
| 8501 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8502 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8503 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8504 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8505 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8506 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8507 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8508 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8509 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8510 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8511 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8512 | #endif |
| 8513 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8514 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8515 | #endif |
| 8516 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8517 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8518 | #endif |
| 8519 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8520 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8521 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8522 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8523 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8524 | #endif |
| 8525 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8526 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8527 | #endif |
| 8528 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8529 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8530 | #endif |
| 8531 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8532 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8533 | #endif |
| 8534 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8535 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8536 | #endif |
| 8537 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8538 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8539 | #endif |
| 8540 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8541 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8542 | #endif |
| 8543 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8544 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8545 | #endif |
| 8546 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8547 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8548 | #endif |
| 8549 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8550 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8551 | #endif |
| 8552 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8553 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8554 | #endif |
| 8555 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8556 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8557 | #endif |
| 8558 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8559 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8560 | #endif |
| 8561 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8562 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8563 | #endif |
| 8564 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8565 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8566 | #endif |
| 8567 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8568 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8569 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8570 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8571 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8572 | #endif |
| 8573 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8574 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8575 | #endif |
| 8576 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8577 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8578 | #endif |
| 8579 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8580 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8581 | #endif |
| 8582 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8583 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8584 | #endif |
| 8585 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8586 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8587 | #endif |
| 8588 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8589 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8590 | #endif |
| 8591 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8592 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8593 | #endif |
| 8594 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8595 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8596 | #endif |
| 8597 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8598 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8599 | #endif |
| 8600 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8601 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8602 | #endif |
| 8603 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8605 | #endif |
| 8606 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8608 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8609 | }; |
| 8610 | |
| 8611 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8612 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8613 | { |
| 8614 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8615 | sizeof(posix_constants_confstr) |
| 8616 | / sizeof(struct constdef)); |
| 8617 | } |
| 8618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8619 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8620 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8621 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8622 | |
| 8623 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8624 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8625 | { |
| 8626 | PyObject *result = NULL; |
| 8627 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8628 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8629 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8630 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8631 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8632 | return NULL; |
| 8633 | |
| 8634 | errno = 0; |
| 8635 | len = confstr(name, buffer, sizeof(buffer)); |
| 8636 | if (len == 0) { |
| 8637 | if (errno) { |
| 8638 | posix_error(); |
| 8639 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8640 | } |
| 8641 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8642 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8643 | } |
| 8644 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8645 | |
| 8646 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8647 | char *buf = PyMem_Malloc(len); |
| 8648 | if (buf == NULL) |
| 8649 | return PyErr_NoMemory(); |
| 8650 | confstr(name, buf, len); |
| 8651 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8652 | PyMem_Free(buf); |
| 8653 | } |
| 8654 | else |
| 8655 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8656 | return result; |
| 8657 | } |
| 8658 | #endif |
| 8659 | |
| 8660 | |
| 8661 | #ifdef HAVE_SYSCONF |
| 8662 | static struct constdef posix_constants_sysconf[] = { |
| 8663 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8664 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8665 | #endif |
| 8666 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8667 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8668 | #endif |
| 8669 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8670 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8671 | #endif |
| 8672 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8673 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8674 | #endif |
| 8675 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8676 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8677 | #endif |
| 8678 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8679 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8680 | #endif |
| 8681 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8682 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8683 | #endif |
| 8684 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8685 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8686 | #endif |
| 8687 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8688 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8689 | #endif |
| 8690 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8691 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8692 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8693 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8694 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8695 | #endif |
| 8696 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8697 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8698 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8699 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8700 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8701 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8702 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8703 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8704 | #endif |
| 8705 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8706 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8707 | #endif |
| 8708 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8709 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8710 | #endif |
| 8711 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8713 | #endif |
| 8714 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8716 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8717 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8718 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8719 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8720 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8721 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8722 | #endif |
| 8723 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8724 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8725 | #endif |
| 8726 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8727 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8728 | #endif |
| 8729 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8730 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8731 | #endif |
| 8732 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8733 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8734 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8735 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8736 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8737 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8738 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8739 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8740 | #endif |
| 8741 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8742 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8743 | #endif |
| 8744 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8745 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8746 | #endif |
| 8747 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8748 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8749 | #endif |
| 8750 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8751 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8752 | #endif |
| 8753 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8754 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8755 | #endif |
| 8756 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8757 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8758 | #endif |
| 8759 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8760 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8761 | #endif |
| 8762 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8763 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8764 | #endif |
| 8765 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8766 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8767 | #endif |
| 8768 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8769 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8770 | #endif |
| 8771 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8772 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8773 | #endif |
| 8774 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8775 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8776 | #endif |
| 8777 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8778 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8779 | #endif |
| 8780 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8781 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8782 | #endif |
| 8783 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8784 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8785 | #endif |
| 8786 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8787 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8788 | #endif |
| 8789 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8790 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8791 | #endif |
| 8792 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8793 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8794 | #endif |
| 8795 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8796 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8797 | #endif |
| 8798 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8799 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8800 | #endif |
| 8801 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8802 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8803 | #endif |
| 8804 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8805 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8806 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8807 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8808 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8809 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8810 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8811 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8812 | #endif |
| 8813 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8814 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8815 | #endif |
| 8816 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8817 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8818 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8819 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8820 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8821 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8822 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8823 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8824 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8825 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8826 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8827 | #endif |
| 8828 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8829 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8830 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8831 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8832 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8833 | #endif |
| 8834 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8835 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8836 | #endif |
| 8837 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8838 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8839 | #endif |
| 8840 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8841 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8842 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8843 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8844 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8845 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8846 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8847 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8848 | #endif |
| 8849 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8850 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8851 | #endif |
| 8852 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8853 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8854 | #endif |
| 8855 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8856 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8857 | #endif |
| 8858 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8859 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8860 | #endif |
| 8861 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8862 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8863 | #endif |
| 8864 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8865 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8866 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8867 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8868 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8869 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8870 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8871 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8872 | #endif |
| 8873 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8874 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8875 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8876 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8877 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8878 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8879 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8880 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8881 | #endif |
| 8882 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8883 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8884 | #endif |
| 8885 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8886 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8887 | #endif |
| 8888 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8889 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8890 | #endif |
| 8891 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8892 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8893 | #endif |
| 8894 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8895 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8896 | #endif |
| 8897 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8898 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8899 | #endif |
| 8900 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8901 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8902 | #endif |
| 8903 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8904 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8905 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8906 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8907 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8908 | #endif |
| 8909 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8910 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8911 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8912 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8913 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8914 | #endif |
| 8915 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8916 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8917 | #endif |
| 8918 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8919 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8920 | #endif |
| 8921 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8922 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8923 | #endif |
| 8924 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8925 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8926 | #endif |
| 8927 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8928 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8929 | #endif |
| 8930 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8931 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8932 | #endif |
| 8933 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8934 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8935 | #endif |
| 8936 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8937 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8938 | #endif |
| 8939 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8940 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8941 | #endif |
| 8942 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8943 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8944 | #endif |
| 8945 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8946 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8947 | #endif |
| 8948 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8949 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8950 | #endif |
| 8951 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8952 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8953 | #endif |
| 8954 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8955 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8956 | #endif |
| 8957 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8958 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8959 | #endif |
| 8960 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8961 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8962 | #endif |
| 8963 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8964 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8965 | #endif |
| 8966 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8967 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8968 | #endif |
| 8969 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8970 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8971 | #endif |
| 8972 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8973 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8974 | #endif |
| 8975 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8976 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8977 | #endif |
| 8978 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8979 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8980 | #endif |
| 8981 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8982 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8983 | #endif |
| 8984 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8985 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8986 | #endif |
| 8987 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8988 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8989 | #endif |
| 8990 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8991 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8992 | #endif |
| 8993 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8994 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8995 | #endif |
| 8996 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8997 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8998 | #endif |
| 8999 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9000 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9001 | #endif |
| 9002 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9003 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9004 | #endif |
| 9005 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9006 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9007 | #endif |
| 9008 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9009 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9010 | #endif |
| 9011 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9012 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9013 | #endif |
| 9014 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9015 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9016 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9017 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9018 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9019 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9020 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9021 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9022 | #endif |
| 9023 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9024 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9025 | #endif |
| 9026 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9027 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9028 | #endif |
| 9029 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9030 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9031 | #endif |
| 9032 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9033 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9034 | #endif |
| 9035 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9036 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9037 | #endif |
| 9038 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9039 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9040 | #endif |
| 9041 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9042 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9043 | #endif |
| 9044 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9045 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9046 | #endif |
| 9047 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9048 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9049 | #endif |
| 9050 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9051 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9052 | #endif |
| 9053 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9054 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9055 | #endif |
| 9056 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9057 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9058 | #endif |
| 9059 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9060 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9061 | #endif |
| 9062 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9063 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9064 | #endif |
| 9065 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9066 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9067 | #endif |
| 9068 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9069 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9070 | #endif |
| 9071 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9072 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9073 | #endif |
| 9074 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9075 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9076 | #endif |
| 9077 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9078 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9079 | #endif |
| 9080 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9081 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9082 | #endif |
| 9083 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9084 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9085 | #endif |
| 9086 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9087 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9088 | #endif |
| 9089 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9090 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9091 | #endif |
| 9092 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9093 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9094 | #endif |
| 9095 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9096 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9097 | #endif |
| 9098 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9099 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9100 | #endif |
| 9101 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9102 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9103 | #endif |
| 9104 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9105 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9106 | #endif |
| 9107 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9108 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9109 | #endif |
| 9110 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9111 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9112 | #endif |
| 9113 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9114 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9115 | #endif |
| 9116 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9117 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9118 | #endif |
| 9119 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9120 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9121 | #endif |
| 9122 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9123 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9124 | #endif |
| 9125 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9126 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9127 | #endif |
| 9128 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9129 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9130 | #endif |
| 9131 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9132 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9133 | #endif |
| 9134 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9135 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9136 | #endif |
| 9137 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9138 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9139 | #endif |
| 9140 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9141 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9142 | #endif |
| 9143 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9144 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9145 | #endif |
| 9146 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9147 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9148 | #endif |
| 9149 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9150 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9151 | #endif |
| 9152 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9153 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9154 | #endif |
| 9155 | }; |
| 9156 | |
| 9157 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9158 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9159 | { |
| 9160 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9161 | sizeof(posix_constants_sysconf) |
| 9162 | / sizeof(struct constdef)); |
| 9163 | } |
| 9164 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9165 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9166 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9167 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9168 | |
| 9169 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9170 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9171 | { |
| 9172 | PyObject *result = NULL; |
| 9173 | int name; |
| 9174 | |
| 9175 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9176 | int value; |
| 9177 | |
| 9178 | errno = 0; |
| 9179 | value = sysconf(name); |
| 9180 | if (value == -1 && errno != 0) |
| 9181 | posix_error(); |
| 9182 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9183 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9184 | } |
| 9185 | return result; |
| 9186 | } |
| 9187 | #endif |
| 9188 | |
| 9189 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9190 | /* This code is used to ensure that the tables of configuration value names |
| 9191 | * are in sorted order as required by conv_confname(), and also to build the |
| 9192 | * the exported dictionaries that are used to publish information about the |
| 9193 | * names available on the host platform. |
| 9194 | * |
| 9195 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9196 | * when used, even for platforms we're not able to test on. It also makes |
| 9197 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9198 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9199 | |
| 9200 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9201 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9202 | { |
| 9203 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9204 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9205 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9206 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9207 | |
| 9208 | return strcmp(c1->name, c2->name); |
| 9209 | } |
| 9210 | |
| 9211 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9212 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9213 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9214 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9215 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9216 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9217 | |
| 9218 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9219 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9220 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9221 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9222 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9223 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9224 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9225 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9226 | Py_XDECREF(o); |
| 9227 | Py_DECREF(d); |
| 9228 | return -1; |
| 9229 | } |
| 9230 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9231 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9232 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9233 | } |
| 9234 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9235 | /* Return -1 on failure, 0 on success. */ |
| 9236 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9237 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9238 | { |
| 9239 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9240 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9241 | sizeof(posix_constants_pathconf) |
| 9242 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9243 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9244 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9245 | #endif |
| 9246 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9247 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9248 | sizeof(posix_constants_confstr) |
| 9249 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9250 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9251 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9252 | #endif |
| 9253 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9254 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9255 | sizeof(posix_constants_sysconf) |
| 9256 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9257 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9258 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9259 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9260 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9261 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9262 | |
| 9263 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9264 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9265 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9266 | 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] | 9267 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9268 | |
| 9269 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9270 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9271 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9272 | abort(); |
| 9273 | /*NOTREACHED*/ |
| 9274 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9275 | return NULL; |
| 9276 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9277 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9278 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9279 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9280 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9281 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9282 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9283 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9284 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9285 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9286 | application (if any) its extension is associated.\n\ |
| 9287 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9288 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9289 | \n\ |
| 9290 | startfile returns as soon as the associated application is launched.\n\ |
| 9291 | There is no option to wait for the application to close, and no way\n\ |
| 9292 | to retrieve the application's exit status.\n\ |
| 9293 | \n\ |
| 9294 | The filepath is relative to the current directory. If you want to use\n\ |
| 9295 | 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] | 9296 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9297 | |
| 9298 | static PyObject * |
| 9299 | win32_startfile(PyObject *self, PyObject *args) |
| 9300 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9301 | PyObject *ofilepath; |
| 9302 | char *filepath; |
| 9303 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9304 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9305 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9306 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9307 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9308 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9309 | &unipath, &operation)) { |
| 9310 | PyErr_Clear(); |
| 9311 | goto normal; |
| 9312 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9313 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9314 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9315 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9316 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9317 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9318 | PyErr_Clear(); |
| 9319 | operation = NULL; |
| 9320 | goto normal; |
| 9321 | } |
| 9322 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9323 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9324 | wpath = PyUnicode_AsUnicode(unipath); |
| 9325 | if (wpath == NULL) |
| 9326 | goto normal; |
| 9327 | if (uoperation) { |
| 9328 | woperation = PyUnicode_AsUnicode(uoperation); |
| 9329 | if (woperation == NULL) |
| 9330 | goto normal; |
| 9331 | } |
| 9332 | else |
| 9333 | woperation = NULL; |
| 9334 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9335 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9336 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 9337 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9338 | Py_END_ALLOW_THREADS |
| 9339 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9340 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9341 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9342 | win32_error_object("startfile", unipath); |
| 9343 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9344 | } |
| 9345 | Py_INCREF(Py_None); |
| 9346 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9347 | |
| 9348 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9349 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9350 | PyUnicode_FSConverter, &ofilepath, |
| 9351 | &operation)) |
| 9352 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 9353 | if (win32_warn_bytes_api()) { |
| 9354 | Py_DECREF(ofilepath); |
| 9355 | return NULL; |
| 9356 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9357 | filepath = PyBytes_AsString(ofilepath); |
| 9358 | Py_BEGIN_ALLOW_THREADS |
| 9359 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9360 | NULL, NULL, SW_SHOWNORMAL); |
| 9361 | Py_END_ALLOW_THREADS |
| 9362 | if (rc <= (HINSTANCE)32) { |
| 9363 | PyObject *errval = win32_error("startfile", filepath); |
| 9364 | Py_DECREF(ofilepath); |
| 9365 | return errval; |
| 9366 | } |
| 9367 | Py_DECREF(ofilepath); |
| 9368 | Py_INCREF(Py_None); |
| 9369 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9370 | } |
| 9371 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9372 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9373 | #ifdef HAVE_GETLOADAVG |
| 9374 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9375 | "getloadavg() -> (float, float, float)\n\n\ |
| 9376 | Return the number of processes in the system run queue averaged over\n\ |
| 9377 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9378 | was unobtainable"); |
| 9379 | |
| 9380 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9381 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9382 | { |
| 9383 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9384 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9385 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9386 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9387 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9388 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9389 | } |
| 9390 | #endif |
| 9391 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9392 | PyDoc_STRVAR(device_encoding__doc__, |
| 9393 | "device_encoding(fd) -> str\n\n\ |
| 9394 | Return a string describing the encoding of the device\n\ |
| 9395 | if the output is a terminal; else return None."); |
| 9396 | |
| 9397 | static PyObject * |
| 9398 | device_encoding(PyObject *self, PyObject *args) |
| 9399 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9400 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9402 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9403 | return NULL; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9404 | |
| 9405 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9406 | } |
| 9407 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9408 | #ifdef __VMS |
| 9409 | /* Use openssl random routine */ |
| 9410 | #include <openssl/rand.h> |
| 9411 | PyDoc_STRVAR(vms_urandom__doc__, |
| 9412 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9413 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9414 | |
| 9415 | static PyObject* |
| 9416 | vms_urandom(PyObject *self, PyObject *args) |
| 9417 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9418 | int howMany; |
| 9419 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9420 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9421 | /* Read arguments */ |
| 9422 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9423 | return NULL; |
| 9424 | if (howMany < 0) |
| 9425 | return PyErr_Format(PyExc_ValueError, |
| 9426 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9427 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9428 | /* Allocate bytes */ |
| 9429 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9430 | if (result != NULL) { |
| 9431 | /* Get random data */ |
| 9432 | if (RAND_pseudo_bytes((unsigned char*) |
| 9433 | PyBytes_AS_STRING(result), |
| 9434 | howMany) < 0) { |
| 9435 | Py_DECREF(result); |
| 9436 | return PyErr_Format(PyExc_ValueError, |
| 9437 | "RAND_pseudo_bytes"); |
| 9438 | } |
| 9439 | } |
| 9440 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9441 | } |
| 9442 | #endif |
| 9443 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9444 | #ifdef HAVE_SETRESUID |
| 9445 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9446 | "setresuid(ruid, euid, suid)\n\n\ |
| 9447 | Set the current process's real, effective, and saved user ids."); |
| 9448 | |
| 9449 | static PyObject* |
| 9450 | posix_setresuid (PyObject *self, PyObject *args) |
| 9451 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9452 | /* We assume uid_t is no larger than a long. */ |
| 9453 | long ruid, euid, suid; |
| 9454 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9455 | return NULL; |
| 9456 | if (setresuid(ruid, euid, suid) < 0) |
| 9457 | return posix_error(); |
| 9458 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9459 | } |
| 9460 | #endif |
| 9461 | |
| 9462 | #ifdef HAVE_SETRESGID |
| 9463 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9464 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9465 | Set the current process's real, effective, and saved group ids."); |
| 9466 | |
| 9467 | static PyObject* |
| 9468 | posix_setresgid (PyObject *self, PyObject *args) |
| 9469 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9470 | /* We assume uid_t is no larger than a long. */ |
| 9471 | long rgid, egid, sgid; |
| 9472 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9473 | return NULL; |
| 9474 | if (setresgid(rgid, egid, sgid) < 0) |
| 9475 | return posix_error(); |
| 9476 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9477 | } |
| 9478 | #endif |
| 9479 | |
| 9480 | #ifdef HAVE_GETRESUID |
| 9481 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9482 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9483 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9484 | |
| 9485 | static PyObject* |
| 9486 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9487 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9488 | uid_t ruid, euid, suid; |
| 9489 | long l_ruid, l_euid, l_suid; |
| 9490 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9491 | return posix_error(); |
| 9492 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9493 | l_ruid = ruid; |
| 9494 | l_euid = euid; |
| 9495 | l_suid = suid; |
| 9496 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9497 | } |
| 9498 | #endif |
| 9499 | |
| 9500 | #ifdef HAVE_GETRESGID |
| 9501 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9502 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9503 | 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] | 9504 | |
| 9505 | static PyObject* |
| 9506 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9507 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9508 | uid_t rgid, egid, sgid; |
| 9509 | long l_rgid, l_egid, l_sgid; |
| 9510 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9511 | return posix_error(); |
| 9512 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9513 | l_rgid = rgid; |
| 9514 | l_egid = egid; |
| 9515 | l_sgid = sgid; |
| 9516 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9517 | } |
| 9518 | #endif |
| 9519 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9520 | /* Posix *at family of functions: |
| 9521 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 9522 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 9523 | unlinkat, utimensat, mkfifoat */ |
| 9524 | |
| 9525 | #ifdef HAVE_FACCESSAT |
| 9526 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 9527 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 9528 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9529 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 9530 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 9531 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9532 | is interpreted relative to the current working directory."); |
| 9533 | |
| 9534 | static PyObject * |
| 9535 | posix_faccessat(PyObject *self, PyObject *args) |
| 9536 | { |
| 9537 | PyObject *opath; |
| 9538 | char *path; |
| 9539 | int mode; |
| 9540 | int res; |
| 9541 | int dirfd, flags = 0; |
| 9542 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 9543 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9544 | return NULL; |
| 9545 | path = PyBytes_AsString(opath); |
| 9546 | Py_BEGIN_ALLOW_THREADS |
| 9547 | res = faccessat(dirfd, path, mode, flags); |
| 9548 | Py_END_ALLOW_THREADS |
| 9549 | Py_DECREF(opath); |
| 9550 | return PyBool_FromLong(res == 0); |
| 9551 | } |
| 9552 | #endif |
| 9553 | |
| 9554 | #ifdef HAVE_FCHMODAT |
| 9555 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 9556 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 9557 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9558 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9559 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9560 | is interpreted relative to the current working directory."); |
| 9561 | |
| 9562 | static PyObject * |
| 9563 | posix_fchmodat(PyObject *self, PyObject *args) |
| 9564 | { |
| 9565 | int dirfd, mode, res; |
| 9566 | int flags = 0; |
| 9567 | PyObject *opath; |
| 9568 | char *path; |
| 9569 | |
| 9570 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 9571 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9572 | return NULL; |
| 9573 | |
| 9574 | path = PyBytes_AsString(opath); |
| 9575 | |
| 9576 | Py_BEGIN_ALLOW_THREADS |
| 9577 | res = fchmodat(dirfd, path, mode, flags); |
| 9578 | Py_END_ALLOW_THREADS |
| 9579 | Py_DECREF(opath); |
| 9580 | if (res < 0) |
| 9581 | return posix_error(); |
| 9582 | Py_RETURN_NONE; |
| 9583 | } |
| 9584 | #endif /* HAVE_FCHMODAT */ |
| 9585 | |
| 9586 | #ifdef HAVE_FCHOWNAT |
| 9587 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 9588 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 9589 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9590 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9591 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9592 | is interpreted relative to the current working directory."); |
| 9593 | |
| 9594 | static PyObject * |
| 9595 | posix_fchownat(PyObject *self, PyObject *args) |
| 9596 | { |
| 9597 | PyObject *opath; |
| 9598 | int dirfd, res; |
| 9599 | long uid, gid; |
| 9600 | int flags = 0; |
| 9601 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9602 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9603 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 9604 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 9605 | return NULL; |
| 9606 | |
| 9607 | path = PyBytes_AsString(opath); |
| 9608 | |
| 9609 | Py_BEGIN_ALLOW_THREADS |
| 9610 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 9611 | Py_END_ALLOW_THREADS |
| 9612 | Py_DECREF(opath); |
| 9613 | if (res < 0) |
| 9614 | return posix_error(); |
| 9615 | Py_RETURN_NONE; |
| 9616 | } |
| 9617 | #endif /* HAVE_FCHOWNAT */ |
| 9618 | |
| 9619 | #ifdef HAVE_FSTATAT |
| 9620 | PyDoc_STRVAR(posix_fstatat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9621 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9622 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9623 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9624 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9625 | is interpreted relative to the current working directory."); |
| 9626 | |
| 9627 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9628 | posix_fstatat(PyObject *self, PyObject *args) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9629 | { |
| 9630 | PyObject *opath; |
| 9631 | char *path; |
| 9632 | STRUCT_STAT st; |
| 9633 | int dirfd, res, flags = 0; |
| 9634 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9635 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 9636 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9637 | return NULL; |
| 9638 | path = PyBytes_AsString(opath); |
| 9639 | |
| 9640 | Py_BEGIN_ALLOW_THREADS |
| 9641 | res = fstatat(dirfd, path, &st, flags); |
| 9642 | Py_END_ALLOW_THREADS |
| 9643 | Py_DECREF(opath); |
| 9644 | if (res != 0) |
| 9645 | return posix_error(); |
| 9646 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9647 | return _pystat_fromstructstat(&st); |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9648 | } |
| 9649 | #endif |
| 9650 | |
| 9651 | #ifdef HAVE_FUTIMESAT |
| 9652 | PyDoc_STRVAR(posix_futimesat__doc__, |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9653 | "futimesat(dirfd, path[, (atime, mtime)])\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9654 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9655 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9656 | is interpreted relative to the current working directory."); |
| 9657 | |
| 9658 | static PyObject * |
| 9659 | posix_futimesat(PyObject *self, PyObject *args) |
| 9660 | { |
| 9661 | PyObject *opath; |
| 9662 | char *path; |
| 9663 | int res, dirfd; |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9664 | PyObject* arg = Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9665 | time_t atime, mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9666 | long ansec, mnsec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9667 | |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9668 | if (!PyArg_ParseTuple(args, "iO&|O:futimesat", |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9669 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 9670 | return NULL; |
| 9671 | path = PyBytes_AsString(opath); |
| 9672 | if (arg == Py_None) { |
| 9673 | /* optional time values not given */ |
| 9674 | Py_BEGIN_ALLOW_THREADS |
| 9675 | res = futimesat(dirfd, path, NULL); |
| 9676 | Py_END_ALLOW_THREADS |
| 9677 | } |
| 9678 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 9679 | PyErr_SetString(PyExc_TypeError, |
| 9680 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 9681 | Py_DECREF(opath); |
| 9682 | return NULL; |
| 9683 | } |
| 9684 | else { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 9685 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 0), |
| 9686 | &atime, &ansec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9687 | Py_DECREF(opath); |
| 9688 | return NULL; |
| 9689 | } |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 9690 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 1), |
| 9691 | &mtime, &mnsec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9692 | Py_DECREF(opath); |
| 9693 | return NULL; |
| 9694 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9695 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9696 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9697 | { |
| 9698 | #ifdef HAVE_UTIMENSAT |
| 9699 | struct timespec buf[2]; |
| 9700 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9701 | buf[0].tv_nsec = ansec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9702 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9703 | buf[1].tv_nsec = mnsec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9704 | res = utimensat(dirfd, path, buf, 0); |
| 9705 | #else |
| 9706 | struct timeval buf[2]; |
| 9707 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9708 | buf[0].tv_usec = ansec / 1000; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9709 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9710 | buf[1].tv_usec = mnsec / 1000; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9711 | res = futimesat(dirfd, path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9712 | #endif |
| 9713 | } |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9714 | Py_END_ALLOW_THREADS |
| 9715 | } |
| 9716 | Py_DECREF(opath); |
| 9717 | if (res < 0) { |
| 9718 | return posix_error(); |
| 9719 | } |
| 9720 | Py_RETURN_NONE; |
| 9721 | } |
| 9722 | #endif |
| 9723 | |
| 9724 | #ifdef HAVE_LINKAT |
| 9725 | PyDoc_STRVAR(posix_linkat__doc__, |
| 9726 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 9727 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 9728 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 9729 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 9730 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 9731 | srcpath is interpreted relative to the current working directory. This\n\ |
| 9732 | also applies for dstpath."); |
| 9733 | |
| 9734 | static PyObject * |
| 9735 | posix_linkat(PyObject *self, PyObject *args) |
| 9736 | { |
| 9737 | PyObject *osrc, *odst; |
| 9738 | char *src, *dst; |
| 9739 | int res, srcfd, dstfd; |
| 9740 | int flags = 0; |
| 9741 | |
| 9742 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 9743 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 9744 | return NULL; |
| 9745 | src = PyBytes_AsString(osrc); |
| 9746 | dst = PyBytes_AsString(odst); |
| 9747 | Py_BEGIN_ALLOW_THREADS |
| 9748 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 9749 | Py_END_ALLOW_THREADS |
| 9750 | Py_DECREF(osrc); |
| 9751 | Py_DECREF(odst); |
| 9752 | if (res < 0) |
| 9753 | return posix_error(); |
| 9754 | Py_RETURN_NONE; |
| 9755 | } |
| 9756 | #endif /* HAVE_LINKAT */ |
| 9757 | |
| 9758 | #ifdef HAVE_MKDIRAT |
| 9759 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 9760 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 9761 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9762 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9763 | is interpreted relative to the current working directory."); |
| 9764 | |
| 9765 | static PyObject * |
| 9766 | posix_mkdirat(PyObject *self, PyObject *args) |
| 9767 | { |
| 9768 | int res, dirfd; |
| 9769 | PyObject *opath; |
| 9770 | char *path; |
| 9771 | int mode = 0777; |
| 9772 | |
| 9773 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 9774 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9775 | return NULL; |
| 9776 | path = PyBytes_AsString(opath); |
| 9777 | Py_BEGIN_ALLOW_THREADS |
| 9778 | res = mkdirat(dirfd, path, mode); |
| 9779 | Py_END_ALLOW_THREADS |
| 9780 | Py_DECREF(opath); |
| 9781 | if (res < 0) |
| 9782 | return posix_error(); |
| 9783 | Py_RETURN_NONE; |
| 9784 | } |
| 9785 | #endif |
| 9786 | |
| 9787 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 9788 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 9789 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 9790 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9791 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9792 | is interpreted relative to the current working directory."); |
| 9793 | |
| 9794 | static PyObject * |
| 9795 | posix_mknodat(PyObject *self, PyObject *args) |
| 9796 | { |
| 9797 | PyObject *opath; |
| 9798 | char *filename; |
| 9799 | int mode = 0600; |
| 9800 | int device = 0; |
| 9801 | int res, dirfd; |
| 9802 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 9803 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 9804 | return NULL; |
| 9805 | filename = PyBytes_AS_STRING(opath); |
| 9806 | Py_BEGIN_ALLOW_THREADS |
| 9807 | res = mknodat(dirfd, filename, mode, device); |
| 9808 | Py_END_ALLOW_THREADS |
| 9809 | Py_DECREF(opath); |
| 9810 | if (res < 0) |
| 9811 | return posix_error(); |
| 9812 | Py_RETURN_NONE; |
| 9813 | } |
| 9814 | #endif |
| 9815 | |
| 9816 | #ifdef HAVE_OPENAT |
| 9817 | PyDoc_STRVAR(posix_openat__doc__, |
| 9818 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 9819 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9820 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9821 | is interpreted relative to the current working directory."); |
| 9822 | |
| 9823 | static PyObject * |
| 9824 | posix_openat(PyObject *self, PyObject *args) |
| 9825 | { |
| 9826 | PyObject *ofile; |
| 9827 | char *file; |
| 9828 | int flag, dirfd, fd; |
| 9829 | int mode = 0777; |
| 9830 | |
| 9831 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 9832 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 9833 | &flag, &mode)) |
| 9834 | return NULL; |
| 9835 | file = PyBytes_AsString(ofile); |
| 9836 | Py_BEGIN_ALLOW_THREADS |
| 9837 | fd = openat(dirfd, file, flag, mode); |
| 9838 | Py_END_ALLOW_THREADS |
| 9839 | Py_DECREF(ofile); |
| 9840 | if (fd < 0) |
| 9841 | return posix_error(); |
| 9842 | return PyLong_FromLong((long)fd); |
| 9843 | } |
| 9844 | #endif |
| 9845 | |
| 9846 | #ifdef HAVE_READLINKAT |
| 9847 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 9848 | "readlinkat(dirfd, path) -> path\n\n\ |
| 9849 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9850 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9851 | is interpreted relative to the current working directory."); |
| 9852 | |
| 9853 | static PyObject * |
| 9854 | posix_readlinkat(PyObject *self, PyObject *args) |
| 9855 | { |
| 9856 | PyObject *v, *opath; |
| 9857 | char buf[MAXPATHLEN]; |
| 9858 | char *path; |
| 9859 | int n, dirfd; |
| 9860 | int arg_is_unicode = 0; |
| 9861 | |
| 9862 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 9863 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 9864 | return NULL; |
| 9865 | path = PyBytes_AsString(opath); |
| 9866 | v = PySequence_GetItem(args, 1); |
| 9867 | if (v == NULL) { |
| 9868 | Py_DECREF(opath); |
| 9869 | return NULL; |
| 9870 | } |
| 9871 | |
| 9872 | if (PyUnicode_Check(v)) { |
| 9873 | arg_is_unicode = 1; |
| 9874 | } |
| 9875 | Py_DECREF(v); |
| 9876 | |
| 9877 | Py_BEGIN_ALLOW_THREADS |
| 9878 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 9879 | Py_END_ALLOW_THREADS |
| 9880 | Py_DECREF(opath); |
| 9881 | if (n < 0) |
| 9882 | return posix_error(); |
| 9883 | |
| 9884 | if (arg_is_unicode) |
| 9885 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 9886 | else |
| 9887 | return PyBytes_FromStringAndSize(buf, n); |
| 9888 | } |
| 9889 | #endif /* HAVE_READLINKAT */ |
| 9890 | |
| 9891 | #ifdef HAVE_RENAMEAT |
| 9892 | PyDoc_STRVAR(posix_renameat__doc__, |
| 9893 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 9894 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 9895 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 9896 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 9897 | oldpath is interpreted relative to the current working directory. This\n\ |
| 9898 | also applies for newpath."); |
| 9899 | |
| 9900 | static PyObject * |
| 9901 | posix_renameat(PyObject *self, PyObject *args) |
| 9902 | { |
| 9903 | int res; |
| 9904 | PyObject *opathold, *opathnew; |
| 9905 | char *opath, *npath; |
| 9906 | int oldfd, newfd; |
| 9907 | |
| 9908 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 9909 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 9910 | return NULL; |
| 9911 | opath = PyBytes_AsString(opathold); |
| 9912 | npath = PyBytes_AsString(opathnew); |
| 9913 | Py_BEGIN_ALLOW_THREADS |
| 9914 | res = renameat(oldfd, opath, newfd, npath); |
| 9915 | Py_END_ALLOW_THREADS |
| 9916 | Py_DECREF(opathold); |
| 9917 | Py_DECREF(opathnew); |
| 9918 | if (res < 0) |
| 9919 | return posix_error(); |
| 9920 | Py_RETURN_NONE; |
| 9921 | } |
| 9922 | #endif |
| 9923 | |
| 9924 | #if HAVE_SYMLINKAT |
| 9925 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 9926 | "symlinkat(src, dstfd, dst)\n\n\ |
| 9927 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 9928 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 9929 | is interpreted relative to the current working directory."); |
| 9930 | |
| 9931 | static PyObject * |
| 9932 | posix_symlinkat(PyObject *self, PyObject *args) |
| 9933 | { |
| 9934 | int res, dstfd; |
| 9935 | PyObject *osrc, *odst; |
| 9936 | char *src, *dst; |
| 9937 | |
| 9938 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 9939 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 9940 | return NULL; |
| 9941 | src = PyBytes_AsString(osrc); |
| 9942 | dst = PyBytes_AsString(odst); |
| 9943 | Py_BEGIN_ALLOW_THREADS |
| 9944 | res = symlinkat(src, dstfd, dst); |
| 9945 | Py_END_ALLOW_THREADS |
| 9946 | Py_DECREF(osrc); |
| 9947 | Py_DECREF(odst); |
| 9948 | if (res < 0) |
| 9949 | return posix_error(); |
| 9950 | Py_RETURN_NONE; |
| 9951 | } |
| 9952 | #endif /* HAVE_SYMLINKAT */ |
| 9953 | |
| 9954 | #ifdef HAVE_UNLINKAT |
| 9955 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 9956 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 9957 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9958 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 9959 | specified, unlinkat() behaves like rmdir().\n\ |
| 9960 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9961 | is interpreted relative to the current working directory."); |
| 9962 | |
| 9963 | static PyObject * |
| 9964 | posix_unlinkat(PyObject *self, PyObject *args) |
| 9965 | { |
| 9966 | int dirfd, res, flags = 0; |
| 9967 | PyObject *opath; |
| 9968 | char *path; |
| 9969 | |
| 9970 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 9971 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9972 | return NULL; |
| 9973 | path = PyBytes_AsString(opath); |
| 9974 | Py_BEGIN_ALLOW_THREADS |
| 9975 | res = unlinkat(dirfd, path, flags); |
| 9976 | Py_END_ALLOW_THREADS |
| 9977 | Py_DECREF(opath); |
| 9978 | if (res < 0) |
| 9979 | return posix_error(); |
| 9980 | Py_RETURN_NONE; |
| 9981 | } |
| 9982 | #endif |
| 9983 | |
| 9984 | #ifdef HAVE_UTIMENSAT |
| 9985 | PyDoc_STRVAR(posix_utimensat__doc__, |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 9986 | "utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\ |
| 9987 | mtime=(mtime_sec, mtime_nsec), flags=0])\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9988 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 9989 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 9990 | relative, it is taken as relative to dirfd.\n\ |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 9991 | If atime and mtime are both None, which is the default, set atime and\n\ |
| 9992 | mtime to the current time.\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9993 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9994 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9995 | is interpreted relative to the current working directory.\n\ |
| 9996 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 9997 | current time.\n\ |
| 9998 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 9999 | |
| 10000 | static PyObject * |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10001 | posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10002 | { |
| 10003 | PyObject *opath; |
| 10004 | char *path; |
| 10005 | int res, dirfd, flags = 0; |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10006 | PyObject *atime = Py_None; |
| 10007 | PyObject *mtime = Py_None; |
| 10008 | |
| 10009 | static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL}; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10010 | |
| 10011 | struct timespec buf[2]; |
| 10012 | |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10013 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10014 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 10015 | return NULL; |
| 10016 | path = PyBytes_AsString(opath); |
| 10017 | if (atime == Py_None && mtime == Py_None) { |
| 10018 | /* optional time values not given */ |
| 10019 | Py_BEGIN_ALLOW_THREADS |
| 10020 | res = utimensat(dirfd, path, NULL, flags); |
| 10021 | Py_END_ALLOW_THREADS |
| 10022 | } |
| 10023 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 10024 | PyErr_SetString(PyExc_TypeError, |
| 10025 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 10026 | Py_DECREF(opath); |
| 10027 | return NULL; |
| 10028 | } |
| 10029 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 10030 | PyErr_SetString(PyExc_TypeError, |
| 10031 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 10032 | Py_DECREF(opath); |
| 10033 | return NULL; |
| 10034 | } |
| 10035 | else { |
| 10036 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 10037 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 10038 | Py_DECREF(opath); |
| 10039 | return NULL; |
| 10040 | } |
| 10041 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 10042 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 10043 | Py_DECREF(opath); |
| 10044 | return NULL; |
| 10045 | } |
| 10046 | Py_BEGIN_ALLOW_THREADS |
| 10047 | res = utimensat(dirfd, path, buf, flags); |
| 10048 | Py_END_ALLOW_THREADS |
| 10049 | } |
| 10050 | Py_DECREF(opath); |
| 10051 | if (res < 0) { |
| 10052 | return posix_error(); |
| 10053 | } |
| 10054 | Py_RETURN_NONE; |
| 10055 | } |
| 10056 | #endif |
| 10057 | |
| 10058 | #ifdef HAVE_MKFIFOAT |
| 10059 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 10060 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 10061 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10062 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10063 | is interpreted relative to the current working directory."); |
| 10064 | |
| 10065 | static PyObject * |
| 10066 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 10067 | { |
| 10068 | PyObject *opath; |
| 10069 | char *filename; |
| 10070 | int mode = 0666; |
| 10071 | int res, dirfd; |
| 10072 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 10073 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 10074 | return NULL; |
| 10075 | filename = PyBytes_AS_STRING(opath); |
| 10076 | Py_BEGIN_ALLOW_THREADS |
| 10077 | res = mkfifoat(dirfd, filename, mode); |
| 10078 | Py_END_ALLOW_THREADS |
| 10079 | Py_DECREF(opath); |
| 10080 | if (res < 0) |
| 10081 | return posix_error(); |
| 10082 | Py_RETURN_NONE; |
| 10083 | } |
| 10084 | #endif |
| 10085 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10086 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10087 | |
| 10088 | static int |
| 10089 | try_getxattr(const char *path, const char *name, |
| 10090 | ssize_t (*get)(const char *, const char *, void *, size_t), |
| 10091 | Py_ssize_t buf_size, PyObject **res) |
| 10092 | { |
| 10093 | PyObject *value; |
| 10094 | Py_ssize_t len; |
| 10095 | |
| 10096 | assert(buf_size <= XATTR_SIZE_MAX); |
| 10097 | value = PyBytes_FromStringAndSize(NULL, buf_size); |
| 10098 | if (!value) |
| 10099 | return 0; |
| 10100 | Py_BEGIN_ALLOW_THREADS; |
| 10101 | len = get(path, name, PyBytes_AS_STRING(value), buf_size); |
| 10102 | Py_END_ALLOW_THREADS; |
| 10103 | if (len < 0) { |
| 10104 | Py_DECREF(value); |
| 10105 | if (errno == ERANGE) { |
| 10106 | value = NULL; |
| 10107 | } |
| 10108 | else { |
| 10109 | posix_error(); |
| 10110 | return 0; |
| 10111 | } |
| 10112 | } |
| 10113 | else if (len != buf_size) { |
| 10114 | /* Can only shrink. */ |
| 10115 | _PyBytes_Resize(&value, len); |
| 10116 | } |
| 10117 | *res = value; |
| 10118 | return 1; |
| 10119 | } |
| 10120 | |
| 10121 | static PyObject * |
| 10122 | getxattr_common(const char *path, PyObject *name_obj, |
| 10123 | ssize_t (*get)(const char *, const char *, void *, size_t)) |
| 10124 | { |
| 10125 | PyObject *value; |
| 10126 | const char *name = PyBytes_AS_STRING(name_obj); |
| 10127 | |
| 10128 | /* Try a small value first. */ |
| 10129 | if (!try_getxattr(path, name, get, 128, &value)) |
| 10130 | return NULL; |
| 10131 | if (value) |
| 10132 | return value; |
| 10133 | /* Now the maximum possible one. */ |
| 10134 | if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value)) |
| 10135 | return NULL; |
| 10136 | assert(value); |
| 10137 | return value; |
| 10138 | } |
| 10139 | |
| 10140 | PyDoc_STRVAR(posix_getxattr__doc__, |
| 10141 | "getxattr(path, attr) -> value\n\n\ |
| 10142 | Return the value of extended attribute *name* on *path*."); |
| 10143 | |
| 10144 | static PyObject * |
| 10145 | posix_getxattr(PyObject *self, PyObject *args) |
| 10146 | { |
| 10147 | PyObject *path, *res, *name; |
| 10148 | |
| 10149 | if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path, |
| 10150 | PyUnicode_FSConverter, &name)) |
| 10151 | return NULL; |
| 10152 | res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr); |
| 10153 | Py_DECREF(path); |
| 10154 | Py_DECREF(name); |
| 10155 | return res; |
| 10156 | } |
| 10157 | |
| 10158 | PyDoc_STRVAR(posix_lgetxattr__doc__, |
| 10159 | "lgetxattr(path, attr) -> value\n\n\ |
| 10160 | Like getxattr but don't follow symlinks."); |
| 10161 | |
| 10162 | static PyObject * |
| 10163 | posix_lgetxattr(PyObject *self, PyObject *args) |
| 10164 | { |
| 10165 | PyObject *path, *res, *name; |
| 10166 | |
| 10167 | if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path, |
| 10168 | PyUnicode_FSConverter, &name)) |
| 10169 | return NULL; |
| 10170 | res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr); |
| 10171 | Py_DECREF(path); |
| 10172 | Py_DECREF(name); |
| 10173 | return res; |
| 10174 | } |
| 10175 | |
| 10176 | static ssize_t |
| 10177 | wrap_fgetxattr(const char *path, const char *name, void *value, size_t size) |
| 10178 | { |
| 10179 | /* Hack to share code. */ |
| 10180 | return fgetxattr((int)(Py_uintptr_t)path, name, value, size); |
| 10181 | } |
| 10182 | |
| 10183 | PyDoc_STRVAR(posix_fgetxattr__doc__, |
| 10184 | "fgetxattr(fd, attr) -> value\n\n\ |
| 10185 | Like getxattr but operate on a fd instead of a path."); |
| 10186 | |
| 10187 | static PyObject * |
| 10188 | posix_fgetxattr(PyObject *self, PyObject *args) |
| 10189 | { |
| 10190 | PyObject *res, *name; |
| 10191 | int fd; |
| 10192 | |
| 10193 | if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name)) |
| 10194 | return NULL; |
| 10195 | res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr); |
| 10196 | Py_DECREF(name); |
| 10197 | return res; |
| 10198 | } |
| 10199 | |
| 10200 | PyDoc_STRVAR(posix_setxattr__doc__, |
| 10201 | "setxattr(path, attr, value, flags=0)\n\n\ |
| 10202 | Set extended attribute *attr* on *path* to *value*."); |
| 10203 | |
| 10204 | static PyObject * |
| 10205 | posix_setxattr(PyObject *self, PyObject *args) |
| 10206 | { |
| 10207 | PyObject *path, *name; |
| 10208 | Py_buffer data; |
| 10209 | int flags = 0, err; |
| 10210 | |
| 10211 | if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter, |
| 10212 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10213 | return NULL; |
| 10214 | Py_BEGIN_ALLOW_THREADS; |
| 10215 | err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10216 | data.buf, data.len, flags); |
| 10217 | Py_END_ALLOW_THREADS; |
| 10218 | Py_DECREF(path); |
| 10219 | Py_DECREF(name); |
| 10220 | PyBuffer_Release(&data); |
| 10221 | if (err) |
| 10222 | return posix_error(); |
| 10223 | Py_RETURN_NONE; |
| 10224 | } |
| 10225 | |
| 10226 | PyDoc_STRVAR(posix_lsetxattr__doc__, |
| 10227 | "lsetxattr(path, attr, value, flags=0)\n\n\ |
| 10228 | Like setxattr but don't follow symlinks."); |
| 10229 | |
| 10230 | static PyObject * |
| 10231 | posix_lsetxattr(PyObject *self, PyObject *args) |
| 10232 | { |
| 10233 | PyObject *path, *name; |
| 10234 | Py_buffer data; |
| 10235 | int flags = 0, err; |
| 10236 | |
| 10237 | if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter, |
| 10238 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10239 | return NULL; |
| 10240 | Py_BEGIN_ALLOW_THREADS; |
| 10241 | err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10242 | data.buf, data.len, flags); |
| 10243 | Py_END_ALLOW_THREADS; |
| 10244 | Py_DECREF(path); |
| 10245 | Py_DECREF(name); |
| 10246 | PyBuffer_Release(&data); |
| 10247 | if (err) |
| 10248 | return posix_error(); |
| 10249 | Py_RETURN_NONE; |
| 10250 | } |
| 10251 | |
| 10252 | PyDoc_STRVAR(posix_fsetxattr__doc__, |
| 10253 | "fsetxattr(fd, attr, value, flags=0)\n\n\ |
| 10254 | Like setxattr but operates on *fd* instead of a path."); |
| 10255 | |
| 10256 | static PyObject * |
| 10257 | posix_fsetxattr(PyObject *self, PyObject *args) |
| 10258 | { |
| 10259 | Py_buffer data; |
| 10260 | const char *name; |
| 10261 | int fd, flags = 0, err; |
| 10262 | |
| 10263 | if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter, |
| 10264 | &name, &data, &flags)) |
| 10265 | return NULL; |
| 10266 | Py_BEGIN_ALLOW_THREADS; |
| 10267 | err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags); |
| 10268 | Py_END_ALLOW_THREADS; |
| 10269 | Py_DECREF(name); |
| 10270 | PyBuffer_Release(&data); |
| 10271 | if (err) |
| 10272 | return posix_error(); |
| 10273 | Py_RETURN_NONE; |
| 10274 | } |
| 10275 | |
| 10276 | PyDoc_STRVAR(posix_removexattr__doc__, |
| 10277 | "removexattr(path, attr)\n\n\ |
| 10278 | Remove extended attribute *attr* on *path*."); |
| 10279 | |
| 10280 | static PyObject * |
| 10281 | posix_removexattr(PyObject *self, PyObject *args) |
| 10282 | { |
| 10283 | PyObject *path, *name; |
| 10284 | int err; |
| 10285 | |
| 10286 | if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path, |
| 10287 | PyUnicode_FSConverter, &name)) |
| 10288 | return NULL; |
| 10289 | Py_BEGIN_ALLOW_THREADS; |
| 10290 | err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10291 | Py_END_ALLOW_THREADS; |
| 10292 | Py_DECREF(path); |
| 10293 | Py_DECREF(name); |
| 10294 | if (err) |
| 10295 | return posix_error(); |
| 10296 | Py_RETURN_NONE; |
| 10297 | } |
| 10298 | |
| 10299 | PyDoc_STRVAR(posix_lremovexattr__doc__, |
| 10300 | "lremovexattr(path, attr)\n\n\ |
| 10301 | Like removexattr but don't follow symlinks."); |
| 10302 | |
| 10303 | static PyObject * |
| 10304 | posix_lremovexattr(PyObject *self, PyObject *args) |
| 10305 | { |
| 10306 | PyObject *path, *name; |
| 10307 | int err; |
| 10308 | |
| 10309 | if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path, |
| 10310 | PyUnicode_FSConverter, &name)) |
| 10311 | return NULL; |
| 10312 | Py_BEGIN_ALLOW_THREADS; |
| 10313 | err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10314 | Py_END_ALLOW_THREADS; |
| 10315 | Py_DECREF(path); |
| 10316 | Py_DECREF(name); |
| 10317 | if (err) |
| 10318 | return posix_error(); |
| 10319 | Py_RETURN_NONE; |
| 10320 | } |
| 10321 | |
| 10322 | PyDoc_STRVAR(posix_fremovexattr__doc__, |
| 10323 | "fremovexattr(fd, attr)\n\n\ |
| 10324 | Like removexattr but operates on a file descriptor."); |
| 10325 | |
| 10326 | static PyObject * |
| 10327 | posix_fremovexattr(PyObject *self, PyObject *args) |
| 10328 | { |
| 10329 | PyObject *name; |
| 10330 | int fd, err; |
| 10331 | |
| 10332 | if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd, |
| 10333 | PyUnicode_FSConverter, &name)) |
| 10334 | return NULL; |
| 10335 | Py_BEGIN_ALLOW_THREADS; |
| 10336 | err = fremovexattr(fd, PyBytes_AS_STRING(name)); |
| 10337 | Py_END_ALLOW_THREADS; |
| 10338 | Py_DECREF(name); |
| 10339 | if (err) |
| 10340 | return posix_error(); |
| 10341 | Py_RETURN_NONE; |
| 10342 | } |
| 10343 | |
| 10344 | static Py_ssize_t |
| 10345 | try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t), |
| 10346 | Py_ssize_t buf_size, char **buf) |
| 10347 | { |
| 10348 | Py_ssize_t len; |
| 10349 | |
| 10350 | *buf = PyMem_MALLOC(buf_size); |
| 10351 | if (!*buf) { |
| 10352 | PyErr_NoMemory(); |
| 10353 | return -1; |
| 10354 | } |
| 10355 | Py_BEGIN_ALLOW_THREADS; |
| 10356 | len = list(path, *buf, buf_size); |
| 10357 | Py_END_ALLOW_THREADS; |
| 10358 | if (len < 0) { |
| 10359 | PyMem_FREE(*buf); |
| 10360 | if (errno != ERANGE) |
| 10361 | posix_error(); |
| 10362 | return -1; |
| 10363 | } |
| 10364 | return len; |
| 10365 | } |
| 10366 | |
| 10367 | static PyObject * |
| 10368 | listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t)) |
| 10369 | { |
| 10370 | PyObject *res, *attr; |
| 10371 | Py_ssize_t len, err, start, i; |
| 10372 | char *buf; |
| 10373 | |
| 10374 | len = try_listxattr(path, list, 256, &buf); |
| 10375 | if (len < 0) { |
| 10376 | if (PyErr_Occurred()) |
| 10377 | return NULL; |
| 10378 | len = try_listxattr(path, list, XATTR_LIST_MAX, &buf); |
| 10379 | if (len < 0) |
| 10380 | return NULL; |
| 10381 | } |
| 10382 | res = PyList_New(0); |
| 10383 | if (!res) { |
| 10384 | PyMem_FREE(buf); |
| 10385 | return NULL; |
| 10386 | } |
| 10387 | for (start = i = 0; i < len; i++) { |
| 10388 | if (!buf[i]) { |
| 10389 | attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start); |
| 10390 | if (!attr) { |
| 10391 | Py_DECREF(res); |
| 10392 | PyMem_FREE(buf); |
| 10393 | return NULL; |
| 10394 | } |
| 10395 | err = PyList_Append(res, attr); |
| 10396 | Py_DECREF(attr); |
| 10397 | if (err) { |
| 10398 | Py_DECREF(res); |
| 10399 | PyMem_FREE(buf); |
| 10400 | return NULL; |
| 10401 | } |
| 10402 | start = i + 1; |
| 10403 | } |
| 10404 | } |
| 10405 | PyMem_FREE(buf); |
| 10406 | return res; |
| 10407 | } |
| 10408 | |
| 10409 | PyDoc_STRVAR(posix_listxattr__doc__, |
| 10410 | "listxattr(path)\n\n\ |
| 10411 | Return a list of extended attributes on *path*."); |
| 10412 | |
| 10413 | static PyObject * |
| 10414 | posix_listxattr(PyObject *self, PyObject *args) |
| 10415 | { |
| 10416 | PyObject *path, *res; |
| 10417 | |
| 10418 | if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path)) |
| 10419 | return NULL; |
| 10420 | res = listxattr_common(PyBytes_AS_STRING(path), listxattr); |
| 10421 | Py_DECREF(path); |
| 10422 | return res; |
| 10423 | } |
| 10424 | |
| 10425 | PyDoc_STRVAR(posix_llistxattr__doc__, |
| 10426 | "llistxattr(path)\n\n\ |
| 10427 | Like listxattr but don't follow symlinks.."); |
| 10428 | |
| 10429 | static PyObject * |
| 10430 | posix_llistxattr(PyObject *self, PyObject *args) |
| 10431 | { |
| 10432 | PyObject *path, *res; |
| 10433 | |
| 10434 | if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path)) |
| 10435 | return NULL; |
| 10436 | res = listxattr_common(PyBytes_AS_STRING(path), llistxattr); |
| 10437 | Py_DECREF(path); |
| 10438 | return res; |
| 10439 | } |
| 10440 | |
| 10441 | static ssize_t |
| 10442 | wrap_flistxattr(const char *path, char *buf, size_t len) |
| 10443 | { |
| 10444 | /* Hack to share code. */ |
| 10445 | return flistxattr((int)(Py_uintptr_t)path, buf, len); |
| 10446 | } |
| 10447 | |
| 10448 | PyDoc_STRVAR(posix_flistxattr__doc__, |
| 10449 | "flistxattr(path)\n\n\ |
| 10450 | Like flistxattr but operates on a file descriptor."); |
| 10451 | |
| 10452 | static PyObject * |
| 10453 | posix_flistxattr(PyObject *self, PyObject *args) |
| 10454 | { |
| 10455 | long fd; |
| 10456 | |
| 10457 | if (!PyArg_ParseTuple(args, "i:flistxattr", &fd)) |
| 10458 | return NULL; |
| 10459 | return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr); |
| 10460 | } |
| 10461 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10462 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10463 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10464 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10465 | PyDoc_STRVAR(posix_urandom__doc__, |
| 10466 | "urandom(n) -> str\n\n\ |
| 10467 | Return n random bytes suitable for cryptographic use."); |
| 10468 | |
| 10469 | static PyObject * |
| 10470 | posix_urandom(PyObject *self, PyObject *args) |
| 10471 | { |
| 10472 | Py_ssize_t size; |
| 10473 | PyObject *result; |
| 10474 | int ret; |
| 10475 | |
| 10476 | /* Read arguments */ |
| 10477 | if (!PyArg_ParseTuple(args, "n:urandom", &size)) |
| 10478 | return NULL; |
| 10479 | if (size < 0) |
| 10480 | return PyErr_Format(PyExc_ValueError, |
| 10481 | "negative argument not allowed"); |
| 10482 | result = PyBytes_FromStringAndSize(NULL, size); |
| 10483 | if (result == NULL) |
| 10484 | return NULL; |
| 10485 | |
| 10486 | ret = _PyOS_URandom(PyBytes_AS_STRING(result), |
| 10487 | PyBytes_GET_SIZE(result)); |
| 10488 | if (ret == -1) { |
| 10489 | Py_DECREF(result); |
| 10490 | return NULL; |
| 10491 | } |
| 10492 | return result; |
| 10493 | } |
| 10494 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 10495 | /* Terminal size querying */ |
| 10496 | |
| 10497 | static PyTypeObject TerminalSizeType; |
| 10498 | |
| 10499 | PyDoc_STRVAR(TerminalSize_docstring, |
| 10500 | "A tuple of (columns, lines) for holding terminal window size"); |
| 10501 | |
| 10502 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 10503 | {"columns", "width of the terminal window in characters"}, |
| 10504 | {"lines", "height of the terminal window in characters"}, |
| 10505 | {NULL, NULL} |
| 10506 | }; |
| 10507 | |
| 10508 | static PyStructSequence_Desc TerminalSize_desc = { |
| 10509 | "os.terminal_size", |
| 10510 | TerminalSize_docstring, |
| 10511 | TerminalSize_fields, |
| 10512 | 2, |
| 10513 | }; |
| 10514 | |
| 10515 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 10516 | PyDoc_STRVAR(termsize__doc__, |
| 10517 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 10518 | "\n" \ |
| 10519 | "The optional argument fd (default standard output) specifies\n" \ |
| 10520 | "which file descriptor should be queried.\n" \ |
| 10521 | "\n" \ |
| 10522 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 10523 | "is thrown.\n" \ |
| 10524 | "\n" \ |
| 10525 | "This function will only be defined if an implementation is\n" \ |
| 10526 | "available for this system.\n" \ |
| 10527 | "\n" \ |
| 10528 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 10529 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 10530 | |
| 10531 | static PyObject* |
| 10532 | get_terminal_size(PyObject *self, PyObject *args) |
| 10533 | { |
| 10534 | int columns, lines; |
| 10535 | PyObject *termsize; |
| 10536 | |
| 10537 | int fd = fileno(stdout); |
| 10538 | /* Under some conditions stdout may not be connected and |
| 10539 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 10540 | * GUI apps don't have valid standard streams by default. |
| 10541 | * |
| 10542 | * If this happens, and the optional fd argument is not present, |
| 10543 | * the ioctl below will fail returning EBADF. This is what we want. |
| 10544 | */ |
| 10545 | |
| 10546 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 10547 | return NULL; |
| 10548 | |
| 10549 | #ifdef TERMSIZE_USE_IOCTL |
| 10550 | { |
| 10551 | struct winsize w; |
| 10552 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 10553 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10554 | columns = w.ws_col; |
| 10555 | lines = w.ws_row; |
| 10556 | } |
| 10557 | #endif /* TERMSIZE_USE_IOCTL */ |
| 10558 | |
| 10559 | #ifdef TERMSIZE_USE_CONIO |
| 10560 | { |
| 10561 | DWORD nhandle; |
| 10562 | HANDLE handle; |
| 10563 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 10564 | switch (fd) { |
| 10565 | case 0: nhandle = STD_INPUT_HANDLE; |
| 10566 | break; |
| 10567 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 10568 | break; |
| 10569 | case 2: nhandle = STD_ERROR_HANDLE; |
| 10570 | break; |
| 10571 | default: |
| 10572 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 10573 | } |
| 10574 | handle = GetStdHandle(nhandle); |
| 10575 | if (handle == NULL) |
| 10576 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 10577 | if (handle == INVALID_HANDLE_VALUE) |
| 10578 | return PyErr_SetFromWindowsErr(0); |
| 10579 | |
| 10580 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 10581 | return PyErr_SetFromWindowsErr(0); |
| 10582 | |
| 10583 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 10584 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 10585 | } |
| 10586 | #endif /* TERMSIZE_USE_CONIO */ |
| 10587 | |
| 10588 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 10589 | if (termsize == NULL) |
| 10590 | return NULL; |
| 10591 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 10592 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 10593 | if (PyErr_Occurred()) { |
| 10594 | Py_DECREF(termsize); |
| 10595 | return NULL; |
| 10596 | } |
| 10597 | return termsize; |
| 10598 | } |
| 10599 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 10600 | |
| 10601 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10602 | static PyMethodDef posix_methods[] = { |
Benjamin Peterson | eaa5eee | 2012-05-05 21:22:14 -0400 | [diff] [blame] | 10603 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10604 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10605 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10606 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10607 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10608 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10609 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10610 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10611 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10612 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10613 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10614 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10615 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10616 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10617 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10618 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10619 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10620 | #endif /* HAVE_LCHMOD */ |
| 10621 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10622 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10623 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10624 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10625 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10626 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10627 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10628 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10629 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10630 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10631 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10632 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10633 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10634 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10635 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10636 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10637 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10638 | METH_NOARGS, posix_getcwd__doc__}, |
| 10639 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10640 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10641 | #endif |
| 10642 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10643 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10644 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10645 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10646 | #ifdef HAVE_FDOPENDIR |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 10647 | {"flistdir", posix_flistdir, METH_VARARGS, posix_flistdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10648 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10649 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10650 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10651 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10652 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10653 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10654 | #ifdef HAVE_GETPRIORITY |
| 10655 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10656 | #endif /* HAVE_GETPRIORITY */ |
| 10657 | #ifdef HAVE_SETPRIORITY |
| 10658 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10659 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10660 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10661 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10662 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10663 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10664 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10665 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10666 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 10667 | {"replace", posix_replace, METH_VARARGS, posix_replace__doc__}, |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10668 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10669 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10670 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10671 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10672 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10673 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10674 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10675 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10676 | win_symlink__doc__}, |
| 10677 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10678 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10679 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10680 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10681 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10682 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10683 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10684 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10685 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 10686 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10687 | {"utime", (PyCFunction)posix_utime, |
| 10688 | METH_VARARGS | METH_KEYWORDS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10689 | #ifdef HAVE_FUTIMES |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10690 | {"futimes", (PyCFunction)posix_futimes, |
| 10691 | METH_VARARGS | METH_KEYWORDS, posix_futimes__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10692 | #endif |
| 10693 | #ifdef HAVE_LUTIMES |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 10694 | {"lutimes", (PyCFunction)posix_lutimes, |
| 10695 | METH_VARARGS | METH_KEYWORDS, posix_lutimes__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10696 | #endif |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10697 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10698 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10699 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10700 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10701 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10702 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 10703 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10704 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10705 | #ifdef HAVE_FEXECVE |
| 10706 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 10707 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10708 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10709 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10710 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10711 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10712 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10713 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10714 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10715 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10716 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10717 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10718 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10719 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10720 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10721 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10722 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10723 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10724 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10725 | {"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] | 10726 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10727 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10728 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10729 | #endif |
| 10730 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10731 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10732 | #endif |
| 10733 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10734 | {"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] | 10735 | #endif |
| 10736 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10737 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10738 | #endif |
| 10739 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10740 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10741 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10742 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10743 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10744 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10745 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10746 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10747 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10748 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10749 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10750 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10751 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10752 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10753 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10754 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10755 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 10756 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10757 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10758 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 10759 | #endif /* HAVE_GETEUID */ |
| 10760 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10761 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10762 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10763 | #ifdef HAVE_GETGROUPLIST |
| 10764 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10765 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10766 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10767 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10768 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10769 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10770 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10771 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10772 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10773 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10774 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10775 | #endif /* HAVE_GETPPID */ |
| 10776 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10777 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10778 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10779 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10780 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10781 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10782 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10783 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10784 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10785 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10786 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10787 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10788 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10789 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10790 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10791 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10792 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10793 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 10794 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10795 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10796 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10797 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10798 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10799 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10800 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10801 | #endif /* HAVE_SETEUID */ |
| 10802 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10803 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10804 | #endif /* HAVE_SETEGID */ |
| 10805 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10806 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10807 | #endif /* HAVE_SETREUID */ |
| 10808 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10809 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10810 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10811 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10812 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10813 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10814 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10815 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10816 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10817 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10818 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10819 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10820 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10821 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10822 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10823 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10824 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10825 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10826 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10827 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10828 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10829 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10830 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10831 | #endif /* HAVE_WAIT3 */ |
| 10832 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10833 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10834 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10835 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10836 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10837 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10838 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10839 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10840 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10841 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10842 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10843 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10844 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10845 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10846 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10847 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10848 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10849 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10850 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10851 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10852 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10853 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10854 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10855 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10856 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 10857 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10858 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10859 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10860 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10861 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10862 | #ifdef HAVE_LOCKF |
| 10863 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10864 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10865 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10866 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10867 | #ifdef HAVE_READV |
| 10868 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10869 | #endif |
| 10870 | #ifdef HAVE_PREAD |
| 10871 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10872 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10873 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10874 | #ifdef HAVE_WRITEV |
| 10875 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10876 | #endif |
| 10877 | #ifdef HAVE_PWRITE |
| 10878 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10879 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10880 | #ifdef HAVE_SENDFILE |
| 10881 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10882 | posix_sendfile__doc__}, |
| 10883 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 10884 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10885 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10886 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10887 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10888 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10889 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10890 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10891 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10892 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10893 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10894 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10895 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10896 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10897 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10898 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10899 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10900 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10901 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10902 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10903 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10904 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10905 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10906 | #ifdef HAVE_TRUNCATE |
| 10907 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 10908 | #endif |
| 10909 | #ifdef HAVE_POSIX_FALLOCATE |
| 10910 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10911 | #endif |
| 10912 | #ifdef HAVE_POSIX_FADVISE |
| 10913 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10914 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10915 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10916 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10917 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10918 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10919 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10920 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10921 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10922 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10923 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10924 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10925 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10926 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10927 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10928 | #ifdef HAVE_SYNC |
| 10929 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10930 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10931 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10932 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10933 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10934 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10935 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10936 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10937 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10938 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10939 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10940 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10941 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10942 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10943 | #endif /* WIFSTOPPED */ |
| 10944 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10945 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10946 | #endif /* WIFSIGNALED */ |
| 10947 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10948 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10949 | #endif /* WIFEXITED */ |
| 10950 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10951 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10952 | #endif /* WEXITSTATUS */ |
| 10953 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10954 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10955 | #endif /* WTERMSIG */ |
| 10956 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10957 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10958 | #endif /* WSTOPSIG */ |
| 10959 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10960 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10961 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10962 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10963 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10964 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10965 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10966 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10967 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10968 | #endif |
| 10969 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10970 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10971 | #endif |
| 10972 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10973 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10974 | #endif |
| 10975 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10976 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10977 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10978 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10979 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10981 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 10982 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10983 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10984 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10985 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10986 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10987 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10988 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 10989 | {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10990 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10991 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10992 | #endif |
| 10993 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10994 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10995 | #endif |
| 10996 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10997 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10998 | #endif |
| 10999 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11000 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11001 | #endif |
| 11002 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11003 | /* posix *at family of functions */ |
| 11004 | #ifdef HAVE_FACCESSAT |
| 11005 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 11006 | #endif |
| 11007 | #ifdef HAVE_FCHMODAT |
| 11008 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 11009 | #endif /* HAVE_FCHMODAT */ |
| 11010 | #ifdef HAVE_FCHOWNAT |
| 11011 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 11012 | #endif /* HAVE_FCHOWNAT */ |
| 11013 | #ifdef HAVE_FSTATAT |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11014 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11015 | #endif |
| 11016 | #ifdef HAVE_FUTIMESAT |
| 11017 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 11018 | #endif |
| 11019 | #ifdef HAVE_LINKAT |
| 11020 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 11021 | #endif /* HAVE_LINKAT */ |
| 11022 | #ifdef HAVE_MKDIRAT |
| 11023 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 11024 | #endif |
| 11025 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 11026 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 11027 | #endif |
| 11028 | #ifdef HAVE_OPENAT |
| 11029 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 11030 | #endif |
| 11031 | #ifdef HAVE_READLINKAT |
| 11032 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 11033 | #endif /* HAVE_READLINKAT */ |
| 11034 | #ifdef HAVE_RENAMEAT |
| 11035 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 11036 | #endif |
| 11037 | #if HAVE_SYMLINKAT |
| 11038 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 11039 | #endif /* HAVE_SYMLINKAT */ |
| 11040 | #ifdef HAVE_UNLINKAT |
| 11041 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 11042 | #endif |
| 11043 | #ifdef HAVE_UTIMENSAT |
Jesus Cea | d03a491 | 2011-11-08 17:28:04 +0100 | [diff] [blame] | 11044 | {"utimensat", (PyCFunction)posix_utimensat, |
| 11045 | METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 11046 | posix_utimensat__doc__}, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11047 | #endif |
| 11048 | #ifdef HAVE_MKFIFOAT |
| 11049 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 11050 | #endif |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11051 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11052 | {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__}, |
| 11053 | {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__}, |
| 11054 | {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__}, |
| 11055 | {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__}, |
| 11056 | {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__}, |
| 11057 | {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__}, |
| 11058 | {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__}, |
| 11059 | {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__}, |
| 11060 | {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__}, |
| 11061 | {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__}, |
| 11062 | {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__}, |
| 11063 | {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__}, |
| 11064 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11065 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 11066 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 11067 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11068 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11069 | }; |
| 11070 | |
| 11071 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11072 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11073 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11074 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11075 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11076 | } |
| 11077 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11078 | #if defined(PYOS_OS2) |
| 11079 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11080 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11081 | { |
| 11082 | APIRET rc; |
| 11083 | ULONG values[QSV_MAX+1]; |
| 11084 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 11085 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11086 | |
| 11087 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 11088 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11089 | Py_END_ALLOW_THREADS |
| 11090 | |
| 11091 | if (rc != NO_ERROR) { |
| 11092 | os2_error(rc); |
| 11093 | return -1; |
| 11094 | } |
| 11095 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11096 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 11097 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 11098 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 11099 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 11100 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 11101 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 11102 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11103 | |
| 11104 | switch (values[QSV_VERSION_MINOR]) { |
| 11105 | case 0: ver = "2.00"; break; |
| 11106 | case 10: ver = "2.10"; break; |
| 11107 | case 11: ver = "2.11"; break; |
| 11108 | case 30: ver = "3.00"; break; |
| 11109 | case 40: ver = "4.00"; break; |
| 11110 | case 50: ver = "5.00"; break; |
| 11111 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11112 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11114 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11115 | ver = &tmp[0]; |
| 11116 | } |
| 11117 | |
| 11118 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11119 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11120 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11121 | |
| 11122 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 11123 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 11124 | tmp[1] = ':'; |
| 11125 | tmp[2] = '\0'; |
| 11126 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11127 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11128 | } |
| 11129 | #endif |
| 11130 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11131 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11132 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11133 | enable_symlink() |
| 11134 | { |
| 11135 | HANDLE tok; |
| 11136 | TOKEN_PRIVILEGES tok_priv; |
| 11137 | LUID luid; |
| 11138 | int meth_idx = 0; |
| 11139 | |
| 11140 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11141 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11142 | |
| 11143 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11144 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11145 | |
| 11146 | tok_priv.PrivilegeCount = 1; |
| 11147 | tok_priv.Privileges[0].Luid = luid; |
| 11148 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11149 | |
| 11150 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11151 | sizeof(TOKEN_PRIVILEGES), |
| 11152 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11153 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11154 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11155 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11156 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11157 | } |
| 11158 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11159 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11160 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11161 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11162 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11163 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11164 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11165 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11166 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11167 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11168 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11169 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11170 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11171 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11172 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11173 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11174 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11175 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11176 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11177 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11178 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11179 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11180 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11181 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11182 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11183 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11184 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11185 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11186 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11187 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11188 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11189 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11190 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11191 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11192 | #endif |
| 11193 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11194 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11195 | #endif |
| 11196 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11197 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11198 | #endif |
| 11199 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11200 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11201 | #endif |
| 11202 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11203 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11204 | #endif |
| 11205 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11206 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11207 | #endif |
| 11208 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11209 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11210 | #endif |
| 11211 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11212 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11213 | #endif |
| 11214 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11215 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11216 | #endif |
| 11217 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11218 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11219 | #endif |
| 11220 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11221 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11222 | #endif |
| 11223 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11224 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11225 | #endif |
| 11226 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11227 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11228 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11229 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11230 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11231 | #endif |
| 11232 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11233 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11234 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11235 | #ifdef O_XATTR |
| 11236 | if (ins(d, "O_XATTR", (long)O_XATTR)) return -1; |
| 11237 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11238 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11239 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11240 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11241 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11242 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11243 | #endif |
| 11244 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11245 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11246 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 11247 | #ifdef O_EXEC |
| 11248 | if (ins(d, "O_EXEC", (long)O_EXEC)) return -1; |
| 11249 | #endif |
| 11250 | #ifdef O_SEARCH |
| 11251 | if (ins(d, "O_SEARCH", (long)O_SEARCH)) return -1; |
| 11252 | #endif |
| 11253 | #ifdef O_TTY_INIT |
| 11254 | if (ins(d, "O_TTY_INIT", (long)O_TTY_INIT)) return -1; |
| 11255 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11256 | #ifdef PRIO_PROCESS |
| 11257 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11258 | #endif |
| 11259 | #ifdef PRIO_PGRP |
| 11260 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11261 | #endif |
| 11262 | #ifdef PRIO_USER |
| 11263 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11264 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11265 | #ifdef O_CLOEXEC |
| 11266 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11267 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11268 | #ifdef O_ACCMODE |
| 11269 | if (ins(d, "O_ACCMODE", (long)O_ACCMODE)) return -1; |
| 11270 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11271 | /* posix - constants for *at functions */ |
| 11272 | #ifdef AT_SYMLINK_NOFOLLOW |
| 11273 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 11274 | #endif |
| 11275 | #ifdef AT_EACCESS |
| 11276 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 11277 | #endif |
| 11278 | #ifdef AT_FDCWD |
| 11279 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 11280 | #endif |
| 11281 | #ifdef AT_REMOVEDIR |
| 11282 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 11283 | #endif |
| 11284 | #ifdef AT_SYMLINK_FOLLOW |
| 11285 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 11286 | #endif |
| 11287 | #ifdef UTIME_NOW |
| 11288 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11289 | #endif |
| 11290 | #ifdef UTIME_OMIT |
| 11291 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11292 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11293 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11294 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11295 | /* MS Windows */ |
| 11296 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11297 | /* Don't inherit in child processes. */ |
| 11298 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11299 | #endif |
| 11300 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11301 | /* Optimize for short life (keep in memory). */ |
| 11302 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11303 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11304 | #endif |
| 11305 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11306 | /* Automatically delete when last handle is closed. */ |
| 11307 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11308 | #endif |
| 11309 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11310 | /* Optimize for random access. */ |
| 11311 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11312 | #endif |
| 11313 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | /* Optimize for sequential access. */ |
| 11315 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11316 | #endif |
| 11317 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11318 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11319 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | /* Send a SIGIO signal whenever input or output |
| 11321 | becomes available on file descriptor */ |
| 11322 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11323 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11324 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11325 | /* Direct disk access. */ |
| 11326 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11327 | #endif |
| 11328 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11329 | /* Must be a directory. */ |
| 11330 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11331 | #endif |
| 11332 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11333 | /* Do not follow links. */ |
| 11334 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11335 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 11336 | #ifdef O_NOLINKS |
| 11337 | /* Fails if link count of the named file is greater than 1 */ |
| 11338 | if (ins(d, "O_NOLINKS", (long)O_NOLINKS)) return -1; |
| 11339 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11340 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11341 | /* Do not update the access time. */ |
| 11342 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11343 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11344 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11345 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11346 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11347 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11348 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11349 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11350 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11351 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11352 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11353 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11354 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11355 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11356 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11357 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11358 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11359 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11360 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11361 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11362 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11363 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11364 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11365 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11366 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11367 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11368 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11369 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11370 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11371 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11372 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11373 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11374 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11375 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11376 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11377 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11378 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11379 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11380 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11381 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11382 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11383 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11384 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11385 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11386 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11387 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11388 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11389 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11390 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11391 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11392 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11393 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11394 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11395 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11396 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11397 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11398 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11399 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11400 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11401 | #endif /* ST_RDONLY */ |
| 11402 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11403 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11404 | #endif /* ST_NOSUID */ |
| 11405 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11406 | /* FreeBSD sendfile() constants */ |
| 11407 | #ifdef SF_NODISKIO |
| 11408 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11409 | #endif |
| 11410 | #ifdef SF_MNOWAIT |
| 11411 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11412 | #endif |
| 11413 | #ifdef SF_SYNC |
| 11414 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11415 | #endif |
| 11416 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11417 | /* constants for posix_fadvise */ |
| 11418 | #ifdef POSIX_FADV_NORMAL |
| 11419 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11420 | #endif |
| 11421 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11422 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11423 | #endif |
| 11424 | #ifdef POSIX_FADV_RANDOM |
| 11425 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11426 | #endif |
| 11427 | #ifdef POSIX_FADV_NOREUSE |
| 11428 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11429 | #endif |
| 11430 | #ifdef POSIX_FADV_WILLNEED |
| 11431 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11432 | #endif |
| 11433 | #ifdef POSIX_FADV_DONTNEED |
| 11434 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11435 | #endif |
| 11436 | |
| 11437 | /* constants for waitid */ |
| 11438 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11439 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11440 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11441 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11442 | #endif |
| 11443 | #ifdef WEXITED |
| 11444 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11445 | #endif |
| 11446 | #ifdef WNOWAIT |
| 11447 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11448 | #endif |
| 11449 | #ifdef WSTOPPED |
| 11450 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11451 | #endif |
| 11452 | #ifdef CLD_EXITED |
| 11453 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11454 | #endif |
| 11455 | #ifdef CLD_DUMPED |
| 11456 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11457 | #endif |
| 11458 | #ifdef CLD_TRAPPED |
| 11459 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11460 | #endif |
| 11461 | #ifdef CLD_CONTINUED |
| 11462 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11463 | #endif |
| 11464 | |
| 11465 | /* constants for lockf */ |
| 11466 | #ifdef F_LOCK |
| 11467 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11468 | #endif |
| 11469 | #ifdef F_TLOCK |
| 11470 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11471 | #endif |
| 11472 | #ifdef F_ULOCK |
| 11473 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11474 | #endif |
| 11475 | #ifdef F_TEST |
| 11476 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11477 | #endif |
| 11478 | |
| 11479 | /* constants for futimens */ |
| 11480 | #ifdef UTIME_NOW |
| 11481 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11482 | #endif |
| 11483 | #ifdef UTIME_OMIT |
| 11484 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11485 | #endif |
| 11486 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11487 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11488 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11489 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11490 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11491 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11492 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11493 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11494 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11495 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11496 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11497 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11498 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11499 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11500 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11501 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11502 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11503 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11504 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11505 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11506 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11507 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11508 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11509 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11510 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11511 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11512 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11513 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11514 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11515 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11516 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11517 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11518 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11519 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11520 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11521 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11522 | #ifdef SCHED_SPORADIC |
| 11523 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11524 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11525 | #ifdef SCHED_BATCH |
| 11526 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11527 | #endif |
| 11528 | #ifdef SCHED_IDLE |
| 11529 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11530 | #endif |
| 11531 | #ifdef SCHED_RESET_ON_FORK |
| 11532 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11533 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11534 | #ifdef SCHED_SYS |
| 11535 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11536 | #endif |
| 11537 | #ifdef SCHED_IA |
| 11538 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11539 | #endif |
| 11540 | #ifdef SCHED_FSS |
| 11541 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11542 | #endif |
| 11543 | #ifdef SCHED_FX |
| 11544 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11545 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11546 | #endif |
| 11547 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11548 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11549 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11550 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11551 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11552 | #endif |
| 11553 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11554 | #ifdef RTLD_LAZY |
| 11555 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11556 | #endif |
| 11557 | #ifdef RTLD_NOW |
| 11558 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11559 | #endif |
| 11560 | #ifdef RTLD_GLOBAL |
| 11561 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11562 | #endif |
| 11563 | #ifdef RTLD_LOCAL |
| 11564 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11565 | #endif |
| 11566 | #ifdef RTLD_NODELETE |
| 11567 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11568 | #endif |
| 11569 | #ifdef RTLD_NOLOAD |
| 11570 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11571 | #endif |
| 11572 | #ifdef RTLD_DEEPBIND |
| 11573 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11574 | #endif |
| 11575 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11576 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11577 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11578 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11579 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11580 | } |
| 11581 | |
| 11582 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11583 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11584 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11585 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11586 | |
| 11587 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11588 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11589 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11590 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11591 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11592 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11593 | #define MODNAME "posix" |
| 11594 | #endif |
| 11595 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11596 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11597 | PyModuleDef_HEAD_INIT, |
| 11598 | MODNAME, |
| 11599 | posix__doc__, |
| 11600 | -1, |
| 11601 | posix_methods, |
| 11602 | NULL, |
| 11603 | NULL, |
| 11604 | NULL, |
| 11605 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11606 | }; |
| 11607 | |
| 11608 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11609 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11610 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11611 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11612 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11613 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11614 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11615 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11616 | #endif |
| 11617 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11618 | m = PyModule_Create(&posixmodule); |
| 11619 | if (m == NULL) |
| 11620 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11621 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11622 | /* Initialize environ dictionary */ |
| 11623 | v = convertenviron(); |
| 11624 | Py_XINCREF(v); |
| 11625 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11626 | return NULL; |
| 11627 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11628 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11629 | if (all_ins(m)) |
| 11630 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11631 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11632 | if (setup_confname_tables(m)) |
| 11633 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11634 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11635 | Py_INCREF(PyExc_OSError); |
| 11636 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11637 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11638 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11639 | if (PyType_Ready(&cpu_set_type) < 0) |
| 11640 | return NULL; |
| 11641 | Py_INCREF(&cpu_set_type); |
| 11642 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11643 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11644 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11645 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11646 | if (posix_putenv_garbage == NULL) |
| 11647 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11648 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11649 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11650 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11651 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11652 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11653 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11654 | #endif |
| 11655 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11656 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11657 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11658 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11659 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11660 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11661 | structseq_new = StatResultType.tp_new; |
| 11662 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11663 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11664 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11665 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11666 | #ifdef NEED_TICKS_PER_SECOND |
| 11667 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11668 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11669 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11670 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11671 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11672 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11673 | # endif |
| 11674 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11675 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11676 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11677 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11678 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11679 | SchedParamType.tp_new = sched_param_new; |
| 11680 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11681 | |
| 11682 | /* initialize TerminalSize_info */ |
| 11683 | PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc); |
| 11684 | Py_INCREF(&TerminalSizeType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11685 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11686 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11687 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11688 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11689 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11690 | Py_INCREF((PyObject*) &StatResultType); |
| 11691 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11692 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11693 | PyModule_AddObject(m, "statvfs_result", |
| 11694 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11695 | |
| 11696 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11697 | Py_INCREF(&SchedParamType); |
| 11698 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11699 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11700 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11701 | |
| 11702 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11703 | /* |
| 11704 | * Step 2 of weak-linking support on Mac OS X. |
| 11705 | * |
| 11706 | * The code below removes functions that are not available on the |
| 11707 | * currently active platform. |
| 11708 | * |
| 11709 | * This block allow one to use a python binary that was build on |
| 11710 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 11711 | * OSX 10.4. |
| 11712 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11713 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11714 | if (fstatvfs == NULL) { |
| 11715 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11716 | return NULL; |
| 11717 | } |
| 11718 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11719 | #endif /* HAVE_FSTATVFS */ |
| 11720 | |
| 11721 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11722 | if (statvfs == NULL) { |
| 11723 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11724 | return NULL; |
| 11725 | } |
| 11726 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11727 | #endif /* HAVE_STATVFS */ |
| 11728 | |
| 11729 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11730 | if (lchown == NULL) { |
| 11731 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11732 | return NULL; |
| 11733 | } |
| 11734 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11735 | #endif /* HAVE_LCHOWN */ |
| 11736 | |
| 11737 | |
| 11738 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 11739 | |
| 11740 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 11741 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 11742 | billion = PyLong_FromLong(1000000000); |
| 11743 | if (!billion) |
| 11744 | return NULL; |
| 11745 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11746 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11747 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11748 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11749 | |
| 11750 | #ifdef __cplusplus |
| 11751 | } |
| 11752 | #endif |