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 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
| 17 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18 | * 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] | 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 31 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 32 | #if defined(__VMS) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 33 | # 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] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 42 | "This module provides access to operating system functionality that is\n\ |
| 43 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 44 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #if defined(PYOS_OS2) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 49 | #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] | 50 | #define INCL_DOS |
| 51 | #define INCL_DOSERRORS |
| 52 | #define INCL_DOSPROCESS |
| 53 | #define INCL_NOPMAPI |
| 54 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 55 | #if defined(PYCC_GCC) |
| 56 | #include <ctype.h> |
| 57 | #include <io.h> |
| 58 | #include <stdio.h> |
| 59 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 60 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 61 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 64 | #ifdef HAVE_SYS_UIO_H |
| 65 | #include <sys/uio.h> |
| 66 | #endif |
| 67 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 70 | #endif /* HAVE_SYS_TYPES_H */ |
| 71 | |
| 72 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 73 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 77 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 78 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 81 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 82 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 84 | #ifdef HAVE_FCNTL_H |
| 85 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 86 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 88 | #ifdef HAVE_GRP_H |
| 89 | #include <grp.h> |
| 90 | #endif |
| 91 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 92 | #ifdef HAVE_SYSEXITS_H |
| 93 | #include <sysexits.h> |
| 94 | #endif /* HAVE_SYSEXITS_H */ |
| 95 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 96 | #ifdef HAVE_SYS_LOADAVG_H |
| 97 | #include <sys/loadavg.h> |
| 98 | #endif |
| 99 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 100 | #ifdef HAVE_LANGINFO_H |
| 101 | #include <langinfo.h> |
| 102 | #endif |
| 103 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 104 | #ifdef HAVE_SYS_SENDFILE_H |
| 105 | #include <sys/sendfile.h> |
| 106 | #endif |
| 107 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 108 | #ifdef HAVE_SCHED_H |
| 109 | #include <sched.h> |
| 110 | #endif |
| 111 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 112 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 113 | #ifdef HAVE_SYS_SOCKET_H |
| 114 | #include <sys/socket.h> |
| 115 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 116 | #endif |
| 117 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 118 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 119 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 120 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 121 | #include <process.h> |
| 122 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 123 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 124 | #define HAVE_GETCWD 1 |
| 125 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 126 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #if defined(__OS2__) |
| 128 | #define HAVE_EXECV 1 |
| 129 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 130 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 131 | #include <process.h> |
| 132 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 133 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #define HAVE_EXECV 1 |
| 135 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 136 | #define HAVE_OPENDIR 1 |
| 137 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 138 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 139 | #define HAVE_WAIT 1 |
| 140 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 141 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 142 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 143 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 144 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 145 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #define HAVE_EXECV 1 |
| 147 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 148 | #define HAVE_SYSTEM 1 |
| 149 | #define HAVE_CWAIT 1 |
| 150 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 151 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 152 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 153 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 154 | /* 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] | 155 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 156 | /* Unix functions that the configure script doesn't check for */ |
| 157 | #define HAVE_EXECV 1 |
| 158 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 159 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 160 | #define HAVE_FORK1 1 |
| 161 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 162 | #define HAVE_GETCWD 1 |
| 163 | #define HAVE_GETEGID 1 |
| 164 | #define HAVE_GETEUID 1 |
| 165 | #define HAVE_GETGID 1 |
| 166 | #define HAVE_GETPPID 1 |
| 167 | #define HAVE_GETUID 1 |
| 168 | #define HAVE_KILL 1 |
| 169 | #define HAVE_OPENDIR 1 |
| 170 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 171 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 172 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 173 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 174 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif /* _MSC_VER */ |
| 176 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 177 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 178 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 180 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 181 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 182 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 183 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 184 | (default) */ |
| 185 | extern char *ctermid_r(char *); |
| 186 | #endif |
| 187 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 188 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 189 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 190 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 191 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 192 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 193 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 194 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 196 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 197 | #endif |
| 198 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 199 | extern int chdir(char *); |
| 200 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 201 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 202 | extern int chdir(const char *); |
| 203 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 204 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 205 | #ifdef __BORLANDC__ |
| 206 | extern int chmod(const char *, int); |
| 207 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 208 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 209 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 210 | /*#ifdef HAVE_FCHMOD |
| 211 | extern int fchmod(int, mode_t); |
| 212 | #endif*/ |
| 213 | /*#ifdef HAVE_LCHMOD |
| 214 | extern int lchmod(const char *, mode_t); |
| 215 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 216 | extern int chown(const char *, uid_t, gid_t); |
| 217 | extern char *getcwd(char *, int); |
| 218 | extern char *strerror(int); |
| 219 | extern int link(const char *, const char *); |
| 220 | extern int rename(const char *, const char *); |
| 221 | extern int stat(const char *, struct stat *); |
| 222 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 224 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 225 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 227 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 228 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 231 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 233 | #ifdef HAVE_UTIME_H |
| 234 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 235 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 237 | #ifdef HAVE_SYS_UTIME_H |
| 238 | #include <sys/utime.h> |
| 239 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 240 | #endif /* HAVE_SYS_UTIME_H */ |
| 241 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | #ifdef HAVE_SYS_TIMES_H |
| 243 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 244 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | |
| 246 | #ifdef HAVE_SYS_PARAM_H |
| 247 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 248 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | |
| 250 | #ifdef HAVE_SYS_UTSNAME_H |
| 251 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 252 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 254 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 256 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 257 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 258 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 259 | #include <direct.h> |
| 260 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 261 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 263 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 264 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 265 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 267 | #endif |
| 268 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 269 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 270 | #endif |
| 271 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 273 | #endif |
| 274 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 276 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 277 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 279 | #endif |
| 280 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 282 | #endif |
| 283 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 285 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 286 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 287 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 288 | #endif |
| 289 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 290 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 291 | #endif |
| 292 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 293 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 294 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 295 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 296 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 297 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 298 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 299 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 300 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 301 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 302 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 303 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 304 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 306 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 307 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 308 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 309 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 310 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 311 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 312 | #define MAXPATHLEN PATH_MAX |
| 313 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 314 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 315 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #endif /* MAXPATHLEN */ |
| 317 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 318 | #ifdef UNION_WAIT |
| 319 | /* Emulate some macros on systems that have a union instead of macros */ |
| 320 | |
| 321 | #ifndef WIFEXITED |
| 322 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 323 | #endif |
| 324 | |
| 325 | #ifndef WEXITSTATUS |
| 326 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 327 | #endif |
| 328 | |
| 329 | #ifndef WTERMSIG |
| 330 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 331 | #endif |
| 332 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 333 | #define WAIT_TYPE union wait |
| 334 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 335 | |
| 336 | #else /* !UNION_WAIT */ |
| 337 | #define WAIT_TYPE int |
| 338 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 339 | #endif /* UNION_WAIT */ |
| 340 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 341 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 342 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 343 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 344 | #define USE_CTERMID_R |
| 345 | #endif |
| 346 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 347 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 348 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 349 | #undef FSTAT |
| 350 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 351 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 352 | # define STAT win32_stat |
| 353 | # define FSTAT win32_fstat |
| 354 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 355 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 356 | # define STAT stat |
| 357 | # define FSTAT fstat |
| 358 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 359 | #endif |
| 360 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 361 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 362 | #include <sys/mkdev.h> |
| 363 | #else |
| 364 | #if defined(MAJOR_IN_SYSMACROS) |
| 365 | #include <sys/sysmacros.h> |
| 366 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 367 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 368 | #include <sys/mkdev.h> |
| 369 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 370 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 371 | |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 372 | static int |
| 373 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 374 | { |
| 375 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 376 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 377 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 378 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 379 | #endif |
| 380 | if (PyErr_Occurred()) |
| 381 | return 0; |
| 382 | return 1; |
| 383 | } |
| 384 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 385 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 386 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 387 | * valid and throw an assertion if it isn't. |
| 388 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 389 | * an assertion can be useful, but it does contradict the POSIX standard |
| 390 | * which for write(2) states: |
| 391 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 392 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 393 | * writing." |
| 394 | * Furthermore, python allows the user to enter any old integer |
| 395 | * as a fd and should merely raise a python exception on error. |
| 396 | * The Microsoft CRT doesn't provide an official way to check for the |
| 397 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 398 | * 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] | 399 | * internal structures involved. |
| 400 | * The structures below must be updated for each version of visual studio |
| 401 | * according to the file internal.h in the CRT source, until MS comes |
| 402 | * up with a less hacky way to do this. |
| 403 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 404 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 405 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 406 | /* The actual size of the structure is determined at runtime. |
| 407 | * Only the first items must be present. |
| 408 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 409 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 410 | intptr_t osfhnd; |
| 411 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 412 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 413 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 414 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 415 | #define IOINFO_L2E 5 |
| 416 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 417 | #define IOINFO_ARRAYS 64 |
| 418 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 419 | #define FOPEN 0x01 |
| 420 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 421 | |
| 422 | /* This function emulates what the windows CRT does to validate file handles */ |
| 423 | int |
| 424 | _PyVerify_fd(int fd) |
| 425 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 426 | const int i1 = fd >> IOINFO_L2E; |
| 427 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 428 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 429 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 430 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 431 | /* Determine the actual size of the ioinfo structure, |
| 432 | * as used by the CRT loaded in memory |
| 433 | */ |
| 434 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 435 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 436 | } |
| 437 | if (sizeof_ioinfo == 0) { |
| 438 | /* This should not happen... */ |
| 439 | goto fail; |
| 440 | } |
| 441 | |
| 442 | /* See that it isn't a special CLEAR fileno */ |
| 443 | if (fd != _NO_CONSOLE_FILENO) { |
| 444 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 445 | * we check pointer validity and other info |
| 446 | */ |
| 447 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 448 | /* finally, check that the file is open */ |
| 449 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 450 | if (info->osfile & FOPEN) { |
| 451 | return 1; |
| 452 | } |
| 453 | } |
| 454 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 455 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 456 | errno = EBADF; |
| 457 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 461 | static int |
| 462 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 463 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 464 | if (!_PyVerify_fd(fd1)) |
| 465 | return 0; |
| 466 | if (fd2 == _NO_CONSOLE_FILENO) |
| 467 | return 0; |
| 468 | if ((unsigned)fd2 < _NHANDLE_) |
| 469 | return 1; |
| 470 | else |
| 471 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 472 | } |
| 473 | #else |
| 474 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 475 | #define _PyVerify_fd_dup2(A, B) (1) |
| 476 | #endif |
| 477 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 478 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 479 | /* The following structure was copied from |
| 480 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 481 | include doesn't seem to be present in the Windows SDK (at least as included |
| 482 | with Visual Studio Express). */ |
| 483 | typedef struct _REPARSE_DATA_BUFFER { |
| 484 | ULONG ReparseTag; |
| 485 | USHORT ReparseDataLength; |
| 486 | USHORT Reserved; |
| 487 | union { |
| 488 | struct { |
| 489 | USHORT SubstituteNameOffset; |
| 490 | USHORT SubstituteNameLength; |
| 491 | USHORT PrintNameOffset; |
| 492 | USHORT PrintNameLength; |
| 493 | ULONG Flags; |
| 494 | WCHAR PathBuffer[1]; |
| 495 | } SymbolicLinkReparseBuffer; |
| 496 | |
| 497 | struct { |
| 498 | USHORT SubstituteNameOffset; |
| 499 | USHORT SubstituteNameLength; |
| 500 | USHORT PrintNameOffset; |
| 501 | USHORT PrintNameLength; |
| 502 | WCHAR PathBuffer[1]; |
| 503 | } MountPointReparseBuffer; |
| 504 | |
| 505 | struct { |
| 506 | UCHAR DataBuffer[1]; |
| 507 | } GenericReparseBuffer; |
| 508 | }; |
| 509 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 510 | |
| 511 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 512 | GenericReparseBuffer) |
| 513 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 514 | |
| 515 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 516 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 517 | { |
| 518 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 519 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 520 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 521 | |
| 522 | if (0 == DeviceIoControl( |
| 523 | reparse_point_handle, |
| 524 | FSCTL_GET_REPARSE_POINT, |
| 525 | NULL, 0, /* in buffer */ |
| 526 | target_buffer, sizeof(target_buffer), |
| 527 | &n_bytes_returned, |
| 528 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 529 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 530 | |
| 531 | if (reparse_tag) |
| 532 | *reparse_tag = rdb->ReparseTag; |
| 533 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 534 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 535 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 536 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 537 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 538 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 539 | #ifdef WITH_NEXT_FRAMEWORK |
| 540 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 541 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 542 | */ |
| 543 | #include <crt_externs.h> |
| 544 | static char **environ; |
| 545 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 546 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 547 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 548 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 549 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 550 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 552 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 553 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 554 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 555 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 556 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 557 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 558 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 559 | APIRET rc; |
| 560 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 561 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 562 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 563 | d = PyDict_New(); |
| 564 | if (d == NULL) |
| 565 | return NULL; |
| 566 | #ifdef WITH_NEXT_FRAMEWORK |
| 567 | if (environ == NULL) |
| 568 | environ = *_NSGetEnviron(); |
| 569 | #endif |
| 570 | #ifdef MS_WINDOWS |
| 571 | /* _wenviron must be initialized in this way if the program is started |
| 572 | through main() instead of wmain(). */ |
| 573 | _wgetenv(L""); |
| 574 | if (_wenviron == NULL) |
| 575 | return d; |
| 576 | /* This part ignores errors */ |
| 577 | for (e = _wenviron; *e != NULL; e++) { |
| 578 | PyObject *k; |
| 579 | PyObject *v; |
| 580 | wchar_t *p = wcschr(*e, L'='); |
| 581 | if (p == NULL) |
| 582 | continue; |
| 583 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 584 | if (k == NULL) { |
| 585 | PyErr_Clear(); |
| 586 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 587 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 588 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 589 | if (v == NULL) { |
| 590 | PyErr_Clear(); |
| 591 | Py_DECREF(k); |
| 592 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 593 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 594 | if (PyDict_GetItem(d, k) == NULL) { |
| 595 | if (PyDict_SetItem(d, k, v) != 0) |
| 596 | PyErr_Clear(); |
| 597 | } |
| 598 | Py_DECREF(k); |
| 599 | Py_DECREF(v); |
| 600 | } |
| 601 | #else |
| 602 | if (environ == NULL) |
| 603 | return d; |
| 604 | /* This part ignores errors */ |
| 605 | for (e = environ; *e != NULL; e++) { |
| 606 | PyObject *k; |
| 607 | PyObject *v; |
| 608 | char *p = strchr(*e, '='); |
| 609 | if (p == NULL) |
| 610 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 611 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 612 | if (k == NULL) { |
| 613 | PyErr_Clear(); |
| 614 | continue; |
| 615 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 616 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 617 | if (v == NULL) { |
| 618 | PyErr_Clear(); |
| 619 | Py_DECREF(k); |
| 620 | continue; |
| 621 | } |
| 622 | if (PyDict_GetItem(d, k) == NULL) { |
| 623 | if (PyDict_SetItem(d, k, v) != 0) |
| 624 | PyErr_Clear(); |
| 625 | } |
| 626 | Py_DECREF(k); |
| 627 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 628 | } |
| 629 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 630 | #if defined(PYOS_OS2) |
| 631 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 632 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 633 | PyObject *v = PyBytes_FromString(buffer); |
| 634 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 635 | Py_DECREF(v); |
| 636 | } |
| 637 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 638 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 639 | PyObject *v = PyBytes_FromString(buffer); |
| 640 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 641 | Py_DECREF(v); |
| 642 | } |
| 643 | #endif |
| 644 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 647 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 648 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 649 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 650 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 651 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 652 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 653 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 654 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 655 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 656 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 657 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 660 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 661 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 662 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 663 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 664 | PyObject *name_str, *rc; |
| 665 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 666 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 667 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 668 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 669 | name_str); |
| 670 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 671 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 674 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 675 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 676 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 677 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 678 | /* XXX We should pass the function name along in the future. |
| 679 | (winreg.c also wants to pass the function name.) |
| 680 | This would however require an additional param to the |
| 681 | Windows error object, which is non-trivial. |
| 682 | */ |
| 683 | errno = GetLastError(); |
| 684 | if (filename) |
| 685 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 686 | else |
| 687 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 688 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 689 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 690 | static PyObject * |
| 691 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 692 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 693 | /* XXX - see win32_error for comments on 'function' */ |
| 694 | errno = GetLastError(); |
| 695 | if (filename) |
| 696 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 697 | else |
| 698 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 701 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 702 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 703 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 704 | if (PyUnicode_CheckExact(*param)) |
| 705 | Py_INCREF(*param); |
| 706 | else if (PyUnicode_Check(*param)) |
| 707 | /* For a Unicode subtype that's not a Unicode object, |
| 708 | return a true Unicode object with the same data. */ |
| 709 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 710 | PyUnicode_GET_SIZE(*param)); |
| 711 | else |
| 712 | *param = PyUnicode_FromEncodedObject(*param, |
| 713 | Py_FileSystemDefaultEncoding, |
| 714 | "strict"); |
| 715 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 718 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 719 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 720 | #if defined(PYOS_OS2) |
| 721 | /********************************************************************** |
| 722 | * Helper Function to Trim and Format OS/2 Messages |
| 723 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 724 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 725 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 726 | { |
| 727 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 728 | |
| 729 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 730 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 731 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 732 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 733 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 734 | } |
| 735 | |
| 736 | /* Add Optional Reason Text */ |
| 737 | if (reason) { |
| 738 | strcat(msgbuf, " : "); |
| 739 | strcat(msgbuf, reason); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | /********************************************************************** |
| 744 | * Decode an OS/2 Operating System Error Code |
| 745 | * |
| 746 | * A convenience function to lookup an OS/2 error code and return a |
| 747 | * text message we can use to raise a Python exception. |
| 748 | * |
| 749 | * Notes: |
| 750 | * The messages for errors returned from the OS/2 kernel reside in |
| 751 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 752 | * |
| 753 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 754 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 755 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 756 | { |
| 757 | APIRET rc; |
| 758 | ULONG msglen; |
| 759 | |
| 760 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 761 | Py_BEGIN_ALLOW_THREADS |
| 762 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 763 | errorcode, "oso001.msg", &msglen); |
| 764 | Py_END_ALLOW_THREADS |
| 765 | |
| 766 | if (rc == NO_ERROR) |
| 767 | os2_formatmsg(msgbuf, msglen, reason); |
| 768 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 769 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 770 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 771 | |
| 772 | return msgbuf; |
| 773 | } |
| 774 | |
| 775 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 776 | errors are not in a global variable e.g. 'errno' nor are |
| 777 | they congruent with posix error numbers. */ |
| 778 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 779 | static PyObject * |
| 780 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 781 | { |
| 782 | char text[1024]; |
| 783 | PyObject *v; |
| 784 | |
| 785 | os2_strerror(text, sizeof(text), code, ""); |
| 786 | |
| 787 | v = Py_BuildValue("(is)", code, text); |
| 788 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 789 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 790 | Py_DECREF(v); |
| 791 | } |
| 792 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 793 | } |
| 794 | |
| 795 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 796 | |
| 797 | /* POSIX generic methods */ |
| 798 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 799 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 800 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 801 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 802 | int fd; |
| 803 | int res; |
| 804 | fd = PyObject_AsFileDescriptor(fdobj); |
| 805 | if (fd < 0) |
| 806 | return NULL; |
| 807 | if (!_PyVerify_fd(fd)) |
| 808 | return posix_error(); |
| 809 | Py_BEGIN_ALLOW_THREADS |
| 810 | res = (*func)(fd); |
| 811 | Py_END_ALLOW_THREADS |
| 812 | if (res < 0) |
| 813 | return posix_error(); |
| 814 | Py_INCREF(Py_None); |
| 815 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 816 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 817 | |
| 818 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 819 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 820 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 821 | PyObject *opath1 = NULL; |
| 822 | char *path1; |
| 823 | int res; |
| 824 | if (!PyArg_ParseTuple(args, format, |
| 825 | PyUnicode_FSConverter, &opath1)) |
| 826 | return NULL; |
| 827 | path1 = PyBytes_AsString(opath1); |
| 828 | Py_BEGIN_ALLOW_THREADS |
| 829 | res = (*func)(path1); |
| 830 | Py_END_ALLOW_THREADS |
| 831 | if (res < 0) |
| 832 | return posix_error_with_allocated_filename(opath1); |
| 833 | Py_DECREF(opath1); |
| 834 | Py_INCREF(Py_None); |
| 835 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 838 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 839 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 840 | char *format, |
| 841 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 842 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 843 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 844 | char *path1, *path2; |
| 845 | int res; |
| 846 | if (!PyArg_ParseTuple(args, format, |
| 847 | PyUnicode_FSConverter, &opath1, |
| 848 | PyUnicode_FSConverter, &opath2)) { |
| 849 | return NULL; |
| 850 | } |
| 851 | path1 = PyBytes_AsString(opath1); |
| 852 | path2 = PyBytes_AsString(opath2); |
| 853 | Py_BEGIN_ALLOW_THREADS |
| 854 | res = (*func)(path1, path2); |
| 855 | Py_END_ALLOW_THREADS |
| 856 | Py_DECREF(opath1); |
| 857 | Py_DECREF(opath2); |
| 858 | if (res != 0) |
| 859 | /* XXX how to report both path1 and path2??? */ |
| 860 | return posix_error(); |
| 861 | Py_INCREF(Py_None); |
| 862 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 865 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 866 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 867 | win32_1str(PyObject* args, char* func, |
| 868 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 869 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 870 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 871 | PyObject *uni; |
| 872 | char *ansi; |
| 873 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 874 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 875 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 876 | PyErr_Clear(); |
| 877 | else { |
| 878 | Py_BEGIN_ALLOW_THREADS |
| 879 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 880 | Py_END_ALLOW_THREADS |
| 881 | if (!result) |
| 882 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 883 | Py_INCREF(Py_None); |
| 884 | return Py_None; |
| 885 | } |
| 886 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 887 | return NULL; |
| 888 | Py_BEGIN_ALLOW_THREADS |
| 889 | result = funcA(ansi); |
| 890 | Py_END_ALLOW_THREADS |
| 891 | if (!result) |
| 892 | return win32_error(func, ansi); |
| 893 | Py_INCREF(Py_None); |
| 894 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 895 | |
| 896 | } |
| 897 | |
| 898 | /* This is a reimplementation of the C library's chdir function, |
| 899 | but one that produces Win32 errors instead of DOS error codes. |
| 900 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 901 | it also needs to set "magic" environment variables indicating |
| 902 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 903 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 904 | win32_chdir(LPCSTR path) |
| 905 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 906 | char new_path[MAX_PATH+1]; |
| 907 | int result; |
| 908 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 909 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 910 | if(!SetCurrentDirectoryA(path)) |
| 911 | return FALSE; |
| 912 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 913 | if (!result) |
| 914 | return FALSE; |
| 915 | /* In the ANSI API, there should not be any paths longer |
| 916 | than MAX_PATH. */ |
| 917 | assert(result <= MAX_PATH+1); |
| 918 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 919 | strncmp(new_path, "//", 2) == 0) |
| 920 | /* UNC path, nothing to do. */ |
| 921 | return TRUE; |
| 922 | env[1] = new_path[0]; |
| 923 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* The Unicode version differs from the ANSI version |
| 927 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 928 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 929 | win32_wchdir(LPCWSTR path) |
| 930 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 931 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 932 | int result; |
| 933 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 934 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 935 | if(!SetCurrentDirectoryW(path)) |
| 936 | return FALSE; |
| 937 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 938 | if (!result) |
| 939 | return FALSE; |
| 940 | if (result > MAX_PATH+1) { |
| 941 | new_path = malloc(result * sizeof(wchar_t)); |
| 942 | if (!new_path) { |
| 943 | SetLastError(ERROR_OUTOFMEMORY); |
| 944 | return FALSE; |
| 945 | } |
| 946 | result = GetCurrentDirectoryW(result, new_path); |
| 947 | if (!result) { |
| 948 | free(new_path); |
| 949 | return FALSE; |
| 950 | } |
| 951 | } |
| 952 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 953 | wcsncmp(new_path, L"//", 2) == 0) |
| 954 | /* UNC path, nothing to do. */ |
| 955 | return TRUE; |
| 956 | env[1] = new_path[0]; |
| 957 | result = SetEnvironmentVariableW(env, new_path); |
| 958 | if (new_path != _new_path) |
| 959 | free(new_path); |
| 960 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 961 | } |
| 962 | #endif |
| 963 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 964 | #ifdef MS_WINDOWS |
| 965 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 966 | - time stamps are restricted to second resolution |
| 967 | - file modification times suffer from forth-and-back conversions between |
| 968 | UTC and local time |
| 969 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 970 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 971 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 972 | |
| 973 | struct win32_stat{ |
| 974 | int st_dev; |
| 975 | __int64 st_ino; |
| 976 | unsigned short st_mode; |
| 977 | int st_nlink; |
| 978 | int st_uid; |
| 979 | int st_gid; |
| 980 | int st_rdev; |
| 981 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 982 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 983 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 984 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 985 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 986 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 987 | int st_ctime_nsec; |
| 988 | }; |
| 989 | |
| 990 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 991 | |
| 992 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 993 | 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] | 994 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 995 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 996 | /* Cannot simply cast and dereference in_ptr, |
| 997 | since it might not be aligned properly */ |
| 998 | __int64 in; |
| 999 | memcpy(&in, in_ptr, sizeof(in)); |
| 1000 | *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] | 1001 | *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] | 1002 | } |
| 1003 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1004 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1005 | 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] | 1006 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1007 | /* XXX endianness */ |
| 1008 | __int64 out; |
| 1009 | out = time_in + secs_between_epochs; |
| 1010 | out = out * 10000000 + nsec_in / 100; |
| 1011 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1014 | /* Below, we *know* that ugo+r is 0444 */ |
| 1015 | #if _S_IREAD != 0400 |
| 1016 | #error Unsupported C library |
| 1017 | #endif |
| 1018 | static int |
| 1019 | attributes_to_mode(DWORD attr) |
| 1020 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1021 | int m = 0; |
| 1022 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1023 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1024 | else |
| 1025 | m |= _S_IFREG; |
| 1026 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1027 | m |= 0444; |
| 1028 | else |
| 1029 | m |= 0666; |
| 1030 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1034 | 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] | 1035 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1036 | memset(result, 0, sizeof(*result)); |
| 1037 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1038 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1039 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1040 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1041 | 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] | 1042 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1043 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1044 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1045 | /* first clear the S_IFMT bits */ |
| 1046 | result->st_mode ^= (result->st_mode & 0170000); |
| 1047 | /* now set the bits that make this a symlink */ |
| 1048 | result->st_mode |= 0120000; |
| 1049 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1051 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1054 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1055 | 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] | 1056 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1057 | HANDLE hFindFile; |
| 1058 | WIN32_FIND_DATAA FileData; |
| 1059 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1060 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1061 | return FALSE; |
| 1062 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1063 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1064 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1065 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1066 | info->ftCreationTime = FileData.ftCreationTime; |
| 1067 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1068 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1069 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1070 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1071 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1072 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1073 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1074 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1078 | 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] | 1079 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1080 | HANDLE hFindFile; |
| 1081 | WIN32_FIND_DATAW FileData; |
| 1082 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1083 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1084 | return FALSE; |
| 1085 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1086 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1087 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1088 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1089 | info->ftCreationTime = FileData.ftCreationTime; |
| 1090 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1091 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1092 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1093 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1094 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1095 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1096 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1097 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1100 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1101 | static int has_GetFinalPathNameByHandle = 0; |
| 1102 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1103 | DWORD); |
| 1104 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1105 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1106 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1107 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1108 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1109 | HINSTANCE hKernel32; |
| 1110 | /* only recheck */ |
| 1111 | if (!has_GetFinalPathNameByHandle) |
| 1112 | { |
| 1113 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 1114 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1115 | "GetFinalPathNameByHandleA"); |
| 1116 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1117 | "GetFinalPathNameByHandleW"); |
| 1118 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1119 | Py_GetFinalPathNameByHandleW; |
| 1120 | } |
| 1121 | return has_GetFinalPathNameByHandle; |
| 1122 | } |
| 1123 | |
| 1124 | static BOOL |
| 1125 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1126 | { |
| 1127 | int buf_size, result_length; |
| 1128 | wchar_t *buf; |
| 1129 | |
| 1130 | /* We have a good handle to the target, use it to determine |
| 1131 | the target path name (then we'll call lstat on it). */ |
| 1132 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1133 | VOLUME_NAME_DOS); |
| 1134 | if(!buf_size) |
| 1135 | return FALSE; |
| 1136 | |
| 1137 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1138 | if (!buf) { |
| 1139 | SetLastError(ERROR_OUTOFMEMORY); |
| 1140 | return FALSE; |
| 1141 | } |
| 1142 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1143 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1144 | buf, buf_size, VOLUME_NAME_DOS); |
| 1145 | |
| 1146 | if(!result_length) { |
| 1147 | free(buf); |
| 1148 | return FALSE; |
| 1149 | } |
| 1150 | |
| 1151 | if(!CloseHandle(hdl)) { |
| 1152 | free(buf); |
| 1153 | return FALSE; |
| 1154 | } |
| 1155 | |
| 1156 | buf[result_length] = 0; |
| 1157 | |
| 1158 | *target_path = buf; |
| 1159 | return TRUE; |
| 1160 | } |
| 1161 | |
| 1162 | static int |
| 1163 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1164 | BOOL traverse); |
| 1165 | static int |
| 1166 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1167 | BOOL traverse) |
| 1168 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1169 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1170 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1171 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1172 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1173 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1174 | const char *dot; |
| 1175 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1176 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1177 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1178 | traverse reparse point. */ |
| 1179 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1182 | hFile = CreateFileA( |
| 1183 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1184 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1185 | 0, /* share mode */ |
| 1186 | NULL, /* security attributes */ |
| 1187 | OPEN_EXISTING, |
| 1188 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1189 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1190 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1191 | the symlink path agin and not the actual final path. */ |
| 1192 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1193 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1194 | NULL); |
| 1195 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1196 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1197 | /* Either the target doesn't exist, or we don't have access to |
| 1198 | get a handle to it. If the former, we need to return an error. |
| 1199 | If the latter, we can use attributes_from_dir. */ |
| 1200 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1201 | return -1; |
| 1202 | /* Could not get attributes on open file. Fall back to |
| 1203 | reading the directory. */ |
| 1204 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1205 | /* Very strange. This should not fail now */ |
| 1206 | return -1; |
| 1207 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1208 | if (traverse) { |
| 1209 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1210 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1211 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1212 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1213 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1214 | } else { |
| 1215 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1216 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1217 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1218 | } |
| 1219 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1220 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1221 | return -1; |
| 1222 | |
| 1223 | /* Close the outer open file handle now that we're about to |
| 1224 | reopen it with different flags. */ |
| 1225 | if (!CloseHandle(hFile)) |
| 1226 | return -1; |
| 1227 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1228 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1229 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1230 | the file without the reparse handling flag set. */ |
| 1231 | hFile2 = CreateFileA( |
| 1232 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1233 | NULL, OPEN_EXISTING, |
| 1234 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1235 | NULL); |
| 1236 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1237 | return -1; |
| 1238 | |
| 1239 | if (!get_target_path(hFile2, &target_path)) |
| 1240 | return -1; |
| 1241 | |
| 1242 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1243 | free(target_path); |
| 1244 | return code; |
| 1245 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1246 | } else |
| 1247 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1248 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1249 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1250 | |
| 1251 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1252 | dot = strrchr(path, '.'); |
| 1253 | if (dot) { |
| 1254 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1255 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1256 | result->st_mode |= 0111; |
| 1257 | } |
| 1258 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1262 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1263 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1264 | { |
| 1265 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1266 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1267 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1268 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1269 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1270 | const wchar_t *dot; |
| 1271 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1272 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1273 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1274 | traverse reparse point. */ |
| 1275 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1278 | hFile = CreateFileW( |
| 1279 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1280 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1281 | 0, /* share mode */ |
| 1282 | NULL, /* security attributes */ |
| 1283 | OPEN_EXISTING, |
| 1284 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1285 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1286 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1287 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1288 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1289 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1290 | NULL); |
| 1291 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1292 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1293 | /* Either the target doesn't exist, or we don't have access to |
| 1294 | get a handle to it. If the former, we need to return an error. |
| 1295 | If the latter, we can use attributes_from_dir. */ |
| 1296 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1297 | return -1; |
| 1298 | /* Could not get attributes on open file. Fall back to |
| 1299 | reading the directory. */ |
| 1300 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1301 | /* Very strange. This should not fail now */ |
| 1302 | return -1; |
| 1303 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1304 | if (traverse) { |
| 1305 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1306 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1307 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1308 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1309 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1310 | } else { |
| 1311 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1312 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1313 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1314 | } |
| 1315 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1316 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1317 | return -1; |
| 1318 | |
| 1319 | /* Close the outer open file handle now that we're about to |
| 1320 | reopen it with different flags. */ |
| 1321 | if (!CloseHandle(hFile)) |
| 1322 | return -1; |
| 1323 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1324 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1325 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1326 | the file without the reparse handling flag set. */ |
| 1327 | hFile2 = CreateFileW( |
| 1328 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1329 | NULL, OPEN_EXISTING, |
| 1330 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1331 | NULL); |
| 1332 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1333 | return -1; |
| 1334 | |
| 1335 | if (!get_target_path(hFile2, &target_path)) |
| 1336 | return -1; |
| 1337 | |
| 1338 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1339 | free(target_path); |
| 1340 | return code; |
| 1341 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1342 | } else |
| 1343 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1344 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1345 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1346 | |
| 1347 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1348 | dot = wcsrchr(path, '.'); |
| 1349 | if (dot) { |
| 1350 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1351 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1352 | result->st_mode |= 0111; |
| 1353 | } |
| 1354 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1358 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1359 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1360 | /* Protocol violation: we explicitly clear errno, instead of |
| 1361 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1362 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1363 | errno = 0; |
| 1364 | return code; |
| 1365 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1366 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1367 | static int |
| 1368 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1369 | { |
| 1370 | /* Protocol violation: we explicitly clear errno, instead of |
| 1371 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1372 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1373 | errno = 0; |
| 1374 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1375 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1376 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1377 | |
| 1378 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1379 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1380 | default does not traverse symlinks and instead returns attributes for |
| 1381 | the symlink. |
| 1382 | |
| 1383 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1384 | win32_stat will first explicitly resolve the symlink target and then will |
| 1385 | call win32_lstat on that result. |
| 1386 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1387 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1388 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1389 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1390 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1391 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1392 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1395 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1396 | 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] | 1397 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1398 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | static int |
| 1402 | win32_stat(const char* path, struct win32_stat *result) |
| 1403 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1404 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1407 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1408 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1409 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1410 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | static int |
| 1414 | win32_fstat(int file_number, struct win32_stat *result) |
| 1415 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1416 | BY_HANDLE_FILE_INFORMATION info; |
| 1417 | HANDLE h; |
| 1418 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1419 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1420 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1421 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1422 | /* Protocol violation: we explicitly clear errno, instead of |
| 1423 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1424 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1425 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1426 | if (h == INVALID_HANDLE_VALUE) { |
| 1427 | /* This is really a C library error (invalid file handle). |
| 1428 | We set the Win32 error to the closes one matching. */ |
| 1429 | SetLastError(ERROR_INVALID_HANDLE); |
| 1430 | return -1; |
| 1431 | } |
| 1432 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1433 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1434 | type = GetFileType(h); |
| 1435 | if (type == FILE_TYPE_UNKNOWN) { |
| 1436 | DWORD error = GetLastError(); |
| 1437 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1438 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1439 | } |
| 1440 | /* else: valid but unknown file */ |
| 1441 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1442 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | if (type != FILE_TYPE_DISK) { |
| 1444 | if (type == FILE_TYPE_CHAR) |
| 1445 | result->st_mode = _S_IFCHR; |
| 1446 | else if (type == FILE_TYPE_PIPE) |
| 1447 | result->st_mode = _S_IFIFO; |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | if (!GetFileInformationByHandle(h, &info)) { |
| 1452 | return -1; |
| 1453 | } |
| 1454 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1455 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1456 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1457 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1458 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | #endif /* MS_WINDOWS */ |
| 1462 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1463 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1464 | "stat_result: Result from stat or lstat.\n\n\ |
| 1465 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1466 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1467 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1468 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1469 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1470 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1471 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1472 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1473 | |
| 1474 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1475 | {"st_mode", "protection bits"}, |
| 1476 | {"st_ino", "inode"}, |
| 1477 | {"st_dev", "device"}, |
| 1478 | {"st_nlink", "number of hard links"}, |
| 1479 | {"st_uid", "user ID of owner"}, |
| 1480 | {"st_gid", "group ID of owner"}, |
| 1481 | {"st_size", "total size, in bytes"}, |
| 1482 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1483 | {NULL, "integer time of last access"}, |
| 1484 | {NULL, "integer time of last modification"}, |
| 1485 | {NULL, "integer time of last change"}, |
| 1486 | {"st_atime", "time of last access"}, |
| 1487 | {"st_mtime", "time of last modification"}, |
| 1488 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1489 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1490 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1491 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1492 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1493 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1494 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1495 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1496 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1497 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1498 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1499 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1500 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1501 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1502 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1503 | #endif |
| 1504 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1505 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1506 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1507 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1508 | }; |
| 1509 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1510 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1511 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1512 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1513 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1514 | #endif |
| 1515 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1516 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1517 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1518 | #else |
| 1519 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1520 | #endif |
| 1521 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1522 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1523 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1524 | #else |
| 1525 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1526 | #endif |
| 1527 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1528 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1529 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1530 | #else |
| 1531 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1532 | #endif |
| 1533 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1534 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1535 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1536 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1537 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1538 | #endif |
| 1539 | |
| 1540 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1541 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1542 | #else |
| 1543 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1544 | #endif |
| 1545 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1546 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1547 | "stat_result", /* name */ |
| 1548 | stat_result__doc__, /* doc */ |
| 1549 | stat_result_fields, |
| 1550 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1551 | }; |
| 1552 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1553 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1554 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1555 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1556 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1557 | 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] | 1558 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1559 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1560 | |
| 1561 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1562 | {"f_bsize", }, |
| 1563 | {"f_frsize", }, |
| 1564 | {"f_blocks", }, |
| 1565 | {"f_bfree", }, |
| 1566 | {"f_bavail", }, |
| 1567 | {"f_files", }, |
| 1568 | {"f_ffree", }, |
| 1569 | {"f_favail", }, |
| 1570 | {"f_flag", }, |
| 1571 | {"f_namemax",}, |
| 1572 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1573 | }; |
| 1574 | |
| 1575 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1576 | "statvfs_result", /* name */ |
| 1577 | statvfs_result__doc__, /* doc */ |
| 1578 | statvfs_result_fields, |
| 1579 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1580 | }; |
| 1581 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1582 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1583 | PyDoc_STRVAR(waitid_result__doc__, |
| 1584 | "waitid_result: Result from waitid.\n\n\ |
| 1585 | This object may be accessed either as a tuple of\n\ |
| 1586 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1587 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1588 | \n\ |
| 1589 | See os.waitid for more information."); |
| 1590 | |
| 1591 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1592 | {"si_pid", }, |
| 1593 | {"si_uid", }, |
| 1594 | {"si_signo", }, |
| 1595 | {"si_status", }, |
| 1596 | {"si_code", }, |
| 1597 | {0} |
| 1598 | }; |
| 1599 | |
| 1600 | static PyStructSequence_Desc waitid_result_desc = { |
| 1601 | "waitid_result", /* name */ |
| 1602 | waitid_result__doc__, /* doc */ |
| 1603 | waitid_result_fields, |
| 1604 | 5 |
| 1605 | }; |
| 1606 | static PyTypeObject WaitidResultType; |
| 1607 | #endif |
| 1608 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1609 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1610 | static PyTypeObject StatResultType; |
| 1611 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1612 | static PyTypeObject SchedParamType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1613 | static newfunc structseq_new; |
| 1614 | |
| 1615 | static PyObject * |
| 1616 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1617 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1618 | PyStructSequence *result; |
| 1619 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1620 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1621 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1622 | if (!result) |
| 1623 | return NULL; |
| 1624 | /* If we have been initialized from a tuple, |
| 1625 | st_?time might be set to None. Initialize it |
| 1626 | from the int slots. */ |
| 1627 | for (i = 7; i <= 9; i++) { |
| 1628 | if (result->ob_item[i+3] == Py_None) { |
| 1629 | Py_DECREF(Py_None); |
| 1630 | Py_INCREF(result->ob_item[i]); |
| 1631 | result->ob_item[i+3] = result->ob_item[i]; |
| 1632 | } |
| 1633 | } |
| 1634 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1640 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1641 | |
| 1642 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1643 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1644 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1645 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1646 | future calls return ints. \n\ |
| 1647 | If newval is omitted, return the current setting.\n"); |
| 1648 | |
| 1649 | static PyObject* |
| 1650 | stat_float_times(PyObject* self, PyObject *args) |
| 1651 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1652 | int newval = -1; |
| 1653 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1654 | return NULL; |
| 1655 | if (newval == -1) |
| 1656 | /* Return old value */ |
| 1657 | return PyBool_FromLong(_stat_float_times); |
| 1658 | _stat_float_times = newval; |
| 1659 | Py_INCREF(Py_None); |
| 1660 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1661 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1662 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1663 | static void |
| 1664 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1665 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1666 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1667 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1668 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1669 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1670 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1671 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1672 | if (!ival) |
| 1673 | return; |
| 1674 | if (_stat_float_times) { |
| 1675 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1676 | } else { |
| 1677 | fval = ival; |
| 1678 | Py_INCREF(fval); |
| 1679 | } |
| 1680 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1681 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1684 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1685 | (used by posix_stat() and posix_fstat()) */ |
| 1686 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1687 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1688 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1689 | unsigned long ansec, mnsec, cnsec; |
| 1690 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1691 | if (v == NULL) |
| 1692 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1693 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1694 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1695 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1696 | PyStructSequence_SET_ITEM(v, 1, |
| 1697 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1698 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1699 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1700 | #endif |
| 1701 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1702 | PyStructSequence_SET_ITEM(v, 2, |
| 1703 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1704 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1705 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1706 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1707 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1708 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1709 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1710 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1711 | PyStructSequence_SET_ITEM(v, 6, |
| 1712 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1713 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1714 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1715 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1716 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1717 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1718 | ansec = st->st_atim.tv_nsec; |
| 1719 | mnsec = st->st_mtim.tv_nsec; |
| 1720 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1721 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1722 | ansec = st->st_atimespec.tv_nsec; |
| 1723 | mnsec = st->st_mtimespec.tv_nsec; |
| 1724 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1725 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1726 | ansec = st->st_atime_nsec; |
| 1727 | mnsec = st->st_mtime_nsec; |
| 1728 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1729 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1730 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1731 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1732 | fill_time(v, 7, st->st_atime, ansec); |
| 1733 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1734 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1735 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1736 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1737 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1738 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1739 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1740 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1741 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1742 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1743 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1744 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1745 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1746 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1747 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1748 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1749 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1750 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1751 | #endif |
| 1752 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1753 | { |
| 1754 | PyObject *val; |
| 1755 | unsigned long bsec,bnsec; |
| 1756 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1757 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1759 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1760 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1761 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1762 | if (_stat_float_times) { |
| 1763 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1764 | } else { |
| 1765 | val = PyLong_FromLong((long)bsec); |
| 1766 | } |
| 1767 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1768 | val); |
| 1769 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1770 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1771 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1772 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1773 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1774 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1775 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1776 | if (PyErr_Occurred()) { |
| 1777 | Py_DECREF(v); |
| 1778 | return NULL; |
| 1779 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1780 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1781 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1784 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1785 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1786 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1787 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1788 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1789 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1790 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1791 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1792 | char *wformat, |
| 1793 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1794 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1795 | STRUCT_STAT st; |
| 1796 | PyObject *opath; |
| 1797 | char *path; |
| 1798 | int res; |
| 1799 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1800 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1801 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1802 | PyUnicodeObject *po; |
| 1803 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1804 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1806 | Py_BEGIN_ALLOW_THREADS |
| 1807 | /* PyUnicode_AS_UNICODE result OK without |
| 1808 | thread lock as it is a simple dereference. */ |
| 1809 | res = wstatfunc(wpath, &st); |
| 1810 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1811 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1812 | if (res != 0) |
| 1813 | return win32_error_unicode("stat", wpath); |
| 1814 | return _pystat_fromstructstat(&st); |
| 1815 | } |
| 1816 | /* Drop the argument parsing error as narrow strings |
| 1817 | are also valid. */ |
| 1818 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1819 | #endif |
| 1820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1821 | if (!PyArg_ParseTuple(args, format, |
| 1822 | PyUnicode_FSConverter, &opath)) |
| 1823 | return NULL; |
| 1824 | path = PyBytes_AsString(opath); |
| 1825 | Py_BEGIN_ALLOW_THREADS |
| 1826 | res = (*statfunc)(path, &st); |
| 1827 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1828 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1829 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1830 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1831 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1832 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1833 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1834 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1835 | } |
| 1836 | else |
| 1837 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1838 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1839 | Py_DECREF(opath); |
| 1840 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1843 | /* POSIX methods */ |
| 1844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1845 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1846 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1847 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1848 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1849 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1850 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1851 | 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] | 1852 | |
| 1853 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1854 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1855 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1856 | PyObject *opath; |
| 1857 | char *path; |
| 1858 | int mode; |
| 1859 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1860 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1861 | DWORD attr; |
| 1862 | PyUnicodeObject *po; |
| 1863 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1864 | Py_BEGIN_ALLOW_THREADS |
| 1865 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1866 | it is a simple dereference. */ |
| 1867 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1868 | Py_END_ALLOW_THREADS |
| 1869 | goto finish; |
| 1870 | } |
| 1871 | /* Drop the argument parsing error as narrow strings |
| 1872 | are also valid. */ |
| 1873 | PyErr_Clear(); |
| 1874 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1875 | PyUnicode_FSConverter, &opath, &mode)) |
| 1876 | return NULL; |
| 1877 | path = PyBytes_AsString(opath); |
| 1878 | Py_BEGIN_ALLOW_THREADS |
| 1879 | attr = GetFileAttributesA(path); |
| 1880 | Py_END_ALLOW_THREADS |
| 1881 | Py_DECREF(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1882 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1883 | if (attr == 0xFFFFFFFF) |
| 1884 | /* File does not exist, or cannot read attributes */ |
| 1885 | return PyBool_FromLong(0); |
| 1886 | /* Access is possible if either write access wasn't requested, or |
| 1887 | the file isn't read-only, or if it's a directory, as there are |
| 1888 | no read-only directories on Windows. */ |
| 1889 | return PyBool_FromLong(!(mode & 2) |
| 1890 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1891 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1892 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1893 | int res; |
| 1894 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1895 | PyUnicode_FSConverter, &opath, &mode)) |
| 1896 | return NULL; |
| 1897 | path = PyBytes_AsString(opath); |
| 1898 | Py_BEGIN_ALLOW_THREADS |
| 1899 | res = access(path, mode); |
| 1900 | Py_END_ALLOW_THREADS |
| 1901 | Py_DECREF(opath); |
| 1902 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1903 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1906 | #ifndef F_OK |
| 1907 | #define F_OK 0 |
| 1908 | #endif |
| 1909 | #ifndef R_OK |
| 1910 | #define R_OK 4 |
| 1911 | #endif |
| 1912 | #ifndef W_OK |
| 1913 | #define W_OK 2 |
| 1914 | #endif |
| 1915 | #ifndef X_OK |
| 1916 | #define X_OK 1 |
| 1917 | #endif |
| 1918 | |
| 1919 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1920 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1921 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1922 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1923 | |
| 1924 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1925 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1926 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1927 | int id; |
| 1928 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1929 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1930 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1931 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1932 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1933 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1934 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1935 | if (id == 0) { |
| 1936 | ret = ttyname(); |
| 1937 | } |
| 1938 | else { |
| 1939 | ret = NULL; |
| 1940 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1941 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1942 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1943 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1944 | if (ret == NULL) |
| 1945 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1946 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1947 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1948 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1949 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1950 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1951 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1952 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1953 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1954 | |
| 1955 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1956 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1957 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1958 | char *ret; |
| 1959 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1960 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1961 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1962 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1963 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1964 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1965 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1966 | if (ret == NULL) |
| 1967 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1968 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1969 | } |
| 1970 | #endif |
| 1971 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1972 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1973 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1974 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1975 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1976 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1977 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1978 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1979 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1980 | 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] | 1981 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1982 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1983 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1984 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1985 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1986 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1987 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1990 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1991 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1992 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1993 | 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] | 1994 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1995 | |
| 1996 | static PyObject * |
| 1997 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1998 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1999 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2000 | } |
| 2001 | #endif /* HAVE_FCHDIR */ |
| 2002 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2004 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2005 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2006 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2007 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2008 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2009 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2010 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2011 | PyObject *opath = NULL; |
| 2012 | char *path = NULL; |
| 2013 | int i; |
| 2014 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2015 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2016 | DWORD attr; |
| 2017 | PyUnicodeObject *po; |
| 2018 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 2019 | Py_BEGIN_ALLOW_THREADS |
| 2020 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 2021 | if (attr != 0xFFFFFFFF) { |
| 2022 | if (i & _S_IWRITE) |
| 2023 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2024 | else |
| 2025 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2026 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 2027 | } |
| 2028 | else |
| 2029 | res = 0; |
| 2030 | Py_END_ALLOW_THREADS |
| 2031 | if (!res) |
| 2032 | return win32_error_unicode("chmod", |
| 2033 | PyUnicode_AS_UNICODE(po)); |
| 2034 | Py_INCREF(Py_None); |
| 2035 | return Py_None; |
| 2036 | } |
| 2037 | /* Drop the argument parsing error as narrow strings |
| 2038 | are also valid. */ |
| 2039 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2040 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2041 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2042 | &opath, &i)) |
| 2043 | return NULL; |
| 2044 | path = PyBytes_AsString(opath); |
| 2045 | Py_BEGIN_ALLOW_THREADS |
| 2046 | attr = GetFileAttributesA(path); |
| 2047 | if (attr != 0xFFFFFFFF) { |
| 2048 | if (i & _S_IWRITE) |
| 2049 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2050 | else |
| 2051 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2052 | res = SetFileAttributesA(path, attr); |
| 2053 | } |
| 2054 | else |
| 2055 | res = 0; |
| 2056 | Py_END_ALLOW_THREADS |
| 2057 | if (!res) { |
| 2058 | win32_error("chmod", path); |
| 2059 | Py_DECREF(opath); |
| 2060 | return NULL; |
| 2061 | } |
| 2062 | Py_DECREF(opath); |
| 2063 | Py_INCREF(Py_None); |
| 2064 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2065 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2066 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2067 | &opath, &i)) |
| 2068 | return NULL; |
| 2069 | path = PyBytes_AsString(opath); |
| 2070 | Py_BEGIN_ALLOW_THREADS |
| 2071 | res = chmod(path, i); |
| 2072 | Py_END_ALLOW_THREADS |
| 2073 | if (res < 0) |
| 2074 | return posix_error_with_allocated_filename(opath); |
| 2075 | Py_DECREF(opath); |
| 2076 | Py_INCREF(Py_None); |
| 2077 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2078 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2081 | #ifdef HAVE_FCHMOD |
| 2082 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2083 | "fchmod(fd, mode)\n\n\ |
| 2084 | Change the access permissions of the file given by file\n\ |
| 2085 | descriptor fd."); |
| 2086 | |
| 2087 | static PyObject * |
| 2088 | posix_fchmod(PyObject *self, PyObject *args) |
| 2089 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2090 | int fd, mode, res; |
| 2091 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2092 | return NULL; |
| 2093 | Py_BEGIN_ALLOW_THREADS |
| 2094 | res = fchmod(fd, mode); |
| 2095 | Py_END_ALLOW_THREADS |
| 2096 | if (res < 0) |
| 2097 | return posix_error(); |
| 2098 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2099 | } |
| 2100 | #endif /* HAVE_FCHMOD */ |
| 2101 | |
| 2102 | #ifdef HAVE_LCHMOD |
| 2103 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2104 | "lchmod(path, mode)\n\n\ |
| 2105 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2106 | affects the link itself rather than the target."); |
| 2107 | |
| 2108 | static PyObject * |
| 2109 | posix_lchmod(PyObject *self, PyObject *args) |
| 2110 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2111 | PyObject *opath; |
| 2112 | char *path; |
| 2113 | int i; |
| 2114 | int res; |
| 2115 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2116 | &opath, &i)) |
| 2117 | return NULL; |
| 2118 | path = PyBytes_AsString(opath); |
| 2119 | Py_BEGIN_ALLOW_THREADS |
| 2120 | res = lchmod(path, i); |
| 2121 | Py_END_ALLOW_THREADS |
| 2122 | if (res < 0) |
| 2123 | return posix_error_with_allocated_filename(opath); |
| 2124 | Py_DECREF(opath); |
| 2125 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2126 | } |
| 2127 | #endif /* HAVE_LCHMOD */ |
| 2128 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2129 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2130 | #ifdef HAVE_CHFLAGS |
| 2131 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2132 | "chflags(path, flags)\n\n\ |
| 2133 | Set file flags."); |
| 2134 | |
| 2135 | static PyObject * |
| 2136 | posix_chflags(PyObject *self, PyObject *args) |
| 2137 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2138 | PyObject *opath; |
| 2139 | char *path; |
| 2140 | unsigned long flags; |
| 2141 | int res; |
| 2142 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2143 | PyUnicode_FSConverter, &opath, &flags)) |
| 2144 | return NULL; |
| 2145 | path = PyBytes_AsString(opath); |
| 2146 | Py_BEGIN_ALLOW_THREADS |
| 2147 | res = chflags(path, flags); |
| 2148 | Py_END_ALLOW_THREADS |
| 2149 | if (res < 0) |
| 2150 | return posix_error_with_allocated_filename(opath); |
| 2151 | Py_DECREF(opath); |
| 2152 | Py_INCREF(Py_None); |
| 2153 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2154 | } |
| 2155 | #endif /* HAVE_CHFLAGS */ |
| 2156 | |
| 2157 | #ifdef HAVE_LCHFLAGS |
| 2158 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2159 | "lchflags(path, flags)\n\n\ |
| 2160 | Set file flags.\n\ |
| 2161 | This function will not follow symbolic links."); |
| 2162 | |
| 2163 | static PyObject * |
| 2164 | posix_lchflags(PyObject *self, PyObject *args) |
| 2165 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2166 | PyObject *opath; |
| 2167 | char *path; |
| 2168 | unsigned long flags; |
| 2169 | int res; |
| 2170 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2171 | PyUnicode_FSConverter, &opath, &flags)) |
| 2172 | return NULL; |
| 2173 | path = PyBytes_AsString(opath); |
| 2174 | Py_BEGIN_ALLOW_THREADS |
| 2175 | res = lchflags(path, flags); |
| 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 | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2182 | } |
| 2183 | #endif /* HAVE_LCHFLAGS */ |
| 2184 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2185 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2186 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2187 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2188 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2189 | |
| 2190 | static PyObject * |
| 2191 | posix_chroot(PyObject *self, PyObject *args) |
| 2192 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2193 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2194 | } |
| 2195 | #endif |
| 2196 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2197 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2198 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2199 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2200 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2201 | |
| 2202 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2203 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2204 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2205 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2206 | } |
| 2207 | #endif /* HAVE_FSYNC */ |
| 2208 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2209 | #ifdef HAVE_SYNC |
| 2210 | PyDoc_STRVAR(posix_sync__doc__, |
| 2211 | "sync()\n\n\ |
| 2212 | Force write of everything to disk."); |
| 2213 | |
| 2214 | static PyObject * |
| 2215 | posix_sync(PyObject *self, PyObject *noargs) |
| 2216 | { |
| 2217 | Py_BEGIN_ALLOW_THREADS |
| 2218 | sync(); |
| 2219 | Py_END_ALLOW_THREADS |
| 2220 | Py_RETURN_NONE; |
| 2221 | } |
| 2222 | #endif |
| 2223 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2224 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2225 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2226 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2227 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2228 | #endif |
| 2229 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2230 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2231 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2232 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2233 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2234 | |
| 2235 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2236 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2237 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2238 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2239 | } |
| 2240 | #endif /* HAVE_FDATASYNC */ |
| 2241 | |
| 2242 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2243 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2244 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2245 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2246 | 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] | 2247 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2248 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2249 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2250 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2251 | PyObject *opath; |
| 2252 | char *path; |
| 2253 | long uid, gid; |
| 2254 | int res; |
| 2255 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2256 | PyUnicode_FSConverter, &opath, |
| 2257 | &uid, &gid)) |
| 2258 | return NULL; |
| 2259 | path = PyBytes_AsString(opath); |
| 2260 | Py_BEGIN_ALLOW_THREADS |
| 2261 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2262 | Py_END_ALLOW_THREADS |
| 2263 | if (res < 0) |
| 2264 | return posix_error_with_allocated_filename(opath); |
| 2265 | Py_DECREF(opath); |
| 2266 | Py_INCREF(Py_None); |
| 2267 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2268 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2269 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2270 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2271 | #ifdef HAVE_FCHOWN |
| 2272 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2273 | "fchown(fd, uid, gid)\n\n\ |
| 2274 | Change the owner and group id of the file given by file descriptor\n\ |
| 2275 | fd to the numeric uid and gid."); |
| 2276 | |
| 2277 | static PyObject * |
| 2278 | posix_fchown(PyObject *self, PyObject *args) |
| 2279 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2280 | int fd; |
| 2281 | long uid, gid; |
| 2282 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2283 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2284 | return NULL; |
| 2285 | Py_BEGIN_ALLOW_THREADS |
| 2286 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2287 | Py_END_ALLOW_THREADS |
| 2288 | if (res < 0) |
| 2289 | return posix_error(); |
| 2290 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2291 | } |
| 2292 | #endif /* HAVE_FCHOWN */ |
| 2293 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2294 | #ifdef HAVE_LCHOWN |
| 2295 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2296 | "lchown(path, uid, gid)\n\n\ |
| 2297 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2298 | This function will not follow symbolic links."); |
| 2299 | |
| 2300 | static PyObject * |
| 2301 | posix_lchown(PyObject *self, PyObject *args) |
| 2302 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2303 | PyObject *opath; |
| 2304 | char *path; |
| 2305 | long uid, gid; |
| 2306 | int res; |
| 2307 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2308 | PyUnicode_FSConverter, &opath, |
| 2309 | &uid, &gid)) |
| 2310 | return NULL; |
| 2311 | path = PyBytes_AsString(opath); |
| 2312 | Py_BEGIN_ALLOW_THREADS |
| 2313 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2314 | Py_END_ALLOW_THREADS |
| 2315 | if (res < 0) |
| 2316 | return posix_error_with_allocated_filename(opath); |
| 2317 | Py_DECREF(opath); |
| 2318 | Py_INCREF(Py_None); |
| 2319 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2320 | } |
| 2321 | #endif /* HAVE_LCHOWN */ |
| 2322 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2323 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2324 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2325 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2326 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2327 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2328 | char buf[1026]; |
| 2329 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2330 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2331 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2332 | if (!use_bytes) { |
| 2333 | wchar_t wbuf[1026]; |
| 2334 | wchar_t *wbuf2 = wbuf; |
| 2335 | PyObject *resobj; |
| 2336 | DWORD len; |
| 2337 | Py_BEGIN_ALLOW_THREADS |
| 2338 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2339 | /* If the buffer is large enough, len does not include the |
| 2340 | terminating \0. If the buffer is too small, len includes |
| 2341 | the space needed for the terminator. */ |
| 2342 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2343 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2344 | if (wbuf2) |
| 2345 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2346 | } |
| 2347 | Py_END_ALLOW_THREADS |
| 2348 | if (!wbuf2) { |
| 2349 | PyErr_NoMemory(); |
| 2350 | return NULL; |
| 2351 | } |
| 2352 | if (!len) { |
| 2353 | if (wbuf2 != wbuf) free(wbuf2); |
| 2354 | return win32_error("getcwdu", NULL); |
| 2355 | } |
| 2356 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2357 | if (wbuf2 != wbuf) free(wbuf2); |
| 2358 | return resobj; |
| 2359 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2360 | #endif |
| 2361 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2362 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2363 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2364 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2365 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2366 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2367 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2368 | Py_END_ALLOW_THREADS |
| 2369 | if (res == NULL) |
| 2370 | return posix_error(); |
| 2371 | if (use_bytes) |
| 2372 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2373 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2374 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2375 | |
| 2376 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2377 | "getcwd() -> path\n\n\ |
| 2378 | Return a unicode string representing the current working directory."); |
| 2379 | |
| 2380 | static PyObject * |
| 2381 | posix_getcwd_unicode(PyObject *self) |
| 2382 | { |
| 2383 | return posix_getcwd(0); |
| 2384 | } |
| 2385 | |
| 2386 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2387 | "getcwdb() -> path\n\n\ |
| 2388 | Return a bytes string representing the current working directory."); |
| 2389 | |
| 2390 | static PyObject * |
| 2391 | posix_getcwd_bytes(PyObject *self) |
| 2392 | { |
| 2393 | return posix_getcwd(1); |
| 2394 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2395 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2396 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2397 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2398 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2399 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2400 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2401 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2402 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2403 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2404 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2405 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2406 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2407 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2408 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2409 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2410 | #ifdef MS_WINDOWS |
| 2411 | PyDoc_STRVAR(win32_link__doc__, |
| 2412 | "link(src, dst)\n\n\ |
| 2413 | Create a hard link to a file."); |
| 2414 | |
| 2415 | static PyObject * |
| 2416 | win32_link(PyObject *self, PyObject *args) |
| 2417 | { |
| 2418 | PyObject *osrc, *odst; |
| 2419 | char *src, *dst; |
| 2420 | BOOL rslt; |
| 2421 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2422 | PyUnicodeObject *usrc, *udst; |
| 2423 | if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) { |
| 2424 | Py_BEGIN_ALLOW_THREADS |
| 2425 | rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst), |
| 2426 | PyUnicode_AS_UNICODE(usrc), NULL); |
| 2427 | Py_END_ALLOW_THREADS |
| 2428 | |
| 2429 | if (rslt == 0) |
| 2430 | return win32_error("link", NULL); |
| 2431 | |
| 2432 | Py_RETURN_NONE; |
| 2433 | } |
| 2434 | |
| 2435 | /* Narrow strings also valid. */ |
| 2436 | PyErr_Clear(); |
| 2437 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2438 | if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc, |
| 2439 | PyUnicode_FSConverter, &odst)) |
| 2440 | return NULL; |
| 2441 | |
| 2442 | src = PyBytes_AsString(osrc); |
| 2443 | dst = PyBytes_AsString(odst); |
| 2444 | |
| 2445 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2446 | rslt = CreateHardLinkA(dst, src, NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2447 | Py_END_ALLOW_THREADS |
| 2448 | |
Stefan Krah | 30b341f | 2010-11-27 11:44:18 +0000 | [diff] [blame] | 2449 | Py_DECREF(osrc); |
| 2450 | Py_DECREF(odst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2451 | if (rslt == 0) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2452 | return win32_error("link", NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2453 | |
| 2454 | Py_RETURN_NONE; |
| 2455 | } |
| 2456 | #endif /* MS_WINDOWS */ |
| 2457 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2458 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2459 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2460 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2461 | Return a list containing the names of the entries in the directory.\n\ |
| 2462 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2463 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2464 | \n\ |
| 2465 | 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] | 2466 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2467 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2468 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2469 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2470 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2471 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2472 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2473 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2474 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2475 | PyObject *d, *v; |
| 2476 | HANDLE hFindFile; |
| 2477 | BOOL result; |
| 2478 | WIN32_FIND_DATA FileData; |
| 2479 | PyObject *opath; |
| 2480 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2481 | char *bufptr = namebuf; |
| 2482 | 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] | 2483 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2484 | PyObject *po = NULL; |
| 2485 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2486 | WIN32_FIND_DATAW wFileData; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2487 | Py_UNICODE *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2488 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2489 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2490 | po_wchars = L"."; |
| 2491 | len = 1; |
| 2492 | } else { |
| 2493 | po_wchars = PyUnicode_AS_UNICODE(po); |
| 2494 | len = PyUnicode_GET_SIZE(po); |
| 2495 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2496 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2497 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2498 | if (!wnamebuf) { |
| 2499 | PyErr_NoMemory(); |
| 2500 | return NULL; |
| 2501 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2502 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2503 | if (len > 0) { |
| 2504 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2505 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2506 | wnamebuf[len++] = L'\\'; |
| 2507 | wcscpy(wnamebuf + len, L"*.*"); |
| 2508 | } |
| 2509 | if ((d = PyList_New(0)) == NULL) { |
| 2510 | free(wnamebuf); |
| 2511 | return NULL; |
| 2512 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2513 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2514 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2515 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2516 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2517 | int error = GetLastError(); |
| 2518 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2519 | free(wnamebuf); |
| 2520 | return d; |
| 2521 | } |
| 2522 | Py_DECREF(d); |
| 2523 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2524 | free(wnamebuf); |
| 2525 | return NULL; |
| 2526 | } |
| 2527 | do { |
| 2528 | /* Skip over . and .. */ |
| 2529 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2530 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2531 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2532 | if (v == NULL) { |
| 2533 | Py_DECREF(d); |
| 2534 | d = NULL; |
| 2535 | break; |
| 2536 | } |
| 2537 | if (PyList_Append(d, v) != 0) { |
| 2538 | Py_DECREF(v); |
| 2539 | Py_DECREF(d); |
| 2540 | d = NULL; |
| 2541 | break; |
| 2542 | } |
| 2543 | Py_DECREF(v); |
| 2544 | } |
| 2545 | Py_BEGIN_ALLOW_THREADS |
| 2546 | result = FindNextFileW(hFindFile, &wFileData); |
| 2547 | Py_END_ALLOW_THREADS |
| 2548 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2549 | it got to the end of the directory. */ |
| 2550 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2551 | Py_DECREF(d); |
| 2552 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2553 | FindClose(hFindFile); |
| 2554 | free(wnamebuf); |
| 2555 | return NULL; |
| 2556 | } |
| 2557 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2558 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2559 | if (FindClose(hFindFile) == FALSE) { |
| 2560 | Py_DECREF(d); |
| 2561 | win32_error_unicode("FindClose", wnamebuf); |
| 2562 | free(wnamebuf); |
| 2563 | return NULL; |
| 2564 | } |
| 2565 | free(wnamebuf); |
| 2566 | return d; |
| 2567 | } |
| 2568 | /* Drop the argument parsing error as narrow strings |
| 2569 | are also valid. */ |
| 2570 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2571 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2572 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2573 | PyUnicode_FSConverter, &opath)) |
| 2574 | return NULL; |
| 2575 | if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) { |
| 2576 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2577 | Py_DECREF(opath); |
| 2578 | return NULL; |
| 2579 | } |
| 2580 | strcpy(namebuf, PyBytes_AsString(opath)); |
| 2581 | len = PyObject_Size(opath); |
Stefan Krah | 2a7feee | 2010-11-27 22:06:49 +0000 | [diff] [blame] | 2582 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2583 | if (len > 0) { |
| 2584 | char ch = namebuf[len-1]; |
| 2585 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2586 | namebuf[len++] = '/'; |
| 2587 | strcpy(namebuf + len, "*.*"); |
| 2588 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2589 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2590 | if ((d = PyList_New(0)) == NULL) |
| 2591 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2592 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2593 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2594 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2595 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2596 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2597 | int error = GetLastError(); |
| 2598 | if (error == ERROR_FILE_NOT_FOUND) |
| 2599 | return d; |
| 2600 | Py_DECREF(d); |
| 2601 | return win32_error("FindFirstFile", namebuf); |
| 2602 | } |
| 2603 | do { |
| 2604 | /* Skip over . and .. */ |
| 2605 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2606 | strcmp(FileData.cFileName, "..") != 0) { |
| 2607 | v = PyBytes_FromString(FileData.cFileName); |
| 2608 | if (v == NULL) { |
| 2609 | Py_DECREF(d); |
| 2610 | d = NULL; |
| 2611 | break; |
| 2612 | } |
| 2613 | if (PyList_Append(d, v) != 0) { |
| 2614 | Py_DECREF(v); |
| 2615 | Py_DECREF(d); |
| 2616 | d = NULL; |
| 2617 | break; |
| 2618 | } |
| 2619 | Py_DECREF(v); |
| 2620 | } |
| 2621 | Py_BEGIN_ALLOW_THREADS |
| 2622 | result = FindNextFile(hFindFile, &FileData); |
| 2623 | Py_END_ALLOW_THREADS |
| 2624 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2625 | it got to the end of the directory. */ |
| 2626 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2627 | Py_DECREF(d); |
| 2628 | win32_error("FindNextFile", namebuf); |
| 2629 | FindClose(hFindFile); |
| 2630 | return NULL; |
| 2631 | } |
| 2632 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2633 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2634 | if (FindClose(hFindFile) == FALSE) { |
| 2635 | Py_DECREF(d); |
| 2636 | return win32_error("FindClose", namebuf); |
| 2637 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2638 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2639 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2640 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2641 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2642 | |
| 2643 | #ifndef MAX_PATH |
| 2644 | #define MAX_PATH CCHMAXPATH |
| 2645 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2646 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2647 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2648 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2649 | PyObject *d, *v; |
| 2650 | char namebuf[MAX_PATH+5]; |
| 2651 | HDIR hdir = 1; |
| 2652 | ULONG srchcnt = 1; |
| 2653 | FILEFINDBUF3 ep; |
| 2654 | APIRET rc; |
| 2655 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2656 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2657 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2658 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2659 | name = PyBytes_AsString(oname); |
| 2660 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2661 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2662 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2663 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2664 | return NULL; |
| 2665 | } |
| 2666 | strcpy(namebuf, name); |
| 2667 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2668 | if (*pt == ALTSEP) |
| 2669 | *pt = SEP; |
| 2670 | if (namebuf[len-1] != SEP) |
| 2671 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2672 | strcpy(namebuf + len, "*.*"); |
| 2673 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2674 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2675 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2676 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2677 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2678 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2679 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2680 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2681 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2682 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2683 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2684 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2685 | |
| 2686 | if (rc != NO_ERROR) { |
| 2687 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2688 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2691 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2692 | do { |
| 2693 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2694 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2695 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2696 | |
| 2697 | strcpy(namebuf, ep.achName); |
| 2698 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2699 | /* Leave Case of Name Alone -- In Native Form */ |
| 2700 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2701 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2702 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2703 | if (v == NULL) { |
| 2704 | Py_DECREF(d); |
| 2705 | d = NULL; |
| 2706 | break; |
| 2707 | } |
| 2708 | if (PyList_Append(d, v) != 0) { |
| 2709 | Py_DECREF(v); |
| 2710 | Py_DECREF(d); |
| 2711 | d = NULL; |
| 2712 | break; |
| 2713 | } |
| 2714 | Py_DECREF(v); |
| 2715 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2716 | } |
| 2717 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2718 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2719 | return d; |
| 2720 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2721 | PyObject *oname; |
| 2722 | char *name; |
| 2723 | PyObject *d, *v; |
| 2724 | DIR *dirp; |
| 2725 | struct dirent *ep; |
| 2726 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2727 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2728 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2729 | /* v is never read, so it does not need to be initialized yet. */ |
| 2730 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2731 | arg_is_unicode = 0; |
| 2732 | PyErr_Clear(); |
| 2733 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2734 | oname = NULL; |
| 2735 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2736 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2737 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2738 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2739 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2740 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2741 | Py_BEGIN_ALLOW_THREADS |
| 2742 | dirp = opendir(name); |
| 2743 | Py_END_ALLOW_THREADS |
| 2744 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2745 | return posix_error_with_allocated_filename(oname); |
| 2746 | } |
| 2747 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2748 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2749 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2750 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2751 | Py_DECREF(oname); |
| 2752 | return NULL; |
| 2753 | } |
| 2754 | for (;;) { |
| 2755 | errno = 0; |
| 2756 | Py_BEGIN_ALLOW_THREADS |
| 2757 | ep = readdir(dirp); |
| 2758 | Py_END_ALLOW_THREADS |
| 2759 | if (ep == NULL) { |
| 2760 | if (errno == 0) { |
| 2761 | break; |
| 2762 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2763 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2764 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2765 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2766 | Py_DECREF(d); |
| 2767 | return posix_error_with_allocated_filename(oname); |
| 2768 | } |
| 2769 | } |
| 2770 | if (ep->d_name[0] == '.' && |
| 2771 | (NAMLEN(ep) == 1 || |
| 2772 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2773 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2774 | if (arg_is_unicode) |
| 2775 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2776 | else |
| 2777 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2778 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2779 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2780 | break; |
| 2781 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2782 | if (PyList_Append(d, v) != 0) { |
| 2783 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2784 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2785 | break; |
| 2786 | } |
| 2787 | Py_DECREF(v); |
| 2788 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2789 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2790 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2791 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2792 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2793 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2794 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2795 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2796 | #endif /* which OS */ |
| 2797 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2798 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2799 | #ifdef HAVE_FDOPENDIR |
| 2800 | PyDoc_STRVAR(posix_fdlistdir__doc__, |
| 2801 | "fdlistdir(fd) -> list_of_strings\n\n\ |
| 2802 | Like listdir(), but uses a file descriptor instead.\n\ |
| 2803 | After succesful execution of this function, fd will be closed."); |
| 2804 | |
| 2805 | static PyObject * |
| 2806 | posix_fdlistdir(PyObject *self, PyObject *args) |
| 2807 | { |
| 2808 | PyObject *d, *v; |
| 2809 | DIR *dirp; |
| 2810 | struct dirent *ep; |
| 2811 | int fd; |
| 2812 | |
| 2813 | errno = 0; |
| 2814 | if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd)) |
| 2815 | return NULL; |
| 2816 | Py_BEGIN_ALLOW_THREADS |
| 2817 | dirp = fdopendir(fd); |
| 2818 | Py_END_ALLOW_THREADS |
| 2819 | if (dirp == NULL) { |
| 2820 | close(fd); |
| 2821 | return posix_error(); |
| 2822 | } |
| 2823 | if ((d = PyList_New(0)) == NULL) { |
| 2824 | Py_BEGIN_ALLOW_THREADS |
| 2825 | closedir(dirp); |
| 2826 | Py_END_ALLOW_THREADS |
| 2827 | return NULL; |
| 2828 | } |
| 2829 | for (;;) { |
| 2830 | errno = 0; |
| 2831 | Py_BEGIN_ALLOW_THREADS |
| 2832 | ep = readdir(dirp); |
| 2833 | Py_END_ALLOW_THREADS |
| 2834 | if (ep == NULL) { |
| 2835 | if (errno == 0) { |
| 2836 | break; |
| 2837 | } else { |
| 2838 | Py_BEGIN_ALLOW_THREADS |
| 2839 | closedir(dirp); |
| 2840 | Py_END_ALLOW_THREADS |
| 2841 | Py_DECREF(d); |
| 2842 | return posix_error(); |
| 2843 | } |
| 2844 | } |
| 2845 | if (ep->d_name[0] == '.' && |
| 2846 | (NAMLEN(ep) == 1 || |
| 2847 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2848 | continue; |
| 2849 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2850 | if (v == NULL) { |
| 2851 | Py_CLEAR(d); |
| 2852 | break; |
| 2853 | } |
| 2854 | if (PyList_Append(d, v) != 0) { |
| 2855 | Py_DECREF(v); |
| 2856 | Py_CLEAR(d); |
| 2857 | break; |
| 2858 | } |
| 2859 | Py_DECREF(v); |
| 2860 | } |
| 2861 | Py_BEGIN_ALLOW_THREADS |
| 2862 | closedir(dirp); |
| 2863 | Py_END_ALLOW_THREADS |
| 2864 | |
| 2865 | return d; |
| 2866 | } |
| 2867 | #endif |
| 2868 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2869 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2870 | /* A helper function for abspath on win32 */ |
| 2871 | static PyObject * |
| 2872 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2873 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2874 | PyObject *opath; |
| 2875 | char *path; |
| 2876 | char outbuf[MAX_PATH*2]; |
| 2877 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2878 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2879 | PyUnicodeObject *po; |
| 2880 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2881 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2882 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2883 | Py_UNICODE *wtemp; |
| 2884 | DWORD result; |
| 2885 | PyObject *v; |
| 2886 | result = GetFullPathNameW(wpath, |
| 2887 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2888 | woutbuf, &wtemp); |
| 2889 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2890 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2891 | if (!woutbufp) |
| 2892 | return PyErr_NoMemory(); |
| 2893 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2894 | } |
| 2895 | if (result) |
| 2896 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2897 | else |
| 2898 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2899 | if (woutbufp != woutbuf) |
| 2900 | free(woutbufp); |
| 2901 | return v; |
| 2902 | } |
| 2903 | /* Drop the argument parsing error as narrow strings |
| 2904 | are also valid. */ |
| 2905 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2906 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2907 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2908 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2909 | PyUnicode_FSConverter, &opath)) |
| 2910 | return NULL; |
| 2911 | path = PyBytes_AsString(opath); |
| 2912 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2913 | outbuf, &temp)) { |
| 2914 | win32_error("GetFullPathName", path); |
| 2915 | Py_DECREF(opath); |
| 2916 | return NULL; |
| 2917 | } |
| 2918 | Py_DECREF(opath); |
| 2919 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2920 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2921 | Py_FileSystemDefaultEncoding, NULL); |
| 2922 | } |
| 2923 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2924 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2925 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 2926 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 2927 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2928 | /* A helper function for samepath on windows */ |
| 2929 | static PyObject * |
| 2930 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 2931 | { |
| 2932 | HANDLE hFile; |
| 2933 | int buf_size; |
| 2934 | wchar_t *target_path; |
| 2935 | int result_length; |
| 2936 | PyObject *result; |
| 2937 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2938 | |
Brian Curtin | 94622b0 | 2010-09-24 00:03:39 +0000 | [diff] [blame] | 2939 | if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) { |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2940 | return NULL; |
| 2941 | } |
| 2942 | |
| 2943 | if(!check_GetFinalPathNameByHandle()) { |
| 2944 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 2945 | NotImplementedError. */ |
| 2946 | return PyErr_Format(PyExc_NotImplementedError, |
| 2947 | "GetFinalPathNameByHandle not available on this platform"); |
| 2948 | } |
| 2949 | |
| 2950 | hFile = CreateFileW( |
| 2951 | path, |
| 2952 | 0, /* desired access */ |
| 2953 | 0, /* share mode */ |
| 2954 | NULL, /* security attributes */ |
| 2955 | OPEN_EXISTING, |
| 2956 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 2957 | FILE_FLAG_BACKUP_SEMANTICS, |
| 2958 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2959 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2960 | if(hFile == INVALID_HANDLE_VALUE) { |
| 2961 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 2962 | return PyErr_Format(PyExc_RuntimeError, |
| 2963 | "Could not get a handle to file."); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | /* We have a good handle to the target, use it to determine the |
| 2967 | target path name. */ |
| 2968 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 2969 | |
| 2970 | if(!buf_size) |
| 2971 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2972 | |
| 2973 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 2974 | if(!target_path) |
| 2975 | return PyErr_NoMemory(); |
| 2976 | |
| 2977 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 2978 | buf_size, VOLUME_NAME_DOS); |
| 2979 | if(!result_length) |
| 2980 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
| 2981 | |
| 2982 | if(!CloseHandle(hFile)) |
| 2983 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2984 | |
| 2985 | target_path[result_length] = 0; |
| 2986 | result = PyUnicode_FromUnicode(target_path, result_length); |
| 2987 | free(target_path); |
| 2988 | return result; |
| 2989 | |
| 2990 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 2991 | |
| 2992 | static PyObject * |
| 2993 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 2994 | { |
| 2995 | HANDLE hFile; |
| 2996 | BY_HANDLE_FILE_INFORMATION info; |
| 2997 | int fd; |
| 2998 | |
| 2999 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3000 | return NULL; |
| 3001 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3002 | if (!_PyVerify_fd(fd)) |
| 3003 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3004 | |
| 3005 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3006 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3007 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3008 | |
| 3009 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3010 | return win32_error("_getfileinformation", NULL); |
| 3011 | |
| 3012 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3013 | info.nFileIndexHigh, |
| 3014 | info.nFileIndexLow); |
| 3015 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3016 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3017 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3018 | "Return true if the pathname refers to an existing directory."); |
| 3019 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3020 | static PyObject * |
| 3021 | posix__isdir(PyObject *self, PyObject *args) |
| 3022 | { |
| 3023 | PyObject *opath; |
| 3024 | char *path; |
| 3025 | PyUnicodeObject *po; |
| 3026 | DWORD attributes; |
| 3027 | |
| 3028 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
| 3029 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 3030 | |
| 3031 | attributes = GetFileAttributesW(wpath); |
| 3032 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3033 | Py_RETURN_FALSE; |
| 3034 | goto check; |
| 3035 | } |
| 3036 | /* Drop the argument parsing error as narrow strings |
| 3037 | are also valid. */ |
| 3038 | PyErr_Clear(); |
| 3039 | |
| 3040 | if (!PyArg_ParseTuple(args, "O&:_isdir", |
| 3041 | PyUnicode_FSConverter, &opath)) |
| 3042 | return NULL; |
| 3043 | |
| 3044 | path = PyBytes_AsString(opath); |
| 3045 | attributes = GetFileAttributesA(path); |
| 3046 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3047 | Py_RETURN_FALSE; |
| 3048 | |
| 3049 | check: |
| 3050 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3051 | Py_RETURN_TRUE; |
| 3052 | else |
| 3053 | Py_RETURN_FALSE; |
| 3054 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3055 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3056 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3057 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3058 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3059 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3060 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3061 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3062 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3064 | int res; |
| 3065 | PyObject *opath; |
| 3066 | char *path; |
| 3067 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3068 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3069 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3070 | PyUnicodeObject *po; |
| 3071 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 3072 | Py_BEGIN_ALLOW_THREADS |
| 3073 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 3074 | it is a simple dereference. */ |
| 3075 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 3076 | Py_END_ALLOW_THREADS |
| 3077 | if (!res) |
| 3078 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 3079 | Py_INCREF(Py_None); |
| 3080 | return Py_None; |
| 3081 | } |
| 3082 | /* Drop the argument parsing error as narrow strings |
| 3083 | are also valid. */ |
| 3084 | PyErr_Clear(); |
| 3085 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3086 | PyUnicode_FSConverter, &opath, &mode)) |
| 3087 | return NULL; |
| 3088 | path = PyBytes_AsString(opath); |
| 3089 | Py_BEGIN_ALLOW_THREADS |
| 3090 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 3091 | it is a simple dereference. */ |
| 3092 | res = CreateDirectoryA(path, NULL); |
| 3093 | Py_END_ALLOW_THREADS |
| 3094 | if (!res) { |
| 3095 | win32_error("mkdir", path); |
| 3096 | Py_DECREF(opath); |
| 3097 | return NULL; |
| 3098 | } |
| 3099 | Py_DECREF(opath); |
| 3100 | Py_INCREF(Py_None); |
| 3101 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3102 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3103 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3104 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3105 | PyUnicode_FSConverter, &opath, &mode)) |
| 3106 | return NULL; |
| 3107 | path = PyBytes_AsString(opath); |
| 3108 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3109 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3110 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3111 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3112 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3113 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3114 | Py_END_ALLOW_THREADS |
| 3115 | if (res < 0) |
| 3116 | return posix_error_with_allocated_filename(opath); |
| 3117 | Py_DECREF(opath); |
| 3118 | Py_INCREF(Py_None); |
| 3119 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3120 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3123 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3124 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3125 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3126 | #include <sys/resource.h> |
| 3127 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3128 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3129 | |
| 3130 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3131 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3132 | "nice(inc) -> new_priority\n\n\ |
| 3133 | 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] | 3134 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3135 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3136 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3137 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3138 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3139 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3140 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3141 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3142 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3143 | /* There are two flavours of 'nice': one that returns the new |
| 3144 | priority (as required by almost all standards out there) and the |
| 3145 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3146 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3147 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3148 | If we are of the nice family that returns the new priority, we |
| 3149 | need to clear errno before the call, and check if errno is filled |
| 3150 | before calling posix_error() on a returnvalue of -1, because the |
| 3151 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3152 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3153 | errno = 0; |
| 3154 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3155 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3156 | if (value == 0) |
| 3157 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3158 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3159 | if (value == -1 && errno != 0) |
| 3160 | /* either nice() or getpriority() returned an error */ |
| 3161 | return posix_error(); |
| 3162 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3163 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3164 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3165 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3166 | |
| 3167 | #ifdef HAVE_GETPRIORITY |
| 3168 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3169 | "getpriority(which, who) -> current_priority\n\n\ |
| 3170 | Get program scheduling priority."); |
| 3171 | |
| 3172 | static PyObject * |
| 3173 | posix_getpriority(PyObject *self, PyObject *args) |
| 3174 | { |
| 3175 | int which, who, retval; |
| 3176 | |
| 3177 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3178 | return NULL; |
| 3179 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3180 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3181 | if (errno != 0) |
| 3182 | return posix_error(); |
| 3183 | return PyLong_FromLong((long)retval); |
| 3184 | } |
| 3185 | #endif /* HAVE_GETPRIORITY */ |
| 3186 | |
| 3187 | |
| 3188 | #ifdef HAVE_SETPRIORITY |
| 3189 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3190 | "setpriority(which, who, prio) -> None\n\n\ |
| 3191 | Set program scheduling priority."); |
| 3192 | |
| 3193 | static PyObject * |
| 3194 | posix_setpriority(PyObject *self, PyObject *args) |
| 3195 | { |
| 3196 | int which, who, prio, retval; |
| 3197 | |
| 3198 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3199 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3200 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3201 | if (retval == -1) |
| 3202 | return posix_error(); |
| 3203 | Py_RETURN_NONE; |
| 3204 | } |
| 3205 | #endif /* HAVE_SETPRIORITY */ |
| 3206 | |
| 3207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3208 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3209 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3210 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3211 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3212 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3213 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3214 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3215 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3216 | PyObject *o1, *o2; |
| 3217 | char *p1, *p2; |
| 3218 | BOOL result; |
| 3219 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 3220 | goto error; |
| 3221 | if (!convert_to_unicode(&o1)) |
| 3222 | goto error; |
| 3223 | if (!convert_to_unicode(&o2)) { |
| 3224 | Py_DECREF(o1); |
| 3225 | goto error; |
| 3226 | } |
| 3227 | Py_BEGIN_ALLOW_THREADS |
| 3228 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 3229 | PyUnicode_AsUnicode(o2)); |
| 3230 | Py_END_ALLOW_THREADS |
| 3231 | Py_DECREF(o1); |
| 3232 | Py_DECREF(o2); |
| 3233 | if (!result) |
| 3234 | return win32_error("rename", NULL); |
| 3235 | Py_INCREF(Py_None); |
| 3236 | return Py_None; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3237 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3238 | PyErr_Clear(); |
| 3239 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 3240 | return NULL; |
| 3241 | Py_BEGIN_ALLOW_THREADS |
| 3242 | result = MoveFileA(p1, p2); |
| 3243 | Py_END_ALLOW_THREADS |
| 3244 | if (!result) |
| 3245 | return win32_error("rename", NULL); |
| 3246 | Py_INCREF(Py_None); |
| 3247 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3248 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3249 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3250 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3253 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3254 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3255 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3256 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3257 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3258 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3259 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3260 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3261 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3262 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3263 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3264 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3265 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3268 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3269 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3270 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3271 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3272 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3273 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3274 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3275 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3276 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3277 | 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] | 3278 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3279 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3280 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3283 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3284 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3285 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3286 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3287 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3288 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3289 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3290 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3291 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3292 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3293 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3294 | wchar_t *command; |
| 3295 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3296 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3297 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3298 | Py_BEGIN_ALLOW_THREADS |
| 3299 | sts = _wsystem(command); |
| 3300 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3301 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3302 | PyObject *command_obj; |
| 3303 | char *command; |
| 3304 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3305 | PyUnicode_FSConverter, &command_obj)) |
| 3306 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3307 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3308 | command = PyBytes_AsString(command_obj); |
| 3309 | Py_BEGIN_ALLOW_THREADS |
| 3310 | sts = system(command); |
| 3311 | Py_END_ALLOW_THREADS |
| 3312 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3313 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3314 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3315 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3316 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3317 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3318 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3319 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3320 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3321 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3322 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3323 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3324 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3325 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3326 | int i; |
| 3327 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3328 | return NULL; |
| 3329 | i = (int)umask(i); |
| 3330 | if (i < 0) |
| 3331 | return posix_error(); |
| 3332 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3335 | #ifdef MS_WINDOWS |
| 3336 | |
| 3337 | /* override the default DeleteFileW behavior so that directory |
| 3338 | symlinks can be removed with this function, the same as with |
| 3339 | Unix symlinks */ |
| 3340 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3341 | { |
| 3342 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3343 | WIN32_FIND_DATAW find_data; |
| 3344 | HANDLE find_data_handle; |
| 3345 | int is_directory = 0; |
| 3346 | int is_link = 0; |
| 3347 | |
| 3348 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3349 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3350 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3351 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3352 | it is a symlink */ |
| 3353 | if(is_directory && |
| 3354 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3355 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3356 | |
| 3357 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3358 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3359 | FindClose(find_data_handle); |
| 3360 | } |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | if (is_directory && is_link) |
| 3365 | return RemoveDirectoryW(lpFileName); |
| 3366 | |
| 3367 | return DeleteFileW(lpFileName); |
| 3368 | } |
| 3369 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3370 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3371 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3372 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3373 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3374 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3375 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3376 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3377 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3378 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3379 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3380 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3381 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3382 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3383 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3384 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3385 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3386 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3387 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3390 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3391 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3392 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3393 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3394 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3395 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3396 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3397 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3398 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3399 | struct utsname u; |
| 3400 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3402 | Py_BEGIN_ALLOW_THREADS |
| 3403 | res = uname(&u); |
| 3404 | Py_END_ALLOW_THREADS |
| 3405 | if (res < 0) |
| 3406 | return posix_error(); |
| 3407 | return Py_BuildValue("(sssss)", |
| 3408 | u.sysname, |
| 3409 | u.nodename, |
| 3410 | u.release, |
| 3411 | u.version, |
| 3412 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3413 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3414 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3415 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3416 | static int |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3417 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3418 | { |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3419 | time_t intval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3420 | if (PyFloat_Check(t)) { |
| 3421 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3422 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3423 | if (!intobj) |
| 3424 | return -1; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3425 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3426 | intval = PyLong_AsUnsignedLongLongMask(intobj); |
| 3427 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3428 | intval = PyLong_AsLong(intobj); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3429 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3430 | Py_DECREF(intobj); |
| 3431 | if (intval == -1 && PyErr_Occurred()) |
| 3432 | return -1; |
| 3433 | *sec = intval; |
| 3434 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
| 3435 | if (*usec < 0) |
| 3436 | /* If rounding gave us a negative number, |
| 3437 | truncate. */ |
| 3438 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3439 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3440 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3441 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3442 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3443 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3444 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3445 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3446 | if (intval == -1 && PyErr_Occurred()) |
| 3447 | return -1; |
| 3448 | *sec = intval; |
| 3449 | *usec = 0; |
| 3450 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3451 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3452 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3453 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3454 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3455 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3456 | Set the access and modified time of the file to the given values. If the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3457 | second form is used, set the access and modified times to the current time."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3458 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3459 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3460 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3461 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3462 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3463 | PyObject *arg; |
| 3464 | PyUnicodeObject *obwpath; |
| 3465 | wchar_t *wpath = NULL; |
| 3466 | PyObject *oapath; |
| 3467 | char *apath; |
| 3468 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3469 | time_t atimesec, mtimesec; |
| 3470 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3471 | FILETIME atime, mtime; |
| 3472 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3473 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3474 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 3475 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 3476 | Py_BEGIN_ALLOW_THREADS |
| 3477 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3478 | NULL, OPEN_EXISTING, |
| 3479 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3480 | Py_END_ALLOW_THREADS |
| 3481 | if (hFile == INVALID_HANDLE_VALUE) |
| 3482 | return win32_error_unicode("utime", wpath); |
| 3483 | } else |
| 3484 | /* Drop the argument parsing error as narrow strings |
| 3485 | are also valid. */ |
| 3486 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3487 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3488 | if (!wpath) { |
| 3489 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3490 | PyUnicode_FSConverter, &oapath, &arg)) |
| 3491 | return NULL; |
| 3492 | apath = PyBytes_AsString(oapath); |
| 3493 | Py_BEGIN_ALLOW_THREADS |
| 3494 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3495 | NULL, OPEN_EXISTING, |
| 3496 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3497 | Py_END_ALLOW_THREADS |
| 3498 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3499 | win32_error("utime", apath); |
| 3500 | Py_DECREF(oapath); |
| 3501 | return NULL; |
| 3502 | } |
| 3503 | Py_DECREF(oapath); |
| 3504 | } |
| 3505 | |
| 3506 | if (arg == Py_None) { |
| 3507 | SYSTEMTIME now; |
| 3508 | GetSystemTime(&now); |
| 3509 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3510 | !SystemTimeToFileTime(&now, &atime)) { |
| 3511 | win32_error("utime", NULL); |
| 3512 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3513 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3514 | } |
| 3515 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3516 | PyErr_SetString(PyExc_TypeError, |
| 3517 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3518 | goto done; |
| 3519 | } |
| 3520 | else { |
| 3521 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3522 | &atimesec, &ausec) == -1) |
| 3523 | goto done; |
| 3524 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3525 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3526 | &mtimesec, &musec) == -1) |
| 3527 | goto done; |
| 3528 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3529 | } |
| 3530 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3531 | /* Avoid putting the file name into the error here, |
| 3532 | as that may confuse the user into believing that |
| 3533 | something is wrong with the file, when it also |
| 3534 | could be the time stamp that gives a problem. */ |
| 3535 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3536 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3537 | } |
| 3538 | Py_INCREF(Py_None); |
| 3539 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3540 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3541 | CloseHandle(hFile); |
| 3542 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3543 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3544 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3545 | PyObject *opath; |
| 3546 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3547 | time_t atime, mtime; |
| 3548 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3549 | int res; |
| 3550 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3551 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3552 | #if defined(HAVE_UTIMES) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3553 | struct timeval buf[2]; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3554 | #define ATIME buf[0].tv_sec |
| 3555 | #define MTIME buf[1].tv_sec |
| 3556 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 3557 | /* XXX should define struct utimbuf instead, above */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3558 | struct utimbuf buf; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3559 | #define ATIME buf.actime |
| 3560 | #define MTIME buf.modtime |
| 3561 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3562 | #else /* HAVE_UTIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3563 | time_t buf[2]; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3564 | #define ATIME buf[0] |
| 3565 | #define MTIME buf[1] |
| 3566 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3567 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3568 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3569 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3570 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3571 | PyUnicode_FSConverter, &opath, &arg)) |
| 3572 | return NULL; |
| 3573 | path = PyBytes_AsString(opath); |
| 3574 | if (arg == Py_None) { |
| 3575 | /* optional time values not given */ |
| 3576 | Py_BEGIN_ALLOW_THREADS |
| 3577 | res = utime(path, NULL); |
| 3578 | Py_END_ALLOW_THREADS |
| 3579 | } |
| 3580 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3581 | PyErr_SetString(PyExc_TypeError, |
| 3582 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3583 | Py_DECREF(opath); |
| 3584 | return NULL; |
| 3585 | } |
| 3586 | else { |
| 3587 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3588 | &atime, &ausec) == -1) { |
| 3589 | Py_DECREF(opath); |
| 3590 | return NULL; |
| 3591 | } |
| 3592 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3593 | &mtime, &musec) == -1) { |
| 3594 | Py_DECREF(opath); |
| 3595 | return NULL; |
| 3596 | } |
| 3597 | ATIME = atime; |
| 3598 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3599 | #ifdef HAVE_UTIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3600 | buf[0].tv_usec = ausec; |
| 3601 | buf[1].tv_usec = musec; |
| 3602 | Py_BEGIN_ALLOW_THREADS |
| 3603 | res = utimes(path, buf); |
| 3604 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3605 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3606 | Py_BEGIN_ALLOW_THREADS |
| 3607 | res = utime(path, UTIME_ARG); |
| 3608 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 3609 | #endif /* HAVE_UTIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3610 | } |
| 3611 | if (res < 0) { |
| 3612 | return posix_error_with_allocated_filename(opath); |
| 3613 | } |
| 3614 | Py_DECREF(opath); |
| 3615 | Py_INCREF(Py_None); |
| 3616 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3617 | #undef UTIME_ARG |
| 3618 | #undef ATIME |
| 3619 | #undef MTIME |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3620 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3623 | #ifdef HAVE_FUTIMES |
| 3624 | PyDoc_STRVAR(posix_futimes__doc__, |
| 3625 | "futimes(fd, (atime, mtime))\n\ |
| 3626 | futimes(fd, None)\n\n\ |
| 3627 | Set the access and modified time of the file specified by the file\n\ |
| 3628 | descriptor fd to the given values. If the second form is used, set the\n\ |
| 3629 | access and modified times to the current time."); |
| 3630 | |
| 3631 | static PyObject * |
| 3632 | posix_futimes(PyObject *self, PyObject *args) |
| 3633 | { |
| 3634 | int res, fd; |
| 3635 | PyObject* arg; |
| 3636 | struct timeval buf[2]; |
| 3637 | long ausec, musec; |
| 3638 | |
| 3639 | if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg)) |
| 3640 | return NULL; |
| 3641 | |
| 3642 | if (arg == Py_None) { |
| 3643 | /* optional time values not given */ |
| 3644 | Py_BEGIN_ALLOW_THREADS |
| 3645 | res = futimes(fd, NULL); |
| 3646 | Py_END_ALLOW_THREADS |
| 3647 | } |
| 3648 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3649 | PyErr_SetString(PyExc_TypeError, |
| 3650 | "futimes() arg 2 must be a tuple (atime, mtime)"); |
| 3651 | return NULL; |
| 3652 | } |
| 3653 | else { |
| 3654 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3655 | &(buf[0].tv_sec), &ausec) == -1) { |
| 3656 | return NULL; |
| 3657 | } |
| 3658 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3659 | &(buf[1].tv_sec), &musec) == -1) { |
| 3660 | return NULL; |
| 3661 | } |
| 3662 | buf[0].tv_usec = ausec; |
| 3663 | buf[1].tv_usec = musec; |
| 3664 | Py_BEGIN_ALLOW_THREADS |
| 3665 | res = futimes(fd, buf); |
| 3666 | Py_END_ALLOW_THREADS |
| 3667 | } |
| 3668 | if (res < 0) |
| 3669 | return posix_error(); |
| 3670 | Py_RETURN_NONE; |
| 3671 | } |
| 3672 | #endif |
| 3673 | |
| 3674 | #ifdef HAVE_LUTIMES |
| 3675 | PyDoc_STRVAR(posix_lutimes__doc__, |
| 3676 | "lutimes(path, (atime, mtime))\n\ |
| 3677 | lutimes(path, None)\n\n\ |
| 3678 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 3679 | |
| 3680 | static PyObject * |
| 3681 | posix_lutimes(PyObject *self, PyObject *args) |
| 3682 | { |
| 3683 | PyObject *opath, *arg; |
| 3684 | const char *path; |
| 3685 | int res; |
| 3686 | struct timeval buf[2]; |
| 3687 | long ausec, musec; |
| 3688 | |
| 3689 | if (!PyArg_ParseTuple(args, "O&O:lutimes", |
| 3690 | PyUnicode_FSConverter, &opath, &arg)) |
| 3691 | return NULL; |
| 3692 | path = PyBytes_AsString(opath); |
| 3693 | if (arg == Py_None) { |
| 3694 | /* optional time values not given */ |
| 3695 | Py_BEGIN_ALLOW_THREADS |
| 3696 | res = lutimes(path, NULL); |
| 3697 | Py_END_ALLOW_THREADS |
| 3698 | } |
| 3699 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3700 | PyErr_SetString(PyExc_TypeError, |
| 3701 | "lutimes() arg 2 must be a tuple (atime, mtime)"); |
| 3702 | Py_DECREF(opath); |
| 3703 | return NULL; |
| 3704 | } |
| 3705 | else { |
| 3706 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3707 | &(buf[0].tv_sec), &ausec) == -1) { |
| 3708 | Py_DECREF(opath); |
| 3709 | return NULL; |
| 3710 | } |
| 3711 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3712 | &(buf[1].tv_sec), &musec) == -1) { |
| 3713 | Py_DECREF(opath); |
| 3714 | return NULL; |
| 3715 | } |
| 3716 | buf[0].tv_usec = ausec; |
| 3717 | buf[1].tv_usec = musec; |
| 3718 | Py_BEGIN_ALLOW_THREADS |
| 3719 | res = lutimes(path, buf); |
| 3720 | Py_END_ALLOW_THREADS |
| 3721 | } |
| 3722 | Py_DECREF(opath); |
| 3723 | if (res < 0) |
| 3724 | return posix_error(); |
| 3725 | Py_RETURN_NONE; |
| 3726 | } |
| 3727 | #endif |
| 3728 | |
| 3729 | #ifdef HAVE_FUTIMENS |
| 3730 | PyDoc_STRVAR(posix_futimens__doc__, |
| 3731 | "futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\ |
| 3732 | futimens(fd, None, None)\n\n\ |
| 3733 | Updates the timestamps of a file specified by the file descriptor fd, with\n\ |
| 3734 | nanosecond precision.\n\ |
| 3735 | The second form sets atime and mtime to the current time.\n\ |
| 3736 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 3737 | current time.\n\ |
| 3738 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 3739 | |
| 3740 | static PyObject * |
| 3741 | posix_futimens(PyObject *self, PyObject *args) |
| 3742 | { |
| 3743 | int res, fd; |
| 3744 | PyObject *atime, *mtime; |
| 3745 | struct timespec buf[2]; |
| 3746 | |
| 3747 | if (!PyArg_ParseTuple(args, "iOO:futimens", |
| 3748 | &fd, &atime, &mtime)) |
| 3749 | return NULL; |
| 3750 | if (atime == Py_None && mtime == Py_None) { |
| 3751 | /* optional time values not given */ |
| 3752 | Py_BEGIN_ALLOW_THREADS |
| 3753 | res = futimens(fd, NULL); |
| 3754 | Py_END_ALLOW_THREADS |
| 3755 | } |
| 3756 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 3757 | PyErr_SetString(PyExc_TypeError, |
| 3758 | "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)"); |
| 3759 | return NULL; |
| 3760 | } |
| 3761 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 3762 | PyErr_SetString(PyExc_TypeError, |
| 3763 | "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)"); |
| 3764 | return NULL; |
| 3765 | } |
| 3766 | else { |
| 3767 | if (!PyArg_ParseTuple(atime, "ll:futimens", |
| 3768 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 3769 | return NULL; |
| 3770 | } |
| 3771 | if (!PyArg_ParseTuple(mtime, "ll:futimens", |
| 3772 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 3773 | return NULL; |
| 3774 | } |
| 3775 | Py_BEGIN_ALLOW_THREADS |
| 3776 | res = futimens(fd, buf); |
| 3777 | Py_END_ALLOW_THREADS |
| 3778 | } |
| 3779 | if (res < 0) |
| 3780 | return posix_error(); |
| 3781 | Py_RETURN_NONE; |
| 3782 | } |
| 3783 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3784 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3785 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3786 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3787 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3788 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3789 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3790 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3791 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3792 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3793 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3794 | int sts; |
| 3795 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3796 | return NULL; |
| 3797 | _exit(sts); |
| 3798 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3801 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3802 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3803 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3804 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3805 | Py_ssize_t i; |
| 3806 | for (i = 0; i < count; i++) |
| 3807 | PyMem_Free(array[i]); |
| 3808 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3809 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3810 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3811 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3812 | int fsconvert_strdup(PyObject *o, char**out) |
| 3813 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3814 | PyObject *bytes; |
| 3815 | Py_ssize_t size; |
| 3816 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3817 | return 0; |
| 3818 | size = PyBytes_GET_SIZE(bytes); |
| 3819 | *out = PyMem_Malloc(size+1); |
| 3820 | if (!*out) |
| 3821 | return 0; |
| 3822 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3823 | Py_DECREF(bytes); |
| 3824 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3825 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3826 | #endif |
| 3827 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3828 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3829 | static char** |
| 3830 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3831 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3832 | char **envlist; |
| 3833 | Py_ssize_t i, pos, envc; |
| 3834 | PyObject *keys=NULL, *vals=NULL; |
| 3835 | PyObject *key, *val, *key2, *val2; |
| 3836 | char *p, *k, *v; |
| 3837 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3838 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3839 | i = PyMapping_Size(env); |
| 3840 | if (i < 0) |
| 3841 | return NULL; |
| 3842 | envlist = PyMem_NEW(char *, i + 1); |
| 3843 | if (envlist == NULL) { |
| 3844 | PyErr_NoMemory(); |
| 3845 | return NULL; |
| 3846 | } |
| 3847 | envc = 0; |
| 3848 | keys = PyMapping_Keys(env); |
| 3849 | vals = PyMapping_Values(env); |
| 3850 | if (!keys || !vals) |
| 3851 | goto error; |
| 3852 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3853 | PyErr_Format(PyExc_TypeError, |
| 3854 | "env.keys() or env.values() is not a list"); |
| 3855 | goto error; |
| 3856 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3857 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3858 | for (pos = 0; pos < i; pos++) { |
| 3859 | key = PyList_GetItem(keys, pos); |
| 3860 | val = PyList_GetItem(vals, pos); |
| 3861 | if (!key || !val) |
| 3862 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3863 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3864 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3865 | goto error; |
| 3866 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3867 | Py_DECREF(key2); |
| 3868 | goto error; |
| 3869 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3870 | |
| 3871 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3872 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3873 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3874 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3875 | k = PyBytes_AsString(key2); |
| 3876 | v = PyBytes_AsString(val2); |
| 3877 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3878 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3879 | p = PyMem_NEW(char, len); |
| 3880 | if (p == NULL) { |
| 3881 | PyErr_NoMemory(); |
| 3882 | Py_DECREF(key2); |
| 3883 | Py_DECREF(val2); |
| 3884 | goto error; |
| 3885 | } |
| 3886 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3887 | envlist[envc++] = p; |
| 3888 | Py_DECREF(key2); |
| 3889 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3890 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3891 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3892 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3893 | } |
| 3894 | Py_DECREF(vals); |
| 3895 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3896 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3897 | envlist[envc] = 0; |
| 3898 | *envc_ptr = envc; |
| 3899 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3900 | |
| 3901 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3902 | Py_XDECREF(keys); |
| 3903 | Py_XDECREF(vals); |
| 3904 | while (--envc >= 0) |
| 3905 | PyMem_DEL(envlist[envc]); |
| 3906 | PyMem_DEL(envlist); |
| 3907 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3908 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3909 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3910 | static char** |
| 3911 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 3912 | { |
| 3913 | int i; |
| 3914 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 3915 | if (argvlist == NULL) { |
| 3916 | PyErr_NoMemory(); |
| 3917 | return NULL; |
| 3918 | } |
| 3919 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3920 | PyObject* item = PySequence_ITEM(argv, i); |
| 3921 | if (item == NULL) |
| 3922 | goto fail; |
| 3923 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 3924 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3925 | goto fail; |
| 3926 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3927 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3928 | } |
| 3929 | argvlist[*argc] = NULL; |
| 3930 | return argvlist; |
| 3931 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3932 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3933 | free_string_array(argvlist, *argc); |
| 3934 | return NULL; |
| 3935 | } |
| 3936 | #endif |
| 3937 | |
| 3938 | #ifdef HAVE_EXECV |
| 3939 | PyDoc_STRVAR(posix_execv__doc__, |
| 3940 | "execv(path, args)\n\n\ |
| 3941 | Execute an executable path with arguments, replacing current process.\n\ |
| 3942 | \n\ |
| 3943 | path: path of executable file\n\ |
| 3944 | args: tuple or list of strings"); |
| 3945 | |
| 3946 | static PyObject * |
| 3947 | posix_execv(PyObject *self, PyObject *args) |
| 3948 | { |
| 3949 | PyObject *opath; |
| 3950 | char *path; |
| 3951 | PyObject *argv; |
| 3952 | char **argvlist; |
| 3953 | Py_ssize_t argc; |
| 3954 | |
| 3955 | /* execv has two arguments: (path, argv), where |
| 3956 | argv is a list or tuple of strings. */ |
| 3957 | |
| 3958 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3959 | PyUnicode_FSConverter, |
| 3960 | &opath, &argv)) |
| 3961 | return NULL; |
| 3962 | path = PyBytes_AsString(opath); |
| 3963 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 3964 | PyErr_SetString(PyExc_TypeError, |
| 3965 | "execv() arg 2 must be a tuple or list"); |
| 3966 | Py_DECREF(opath); |
| 3967 | return NULL; |
| 3968 | } |
| 3969 | argc = PySequence_Size(argv); |
| 3970 | if (argc < 1) { |
| 3971 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 3972 | Py_DECREF(opath); |
| 3973 | return NULL; |
| 3974 | } |
| 3975 | |
| 3976 | argvlist = parse_arglist(argv, &argc); |
| 3977 | if (argvlist == NULL) { |
| 3978 | Py_DECREF(opath); |
| 3979 | return NULL; |
| 3980 | } |
| 3981 | |
| 3982 | execv(path, argvlist); |
| 3983 | |
| 3984 | /* If we get here it's definitely an error */ |
| 3985 | |
| 3986 | free_string_array(argvlist, argc); |
| 3987 | Py_DECREF(opath); |
| 3988 | return posix_error(); |
| 3989 | } |
| 3990 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3991 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3992 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3993 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3994 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3995 | path: path of executable file\n\ |
| 3996 | args: tuple or list of arguments\n\ |
| 3997 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3998 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3999 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4000 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4001 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4002 | PyObject *opath; |
| 4003 | char *path; |
| 4004 | PyObject *argv, *env; |
| 4005 | char **argvlist; |
| 4006 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4007 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4008 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4009 | /* execve has three arguments: (path, argv, env), where |
| 4010 | argv is a list or tuple of strings and env is a dictionary |
| 4011 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4012 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4013 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 4014 | PyUnicode_FSConverter, |
| 4015 | &opath, &argv, &env)) |
| 4016 | return NULL; |
| 4017 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4018 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4019 | PyErr_SetString(PyExc_TypeError, |
| 4020 | "execve() arg 2 must be a tuple or list"); |
| 4021 | goto fail_0; |
| 4022 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4023 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4024 | if (!PyMapping_Check(env)) { |
| 4025 | PyErr_SetString(PyExc_TypeError, |
| 4026 | "execve() arg 3 must be a mapping object"); |
| 4027 | goto fail_0; |
| 4028 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4029 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4030 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4031 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4032 | goto fail_0; |
| 4033 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4034 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4035 | envlist = parse_envlist(env, &envc); |
| 4036 | if (envlist == NULL) |
| 4037 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4038 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4039 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4040 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4041 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4042 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4043 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4044 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4045 | while (--envc >= 0) |
| 4046 | PyMem_DEL(envlist[envc]); |
| 4047 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4048 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4049 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4050 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4051 | Py_DECREF(opath); |
| 4052 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4053 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4054 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4055 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4056 | #ifdef HAVE_FEXECVE |
| 4057 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4058 | "fexecve(fd, args, env)\n\n\ |
| 4059 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4060 | environment, replacing the current process.\n\ |
| 4061 | \n\ |
| 4062 | fd: file descriptor of executable\n\ |
| 4063 | args: tuple or list of arguments\n\ |
| 4064 | env: dictionary of strings mapping to strings"); |
| 4065 | |
| 4066 | static PyObject * |
| 4067 | posix_fexecve(PyObject *self, PyObject *args) |
| 4068 | { |
| 4069 | int fd; |
| 4070 | PyObject *argv, *env; |
| 4071 | char **argvlist; |
| 4072 | char **envlist; |
| 4073 | Py_ssize_t argc, envc; |
| 4074 | |
| 4075 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4076 | &fd, &argv, &env)) |
| 4077 | return NULL; |
| 4078 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4079 | PyErr_SetString(PyExc_TypeError, |
| 4080 | "fexecve() arg 2 must be a tuple or list"); |
| 4081 | return NULL; |
| 4082 | } |
| 4083 | argc = PySequence_Size(argv); |
| 4084 | if (!PyMapping_Check(env)) { |
| 4085 | PyErr_SetString(PyExc_TypeError, |
| 4086 | "fexecve() arg 3 must be a mapping object"); |
| 4087 | return NULL; |
| 4088 | } |
| 4089 | |
| 4090 | argvlist = parse_arglist(argv, &argc); |
| 4091 | if (argvlist == NULL) |
| 4092 | return NULL; |
| 4093 | |
| 4094 | envlist = parse_envlist(env, &envc); |
| 4095 | if (envlist == NULL) |
| 4096 | goto fail; |
| 4097 | |
| 4098 | fexecve(fd, argvlist, envlist); |
| 4099 | |
| 4100 | /* If we get here it's definitely an error */ |
| 4101 | |
| 4102 | (void) posix_error(); |
| 4103 | |
| 4104 | while (--envc >= 0) |
| 4105 | PyMem_DEL(envlist[envc]); |
| 4106 | PyMem_DEL(envlist); |
| 4107 | fail: |
| 4108 | free_string_array(argvlist, argc); |
| 4109 | return NULL; |
| 4110 | } |
| 4111 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4112 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4113 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4114 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4115 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4116 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4117 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4118 | mode: mode of process creation\n\ |
| 4119 | path: path of executable file\n\ |
| 4120 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4121 | |
| 4122 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4123 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4124 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4125 | PyObject *opath; |
| 4126 | char *path; |
| 4127 | PyObject *argv; |
| 4128 | char **argvlist; |
| 4129 | int mode, i; |
| 4130 | Py_ssize_t argc; |
| 4131 | Py_intptr_t spawnval; |
| 4132 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4133 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4134 | /* spawnv has three arguments: (mode, path, argv), where |
| 4135 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4136 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4137 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4138 | PyUnicode_FSConverter, |
| 4139 | &opath, &argv)) |
| 4140 | return NULL; |
| 4141 | path = PyBytes_AsString(opath); |
| 4142 | if (PyList_Check(argv)) { |
| 4143 | argc = PyList_Size(argv); |
| 4144 | getitem = PyList_GetItem; |
| 4145 | } |
| 4146 | else if (PyTuple_Check(argv)) { |
| 4147 | argc = PyTuple_Size(argv); |
| 4148 | getitem = PyTuple_GetItem; |
| 4149 | } |
| 4150 | else { |
| 4151 | PyErr_SetString(PyExc_TypeError, |
| 4152 | "spawnv() arg 2 must be a tuple or list"); |
| 4153 | Py_DECREF(opath); |
| 4154 | return NULL; |
| 4155 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4156 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4157 | argvlist = PyMem_NEW(char *, argc+1); |
| 4158 | if (argvlist == NULL) { |
| 4159 | Py_DECREF(opath); |
| 4160 | return PyErr_NoMemory(); |
| 4161 | } |
| 4162 | for (i = 0; i < argc; i++) { |
| 4163 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4164 | &argvlist[i])) { |
| 4165 | free_string_array(argvlist, i); |
| 4166 | PyErr_SetString( |
| 4167 | PyExc_TypeError, |
| 4168 | "spawnv() arg 2 must contain only strings"); |
| 4169 | Py_DECREF(opath); |
| 4170 | return NULL; |
| 4171 | } |
| 4172 | } |
| 4173 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4174 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4175 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4176 | Py_BEGIN_ALLOW_THREADS |
| 4177 | spawnval = spawnv(mode, path, argvlist); |
| 4178 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4179 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4180 | if (mode == _OLD_P_OVERLAY) |
| 4181 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4182 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4183 | Py_BEGIN_ALLOW_THREADS |
| 4184 | spawnval = _spawnv(mode, path, argvlist); |
| 4185 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4186 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4187 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4188 | free_string_array(argvlist, argc); |
| 4189 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4190 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4191 | if (spawnval == -1) |
| 4192 | return posix_error(); |
| 4193 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4194 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4195 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4196 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4197 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4198 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4202 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4203 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4204 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4205 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4206 | mode: mode of process creation\n\ |
| 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 | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4210 | |
| 4211 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4212 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +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; |
| 4219 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4220 | int mode; |
| 4221 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4222 | Py_intptr_t spawnval; |
| 4223 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4224 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4225 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4226 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4227 | argv is a list or tuple of strings and env is a dictionary |
| 4228 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4229 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4230 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4231 | PyUnicode_FSConverter, |
| 4232 | &opath, &argv, &env)) |
| 4233 | return NULL; |
| 4234 | path = PyBytes_AsString(opath); |
| 4235 | if (PyList_Check(argv)) { |
| 4236 | argc = PyList_Size(argv); |
| 4237 | getitem = PyList_GetItem; |
| 4238 | } |
| 4239 | else if (PyTuple_Check(argv)) { |
| 4240 | argc = PyTuple_Size(argv); |
| 4241 | getitem = PyTuple_GetItem; |
| 4242 | } |
| 4243 | else { |
| 4244 | PyErr_SetString(PyExc_TypeError, |
| 4245 | "spawnve() arg 2 must be a tuple or list"); |
| 4246 | goto fail_0; |
| 4247 | } |
| 4248 | if (!PyMapping_Check(env)) { |
| 4249 | PyErr_SetString(PyExc_TypeError, |
| 4250 | "spawnve() arg 3 must be a mapping object"); |
| 4251 | goto fail_0; |
| 4252 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4253 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4254 | argvlist = PyMem_NEW(char *, argc+1); |
| 4255 | if (argvlist == NULL) { |
| 4256 | PyErr_NoMemory(); |
| 4257 | goto fail_0; |
| 4258 | } |
| 4259 | for (i = 0; i < argc; i++) { |
| 4260 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4261 | &argvlist[i])) |
| 4262 | { |
| 4263 | lastarg = i; |
| 4264 | goto fail_1; |
| 4265 | } |
| 4266 | } |
| 4267 | lastarg = argc; |
| 4268 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4269 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4270 | envlist = parse_envlist(env, &envc); |
| 4271 | if (envlist == NULL) |
| 4272 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4273 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4274 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4275 | Py_BEGIN_ALLOW_THREADS |
| 4276 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4277 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4278 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4279 | if (mode == _OLD_P_OVERLAY) |
| 4280 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4281 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4282 | Py_BEGIN_ALLOW_THREADS |
| 4283 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4284 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4285 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4286 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4287 | if (spawnval == -1) |
| 4288 | (void) posix_error(); |
| 4289 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4290 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4291 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4292 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4293 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4294 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4295 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4296 | while (--envc >= 0) |
| 4297 | PyMem_DEL(envlist[envc]); |
| 4298 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4299 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4300 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4301 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4302 | Py_DECREF(opath); |
| 4303 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4304 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4305 | |
| 4306 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4307 | #if defined(PYOS_OS2) |
| 4308 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4309 | "spawnvp(mode, file, args)\n\n\ |
| 4310 | Execute the program 'file' in a new process, using the environment\n\ |
| 4311 | search path to find the file.\n\ |
| 4312 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4313 | mode: mode of process creation\n\ |
| 4314 | file: executable file name\n\ |
| 4315 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4316 | |
| 4317 | static PyObject * |
| 4318 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4319 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4320 | PyObject *opath; |
| 4321 | char *path; |
| 4322 | PyObject *argv; |
| 4323 | char **argvlist; |
| 4324 | int mode, i, argc; |
| 4325 | Py_intptr_t spawnval; |
| 4326 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4327 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4328 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4329 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4330 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4331 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4332 | PyUnicode_FSConverter, |
| 4333 | &opath, &argv)) |
| 4334 | return NULL; |
| 4335 | path = PyBytes_AsString(opath); |
| 4336 | if (PyList_Check(argv)) { |
| 4337 | argc = PyList_Size(argv); |
| 4338 | getitem = PyList_GetItem; |
| 4339 | } |
| 4340 | else if (PyTuple_Check(argv)) { |
| 4341 | argc = PyTuple_Size(argv); |
| 4342 | getitem = PyTuple_GetItem; |
| 4343 | } |
| 4344 | else { |
| 4345 | PyErr_SetString(PyExc_TypeError, |
| 4346 | "spawnvp() arg 2 must be a tuple or list"); |
| 4347 | Py_DECREF(opath); |
| 4348 | return NULL; |
| 4349 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4350 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4351 | argvlist = PyMem_NEW(char *, argc+1); |
| 4352 | if (argvlist == NULL) { |
| 4353 | Py_DECREF(opath); |
| 4354 | return PyErr_NoMemory(); |
| 4355 | } |
| 4356 | for (i = 0; i < argc; i++) { |
| 4357 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4358 | &argvlist[i])) { |
| 4359 | free_string_array(argvlist, i); |
| 4360 | PyErr_SetString( |
| 4361 | PyExc_TypeError, |
| 4362 | "spawnvp() arg 2 must contain only strings"); |
| 4363 | Py_DECREF(opath); |
| 4364 | return NULL; |
| 4365 | } |
| 4366 | } |
| 4367 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4368 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4369 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4370 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4371 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4372 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4373 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4374 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4375 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4376 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4377 | free_string_array(argvlist, argc); |
| 4378 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4379 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4380 | if (spawnval == -1) |
| 4381 | return posix_error(); |
| 4382 | else |
| 4383 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | |
| 4387 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4388 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4389 | Execute the program 'file' in a new process, using the environment\n\ |
| 4390 | search path to find the file.\n\ |
| 4391 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4392 | mode: mode of process creation\n\ |
| 4393 | file: executable file name\n\ |
| 4394 | args: tuple or list of arguments\n\ |
| 4395 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4396 | |
| 4397 | static PyObject * |
| 4398 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4399 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4400 | PyObject *opath |
| 4401 | char *path; |
| 4402 | PyObject *argv, *env; |
| 4403 | char **argvlist; |
| 4404 | char **envlist; |
| 4405 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4406 | int mode; |
| 4407 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4408 | Py_intptr_t spawnval; |
| 4409 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4410 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4411 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4412 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4413 | argv is a list or tuple of strings and env is a dictionary |
| 4414 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4415 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4416 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4417 | PyUnicode_FSConverter, |
| 4418 | &opath, &argv, &env)) |
| 4419 | return NULL; |
| 4420 | path = PyBytes_AsString(opath); |
| 4421 | if (PyList_Check(argv)) { |
| 4422 | argc = PyList_Size(argv); |
| 4423 | getitem = PyList_GetItem; |
| 4424 | } |
| 4425 | else if (PyTuple_Check(argv)) { |
| 4426 | argc = PyTuple_Size(argv); |
| 4427 | getitem = PyTuple_GetItem; |
| 4428 | } |
| 4429 | else { |
| 4430 | PyErr_SetString(PyExc_TypeError, |
| 4431 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4432 | goto fail_0; |
| 4433 | } |
| 4434 | if (!PyMapping_Check(env)) { |
| 4435 | PyErr_SetString(PyExc_TypeError, |
| 4436 | "spawnvpe() arg 3 must be a mapping object"); |
| 4437 | goto fail_0; |
| 4438 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4440 | argvlist = PyMem_NEW(char *, argc+1); |
| 4441 | if (argvlist == NULL) { |
| 4442 | PyErr_NoMemory(); |
| 4443 | goto fail_0; |
| 4444 | } |
| 4445 | for (i = 0; i < argc; i++) { |
| 4446 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4447 | &argvlist[i])) |
| 4448 | { |
| 4449 | lastarg = i; |
| 4450 | goto fail_1; |
| 4451 | } |
| 4452 | } |
| 4453 | lastarg = argc; |
| 4454 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4455 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4456 | envlist = parse_envlist(env, &envc); |
| 4457 | if (envlist == NULL) |
| 4458 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4459 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4460 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4461 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4462 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4463 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4464 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4465 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4466 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4467 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4468 | if (spawnval == -1) |
| 4469 | (void) posix_error(); |
| 4470 | else |
| 4471 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4472 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4473 | while (--envc >= 0) |
| 4474 | PyMem_DEL(envlist[envc]); |
| 4475 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4476 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4477 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4478 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4479 | Py_DECREF(opath); |
| 4480 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4481 | } |
| 4482 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4483 | #endif /* HAVE_SPAWNV */ |
| 4484 | |
| 4485 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4486 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4487 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4488 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4489 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4490 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4491 | 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] | 4492 | |
| 4493 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4494 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4495 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4496 | pid_t pid; |
| 4497 | int result = 0; |
| 4498 | _PyImport_AcquireLock(); |
| 4499 | pid = fork1(); |
| 4500 | if (pid == 0) { |
| 4501 | /* child: this clobbers and resets the import lock. */ |
| 4502 | PyOS_AfterFork(); |
| 4503 | } else { |
| 4504 | /* parent: release the import lock. */ |
| 4505 | result = _PyImport_ReleaseLock(); |
| 4506 | } |
| 4507 | if (pid == -1) |
| 4508 | return posix_error(); |
| 4509 | if (result < 0) { |
| 4510 | /* Don't clobber the OSError if the fork failed. */ |
| 4511 | PyErr_SetString(PyExc_RuntimeError, |
| 4512 | "not holding the import lock"); |
| 4513 | return NULL; |
| 4514 | } |
| 4515 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4516 | } |
| 4517 | #endif |
| 4518 | |
| 4519 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4520 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4521 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4522 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4523 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4524 | 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] | 4525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4526 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4527 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4528 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4529 | pid_t pid; |
| 4530 | int result = 0; |
| 4531 | _PyImport_AcquireLock(); |
| 4532 | pid = fork(); |
| 4533 | if (pid == 0) { |
| 4534 | /* child: this clobbers and resets the import lock. */ |
| 4535 | PyOS_AfterFork(); |
| 4536 | } else { |
| 4537 | /* parent: release the import lock. */ |
| 4538 | result = _PyImport_ReleaseLock(); |
| 4539 | } |
| 4540 | if (pid == -1) |
| 4541 | return posix_error(); |
| 4542 | if (result < 0) { |
| 4543 | /* Don't clobber the OSError if the fork failed. */ |
| 4544 | PyErr_SetString(PyExc_RuntimeError, |
| 4545 | "not holding the import lock"); |
| 4546 | return NULL; |
| 4547 | } |
| 4548 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4549 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4550 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4551 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4552 | #ifdef HAVE_SCHED_H |
| 4553 | |
| 4554 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 4555 | "sched_get_priority_max(policy)\n\n\ |
| 4556 | Get the maximum scheduling priority for *policy*."); |
| 4557 | |
| 4558 | static PyObject * |
| 4559 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 4560 | { |
| 4561 | int policy, max; |
| 4562 | |
| 4563 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 4564 | return NULL; |
| 4565 | max = sched_get_priority_max(policy); |
| 4566 | if (max < 0) |
| 4567 | return posix_error(); |
| 4568 | return PyLong_FromLong(max); |
| 4569 | } |
| 4570 | |
| 4571 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 4572 | "sched_get_priority_min(policy)\n\n\ |
| 4573 | Get the minimum scheduling priority for *policy*."); |
| 4574 | |
| 4575 | static PyObject * |
| 4576 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 4577 | { |
| 4578 | int policy, min; |
| 4579 | |
| 4580 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 4581 | return NULL; |
| 4582 | min = sched_get_priority_min(policy); |
| 4583 | if (min < 0) |
| 4584 | return posix_error(); |
| 4585 | return PyLong_FromLong(min); |
| 4586 | } |
| 4587 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4588 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4589 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4590 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 4591 | "sched_getscheduler(pid)\n\n\ |
| 4592 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 4593 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 4594 | |
| 4595 | static PyObject * |
| 4596 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 4597 | { |
| 4598 | pid_t pid; |
| 4599 | int policy; |
| 4600 | |
| 4601 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 4602 | return NULL; |
| 4603 | policy = sched_getscheduler(pid); |
| 4604 | if (policy < 0) |
| 4605 | return posix_error(); |
| 4606 | return PyLong_FromLong(policy); |
| 4607 | } |
| 4608 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4609 | #endif |
| 4610 | |
| 4611 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 4612 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4613 | static PyObject * |
| 4614 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4615 | { |
| 4616 | PyObject *res, *priority; |
| 4617 | static char *kwlist[] = {"sched_priority"}; |
| 4618 | |
| 4619 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 4620 | return NULL; |
| 4621 | res = PyStructSequence_New(type); |
| 4622 | if (!res) |
| 4623 | return NULL; |
| 4624 | Py_INCREF(priority); |
| 4625 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4626 | return res; |
| 4627 | } |
| 4628 | |
| 4629 | PyDoc_STRVAR(sched_param__doc__, |
| 4630 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 4631 | Current has only one field: sched_priority"); |
| 4632 | |
| 4633 | static PyStructSequence_Field sched_param_fields[] = { |
| 4634 | {"sched_priority", "the scheduling priority"}, |
| 4635 | {0} |
| 4636 | }; |
| 4637 | |
| 4638 | static PyStructSequence_Desc sched_param_desc = { |
| 4639 | "sched_param", /* name */ |
| 4640 | sched_param__doc__, /* doc */ |
| 4641 | sched_param_fields, |
| 4642 | 1 |
| 4643 | }; |
| 4644 | |
| 4645 | static int |
| 4646 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 4647 | { |
| 4648 | long priority; |
| 4649 | |
| 4650 | if (Py_TYPE(param) != &SchedParamType) { |
| 4651 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 4652 | return 0; |
| 4653 | } |
| 4654 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 4655 | if (priority == -1 && PyErr_Occurred()) |
| 4656 | return 0; |
| 4657 | if (priority > INT_MAX || priority < INT_MIN) { |
| 4658 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 4659 | return 0; |
| 4660 | } |
| 4661 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 4662 | return 1; |
| 4663 | } |
| 4664 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4665 | #endif |
| 4666 | |
| 4667 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4668 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4669 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 4670 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 4671 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 4672 | If *pid* is 0, the calling process is changed.\n\ |
| 4673 | *param* is an instance of sched_param."); |
| 4674 | |
| 4675 | static PyObject * |
| 4676 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 4677 | { |
| 4678 | pid_t pid; |
| 4679 | int policy; |
| 4680 | struct sched_param param; |
| 4681 | |
| 4682 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 4683 | &pid, &policy, &convert_sched_param, ¶m)) |
| 4684 | return NULL; |
| 4685 | if (sched_setscheduler(pid, policy, ¶m)) |
| 4686 | return posix_error(); |
| 4687 | Py_RETURN_NONE; |
| 4688 | } |
| 4689 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4690 | #endif |
| 4691 | |
| 4692 | #ifdef HAVE_SCHED_SETPARAM |
| 4693 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4694 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 4695 | "sched_getparam(pid) -> sched_param\n\n\ |
| 4696 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 4697 | sched_param class. A PID of 0 means the calling process."); |
| 4698 | |
| 4699 | static PyObject * |
| 4700 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 4701 | { |
| 4702 | pid_t pid; |
| 4703 | struct sched_param param; |
| 4704 | PyObject *res, *priority; |
| 4705 | |
| 4706 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 4707 | return NULL; |
| 4708 | if (sched_getparam(pid, ¶m)) |
| 4709 | return posix_error(); |
| 4710 | res = PyStructSequence_New(&SchedParamType); |
| 4711 | if (!res) |
| 4712 | return NULL; |
| 4713 | priority = PyLong_FromLong(param.sched_priority); |
| 4714 | if (!priority) { |
| 4715 | Py_DECREF(res); |
| 4716 | return NULL; |
| 4717 | } |
| 4718 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4719 | return res; |
| 4720 | } |
| 4721 | |
| 4722 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 4723 | "sched_setparam(pid, param)\n\n\ |
| 4724 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 4725 | A PID of 0 means the calling process."); |
| 4726 | |
| 4727 | static PyObject * |
| 4728 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 4729 | { |
| 4730 | pid_t pid; |
| 4731 | struct sched_param param; |
| 4732 | |
| 4733 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 4734 | &pid, &convert_sched_param, ¶m)) |
| 4735 | return NULL; |
| 4736 | if (sched_setparam(pid, ¶m)) |
| 4737 | return posix_error(); |
| 4738 | Py_RETURN_NONE; |
| 4739 | } |
| 4740 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4741 | #endif |
| 4742 | |
| 4743 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 4744 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4745 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 4746 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 4747 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 4748 | |
| 4749 | static PyObject * |
| 4750 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 4751 | { |
| 4752 | pid_t pid; |
| 4753 | struct timespec interval; |
| 4754 | |
| 4755 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 4756 | return NULL; |
| 4757 | if (sched_rr_get_interval(pid, &interval)) |
| 4758 | return posix_error(); |
| 4759 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 4760 | } |
| 4761 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4762 | #endif |
| 4763 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4764 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 4765 | "sched_yield()\n\n\ |
| 4766 | Voluntarily relinquish the CPU."); |
| 4767 | |
| 4768 | static PyObject * |
| 4769 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 4770 | { |
| 4771 | if (sched_yield()) |
| 4772 | return posix_error(); |
| 4773 | Py_RETURN_NONE; |
| 4774 | } |
| 4775 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 4776 | #ifdef HAVE_SCHED_SETAFFINITY |
| 4777 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4778 | typedef struct { |
| 4779 | PyObject_HEAD; |
| 4780 | Py_ssize_t size; |
| 4781 | int ncpus; |
| 4782 | cpu_set_t *set; |
| 4783 | } Py_cpu_set; |
| 4784 | |
| 4785 | static PyTypeObject cpu_set_type; |
| 4786 | |
| 4787 | static void |
| 4788 | cpu_set_dealloc(Py_cpu_set *set) |
| 4789 | { |
| 4790 | assert(set->set); |
| 4791 | CPU_FREE(set->set); |
| 4792 | Py_TYPE(set)->tp_free(set); |
| 4793 | } |
| 4794 | |
| 4795 | static Py_cpu_set * |
| 4796 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 4797 | { |
| 4798 | Py_cpu_set *set; |
| 4799 | |
| 4800 | if (size < 0) { |
| 4801 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 4802 | return NULL; |
| 4803 | } |
| 4804 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 4805 | if (!set) |
| 4806 | return NULL; |
| 4807 | set->ncpus = size; |
| 4808 | set->size = CPU_ALLOC_SIZE(size); |
| 4809 | set->set = CPU_ALLOC(size); |
| 4810 | if (!set->set) { |
| 4811 | type->tp_free(set); |
| 4812 | PyErr_NoMemory(); |
| 4813 | return NULL; |
| 4814 | } |
| 4815 | CPU_ZERO_S(set->size, set->set); |
| 4816 | return set; |
| 4817 | } |
| 4818 | |
| 4819 | static PyObject * |
| 4820 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4821 | { |
| 4822 | int size; |
| 4823 | |
| 4824 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 4825 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 4826 | return NULL; |
| 4827 | return (PyObject *)make_new_cpu_set(type, size); |
| 4828 | } |
| 4829 | |
| 4830 | static PyObject * |
| 4831 | cpu_set_repr(Py_cpu_set *set) |
| 4832 | { |
| 4833 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
| 4834 | } |
| 4835 | |
| 4836 | static Py_ssize_t |
| 4837 | cpu_set_len(Py_cpu_set *set) |
| 4838 | { |
| 4839 | return set->ncpus; |
| 4840 | } |
| 4841 | |
| 4842 | static int |
| 4843 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 4844 | { |
| 4845 | int cpu; |
| 4846 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 4847 | return -1; |
| 4848 | if (cpu < 0) { |
| 4849 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 4850 | return -1; |
| 4851 | } |
| 4852 | if (cpu >= set->ncpus) { |
| 4853 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 4854 | return -1; |
| 4855 | } |
| 4856 | return cpu; |
| 4857 | } |
| 4858 | |
| 4859 | PyDoc_STRVAR(cpu_set_set_doc, |
| 4860 | "cpu_set.set(i)\n\n\ |
| 4861 | Add CPU *i* to the set."); |
| 4862 | |
| 4863 | static PyObject * |
| 4864 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 4865 | { |
| 4866 | int cpu = _get_cpu(set, "i|set", args); |
| 4867 | if (cpu == -1) |
| 4868 | return NULL; |
| 4869 | CPU_SET_S(cpu, set->size, set->set); |
| 4870 | Py_RETURN_NONE; |
| 4871 | } |
| 4872 | |
| 4873 | PyDoc_STRVAR(cpu_set_count_doc, |
| 4874 | "cpu_set.count() -> int\n\n\ |
| 4875 | Return the number of CPUs active in the set."); |
| 4876 | |
| 4877 | static PyObject * |
| 4878 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 4879 | { |
| 4880 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 4881 | } |
| 4882 | |
| 4883 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 4884 | "cpu_set.clear(i)\n\n\ |
| 4885 | Remove CPU *i* from the set."); |
| 4886 | |
| 4887 | static PyObject * |
| 4888 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 4889 | { |
| 4890 | int cpu = _get_cpu(set, "i|clear", args); |
| 4891 | if (cpu == -1) |
| 4892 | return NULL; |
| 4893 | CPU_CLR_S(cpu, set->size, set->set); |
| 4894 | Py_RETURN_NONE; |
| 4895 | } |
| 4896 | |
| 4897 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 4898 | "cpu_set.isset(i) -> bool\n\n\ |
| 4899 | Test if CPU *i* is in the set."); |
| 4900 | |
| 4901 | static PyObject * |
| 4902 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 4903 | { |
| 4904 | int cpu = _get_cpu(set, "i|isset", args); |
| 4905 | if (cpu == -1) |
| 4906 | return NULL; |
| 4907 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 4908 | Py_RETURN_TRUE; |
| 4909 | Py_RETURN_FALSE; |
| 4910 | } |
| 4911 | |
| 4912 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 4913 | "cpu_set.zero()\n\n\ |
| 4914 | Clear the cpu_set."); |
| 4915 | |
| 4916 | static PyObject * |
| 4917 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 4918 | { |
| 4919 | CPU_ZERO_S(set->size, set->set); |
| 4920 | Py_RETURN_NONE; |
| 4921 | } |
| 4922 | |
| 4923 | static PyObject * |
| 4924 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 4925 | { |
| 4926 | int eq; |
| 4927 | |
| 4928 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) { |
| 4929 | Py_INCREF(Py_NotImplemented); |
| 4930 | return Py_NotImplemented; |
| 4931 | } |
| 4932 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 4933 | if ((op == Py_EQ) ? eq : !eq) |
| 4934 | Py_RETURN_TRUE; |
| 4935 | else |
| 4936 | Py_RETURN_FALSE; |
| 4937 | } |
| 4938 | |
| 4939 | #define CPU_SET_BINOP(name, op) \ |
| 4940 | static PyObject * \ |
| 4941 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 4942 | if (res) { \ |
| 4943 | Py_INCREF(res); \ |
| 4944 | } \ |
| 4945 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 4946 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4947 | if (!res) \ |
| 4948 | return NULL; \ |
| 4949 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 4950 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4951 | Py_DECREF(res); \ |
| 4952 | Py_INCREF(Py_NotImplemented); \ |
| 4953 | return Py_NotImplemented; \ |
| 4954 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 4955 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4956 | op(res->size, res->set, left->set, right->set); \ |
| 4957 | return (PyObject *)res; \ |
| 4958 | } \ |
| 4959 | static PyObject * \ |
| 4960 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 4961 | return do_cpu_set_##name(left, right, NULL); \ |
| 4962 | } \ |
| 4963 | static PyObject * \ |
| 4964 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 4965 | return do_cpu_set_##name(left, right, left); \ |
| 4966 | } \ |
| 4967 | |
| 4968 | CPU_SET_BINOP(and, CPU_AND_S) |
| 4969 | CPU_SET_BINOP(or, CPU_OR_S) |
| 4970 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 4971 | #undef CPU_SET_BINOP |
| 4972 | |
| 4973 | PyDoc_STRVAR(cpu_set_doc, |
| 4974 | "cpu_set(size)\n\n\ |
| 4975 | Create an empty mask of CPUs."); |
| 4976 | |
| 4977 | static PyNumberMethods cpu_set_as_number = { |
| 4978 | 0, /*nb_add*/ |
| 4979 | 0, /*nb_subtract*/ |
| 4980 | 0, /*nb_multiply*/ |
| 4981 | 0, /*nb_remainder*/ |
| 4982 | 0, /*nb_divmod*/ |
| 4983 | 0, /*nb_power*/ |
| 4984 | 0, /*nb_negative*/ |
| 4985 | 0, /*nb_positive*/ |
| 4986 | 0, /*nb_absolute*/ |
| 4987 | 0, /*nb_bool*/ |
| 4988 | 0, /*nb_invert*/ |
| 4989 | 0, /*nb_lshift*/ |
| 4990 | 0, /*nb_rshift*/ |
| 4991 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 4992 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 4993 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 4994 | 0, /*nb_int*/ |
| 4995 | 0, /*nb_reserved*/ |
| 4996 | 0, /*nb_float*/ |
| 4997 | 0, /*nb_inplace_add*/ |
| 4998 | 0, /*nb_inplace_subtract*/ |
| 4999 | 0, /*nb_inplace_multiply*/ |
| 5000 | 0, /*nb_inplace_remainder*/ |
| 5001 | 0, /*nb_inplace_power*/ |
| 5002 | 0, /*nb_inplace_lshift*/ |
| 5003 | 0, /*nb_inplace_rshift*/ |
| 5004 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5005 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5006 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5007 | }; |
| 5008 | |
| 5009 | static PySequenceMethods cpu_set_as_sequence = { |
| 5010 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5011 | }; |
| 5012 | |
| 5013 | static PyMethodDef cpu_set_methods[] = { |
| 5014 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5015 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5016 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5017 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5018 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5019 | {NULL, NULL} /* sentinel */ |
| 5020 | }; |
| 5021 | |
| 5022 | static PyTypeObject cpu_set_type = { |
| 5023 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5024 | "posix.cpu_set", /* tp_name */ |
| 5025 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5026 | 0, /* tp_itemsize */ |
| 5027 | /* methods */ |
| 5028 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5029 | 0, /* tp_print */ |
| 5030 | 0, /* tp_getattr */ |
| 5031 | 0, /* tp_setattr */ |
| 5032 | 0, /* tp_reserved */ |
| 5033 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5034 | &cpu_set_as_number, /* tp_as_number */ |
| 5035 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5036 | 0, /* tp_as_mapping */ |
| 5037 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5038 | 0, /* tp_call */ |
| 5039 | 0, /* tp_str */ |
| 5040 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5041 | 0, /* tp_setattro */ |
| 5042 | 0, /* tp_as_buffer */ |
| 5043 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5044 | cpu_set_doc, /* tp_doc */ |
| 5045 | 0, /* tp_traverse */ |
| 5046 | 0, /* tp_clear */ |
| 5047 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5048 | 0, /* tp_weaklistoffset */ |
| 5049 | 0, /* tp_iter */ |
| 5050 | 0, /* tp_iternext */ |
| 5051 | cpu_set_methods, /* tp_methods */ |
| 5052 | 0, /* tp_members */ |
| 5053 | 0, /* tp_getset */ |
| 5054 | 0, /* tp_base */ |
| 5055 | 0, /* tp_dict */ |
| 5056 | 0, /* tp_descr_get */ |
| 5057 | 0, /* tp_descr_set */ |
| 5058 | 0, /* tp_dictoffset */ |
| 5059 | 0, /* tp_init */ |
| 5060 | PyType_GenericAlloc, /* tp_alloc */ |
| 5061 | cpu_set_new, /* tp_new */ |
| 5062 | PyObject_Del, /* tp_free */ |
| 5063 | }; |
| 5064 | |
| 5065 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5066 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5067 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5068 | |
| 5069 | static PyObject * |
| 5070 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5071 | { |
| 5072 | pid_t pid; |
| 5073 | Py_cpu_set *cpu_set; |
| 5074 | |
| 5075 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!|sched_setaffinity", |
| 5076 | &pid, &cpu_set_type, &cpu_set)) |
| 5077 | return NULL; |
| 5078 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5079 | return posix_error(); |
| 5080 | Py_RETURN_NONE; |
| 5081 | } |
| 5082 | |
| 5083 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5084 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5085 | Return the affinity of the process with PID *pid*.\n\ |
| 5086 | The returned cpu_set will be of size *ncpus*."); |
| 5087 | |
| 5088 | static PyObject * |
| 5089 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5090 | { |
| 5091 | pid_t pid; |
| 5092 | int ncpus; |
| 5093 | Py_cpu_set *res; |
| 5094 | |
| 5095 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i|sched_getaffinity", |
| 5096 | &pid, &ncpus)) |
| 5097 | return NULL; |
| 5098 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5099 | if (!res) |
| 5100 | return NULL; |
| 5101 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5102 | Py_DECREF(res); |
| 5103 | return posix_error(); |
| 5104 | } |
| 5105 | return (PyObject *)res; |
| 5106 | } |
| 5107 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5108 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5109 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5110 | #endif /* HAVE_SCHED_H */ |
| 5111 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5112 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5113 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5114 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5115 | #define DEV_PTY_FILE "/dev/ptc" |
| 5116 | #define HAVE_DEV_PTMX |
| 5117 | #else |
| 5118 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5119 | #endif |
| 5120 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5121 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5122 | #ifdef HAVE_PTY_H |
| 5123 | #include <pty.h> |
| 5124 | #else |
| 5125 | #ifdef HAVE_LIBUTIL_H |
| 5126 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5127 | #else |
| 5128 | #ifdef HAVE_UTIL_H |
| 5129 | #include <util.h> |
| 5130 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5131 | #endif /* HAVE_LIBUTIL_H */ |
| 5132 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5133 | #ifdef HAVE_STROPTS_H |
| 5134 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5135 | #endif |
| 5136 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5137 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5138 | #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] | 5139 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5140 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5141 | 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] | 5142 | |
| 5143 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5144 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5145 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5146 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5147 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5148 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5149 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5150 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5151 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5152 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5153 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5154 | #endif |
| 5155 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5156 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5157 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5158 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5159 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5160 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5161 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5162 | if (slave_name == NULL) |
| 5163 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5164 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5165 | slave_fd = open(slave_name, O_RDWR); |
| 5166 | if (slave_fd < 0) |
| 5167 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5168 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5169 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5170 | if (master_fd < 0) |
| 5171 | return posix_error(); |
| 5172 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5173 | /* change permission of slave */ |
| 5174 | if (grantpt(master_fd) < 0) { |
| 5175 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5176 | return posix_error(); |
| 5177 | } |
| 5178 | /* unlock slave */ |
| 5179 | if (unlockpt(master_fd) < 0) { |
| 5180 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5181 | return posix_error(); |
| 5182 | } |
| 5183 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5184 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5185 | if (slave_name == NULL) |
| 5186 | return posix_error(); |
| 5187 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5188 | if (slave_fd < 0) |
| 5189 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5190 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5191 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5192 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5193 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5194 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5195 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5196 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5197 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5198 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5199 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5200 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5201 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5202 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5203 | |
| 5204 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5205 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5206 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5207 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5208 | 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] | 5209 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5210 | |
| 5211 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5212 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5213 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5214 | int master_fd = -1, result = 0; |
| 5215 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5216 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5217 | _PyImport_AcquireLock(); |
| 5218 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5219 | if (pid == 0) { |
| 5220 | /* child: this clobbers and resets the import lock. */ |
| 5221 | PyOS_AfterFork(); |
| 5222 | } else { |
| 5223 | /* parent: release the import lock. */ |
| 5224 | result = _PyImport_ReleaseLock(); |
| 5225 | } |
| 5226 | if (pid == -1) |
| 5227 | return posix_error(); |
| 5228 | if (result < 0) { |
| 5229 | /* Don't clobber the OSError if the fork failed. */ |
| 5230 | PyErr_SetString(PyExc_RuntimeError, |
| 5231 | "not holding the import lock"); |
| 5232 | return NULL; |
| 5233 | } |
| 5234 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5235 | } |
| 5236 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5237 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5238 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5239 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5240 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5241 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5242 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5243 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5244 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5245 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5246 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5247 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5248 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5249 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5250 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5251 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5252 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5253 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5254 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5255 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5256 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5257 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5258 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5259 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5260 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5261 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5262 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5263 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5264 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5265 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5266 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5267 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5268 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5269 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5270 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5271 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5272 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5273 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5274 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5275 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5276 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5277 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5278 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5279 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5280 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5281 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5282 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5283 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5284 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5285 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5286 | } |
| 5287 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5288 | #ifdef HAVE_GETGROUPLIST |
| 5289 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5290 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5291 | Returns a list of groups to which a user belongs.\n\n\ |
| 5292 | user: username to lookup\n\ |
| 5293 | group: base group id of the user"); |
| 5294 | |
| 5295 | static PyObject * |
| 5296 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5297 | { |
| 5298 | #ifdef NGROUPS_MAX |
| 5299 | #define MAX_GROUPS NGROUPS_MAX |
| 5300 | #else |
| 5301 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5302 | #define MAX_GROUPS 64 |
| 5303 | #endif |
| 5304 | |
| 5305 | const char *user; |
| 5306 | int i, ngroups; |
| 5307 | PyObject *list; |
| 5308 | #ifdef __APPLE__ |
| 5309 | int *groups, basegid; |
| 5310 | #else |
| 5311 | gid_t *groups, basegid; |
| 5312 | #endif |
| 5313 | ngroups = MAX_GROUPS; |
| 5314 | |
| 5315 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5316 | return NULL; |
| 5317 | |
| 5318 | #ifdef __APPLE__ |
| 5319 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5320 | #else |
| 5321 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5322 | #endif |
| 5323 | if (groups == NULL) |
| 5324 | return PyErr_NoMemory(); |
| 5325 | |
| 5326 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5327 | PyMem_Del(groups); |
| 5328 | return posix_error(); |
| 5329 | } |
| 5330 | |
| 5331 | list = PyList_New(ngroups); |
| 5332 | if (list == NULL) { |
| 5333 | PyMem_Del(groups); |
| 5334 | return NULL; |
| 5335 | } |
| 5336 | |
| 5337 | for (i = 0; i < ngroups; i++) { |
| 5338 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5339 | if (o == NULL) { |
| 5340 | Py_DECREF(list); |
| 5341 | PyMem_Del(groups); |
| 5342 | return NULL; |
| 5343 | } |
| 5344 | PyList_SET_ITEM(list, i, o); |
| 5345 | } |
| 5346 | |
| 5347 | PyMem_Del(groups); |
| 5348 | |
| 5349 | return list; |
| 5350 | } |
| 5351 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5352 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5353 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5354 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5355 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5356 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5357 | |
| 5358 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5359 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5360 | { |
| 5361 | PyObject *result = NULL; |
| 5362 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5363 | #ifdef NGROUPS_MAX |
| 5364 | #define MAX_GROUPS NGROUPS_MAX |
| 5365 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5366 | /* 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] | 5367 | #define MAX_GROUPS 64 |
| 5368 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5369 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5370 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5371 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5372 | * This is a helper variable to store the intermediate result when |
| 5373 | * that happens. |
| 5374 | * |
| 5375 | * To keep the code readable the OSX behaviour is unconditional, |
| 5376 | * according to the POSIX spec this should be safe on all unix-y |
| 5377 | * systems. |
| 5378 | */ |
| 5379 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5380 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5382 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5383 | if (n < 0) { |
| 5384 | if (errno == EINVAL) { |
| 5385 | n = getgroups(0, NULL); |
| 5386 | if (n == -1) { |
| 5387 | return posix_error(); |
| 5388 | } |
| 5389 | if (n == 0) { |
| 5390 | /* Avoid malloc(0) */ |
| 5391 | alt_grouplist = grouplist; |
| 5392 | } else { |
| 5393 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5394 | if (alt_grouplist == NULL) { |
| 5395 | errno = EINVAL; |
| 5396 | return posix_error(); |
| 5397 | } |
| 5398 | n = getgroups(n, alt_grouplist); |
| 5399 | if (n == -1) { |
| 5400 | PyMem_Free(alt_grouplist); |
| 5401 | return posix_error(); |
| 5402 | } |
| 5403 | } |
| 5404 | } else { |
| 5405 | return posix_error(); |
| 5406 | } |
| 5407 | } |
| 5408 | result = PyList_New(n); |
| 5409 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5410 | int i; |
| 5411 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5412 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5413 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5414 | Py_DECREF(result); |
| 5415 | result = NULL; |
| 5416 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5417 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5418 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5419 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
| 5422 | if (alt_grouplist != grouplist) { |
| 5423 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5424 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5425 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5426 | return result; |
| 5427 | } |
| 5428 | #endif |
| 5429 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5430 | #ifdef HAVE_INITGROUPS |
| 5431 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5432 | "initgroups(username, gid) -> None\n\n\ |
| 5433 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5434 | the groups of which the specified username is a member, plus the specified\n\ |
| 5435 | group id."); |
| 5436 | |
| 5437 | static PyObject * |
| 5438 | posix_initgroups(PyObject *self, PyObject *args) |
| 5439 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5440 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5441 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5442 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5443 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5444 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5445 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5446 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5447 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5448 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5449 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5450 | res = initgroups(username, (gid_t) gid); |
| 5451 | Py_DECREF(oname); |
| 5452 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5453 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5454 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5455 | Py_INCREF(Py_None); |
| 5456 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5457 | } |
| 5458 | #endif |
| 5459 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5460 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5461 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5462 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5463 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5464 | |
| 5465 | static PyObject * |
| 5466 | posix_getpgid(PyObject *self, PyObject *args) |
| 5467 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5468 | pid_t pid, pgid; |
| 5469 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5470 | return NULL; |
| 5471 | pgid = getpgid(pid); |
| 5472 | if (pgid < 0) |
| 5473 | return posix_error(); |
| 5474 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5475 | } |
| 5476 | #endif /* HAVE_GETPGID */ |
| 5477 | |
| 5478 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5479 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5480 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5481 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5482 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5483 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5484 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5485 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5486 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5487 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5488 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5489 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5490 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5491 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5492 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5493 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5494 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5495 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5496 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5497 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5498 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5499 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5500 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5501 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5502 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5503 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5504 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5505 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5506 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5507 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5508 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5509 | return posix_error(); |
| 5510 | Py_INCREF(Py_None); |
| 5511 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5512 | } |
| 5513 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5514 | #endif /* HAVE_SETPGRP */ |
| 5515 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5516 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5517 | |
| 5518 | #ifdef MS_WINDOWS |
| 5519 | #include <tlhelp32.h> |
| 5520 | |
| 5521 | static PyObject* |
| 5522 | win32_getppid() |
| 5523 | { |
| 5524 | HANDLE snapshot; |
| 5525 | pid_t mypid; |
| 5526 | PyObject* result = NULL; |
| 5527 | BOOL have_record; |
| 5528 | PROCESSENTRY32 pe; |
| 5529 | |
| 5530 | mypid = getpid(); /* This function never fails */ |
| 5531 | |
| 5532 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 5533 | if (snapshot == INVALID_HANDLE_VALUE) |
| 5534 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5535 | |
| 5536 | pe.dwSize = sizeof(pe); |
| 5537 | have_record = Process32First(snapshot, &pe); |
| 5538 | while (have_record) { |
| 5539 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 5540 | /* We could cache the ulong value in a static variable. */ |
| 5541 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 5542 | break; |
| 5543 | } |
| 5544 | |
| 5545 | have_record = Process32Next(snapshot, &pe); |
| 5546 | } |
| 5547 | |
| 5548 | /* If our loop exits and our pid was not found (result will be NULL) |
| 5549 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 5550 | * error anyway, so let's raise it. */ |
| 5551 | if (!result) |
| 5552 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5553 | |
| 5554 | CloseHandle(snapshot); |
| 5555 | |
| 5556 | return result; |
| 5557 | } |
| 5558 | #endif /*MS_WINDOWS*/ |
| 5559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5560 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5561 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5562 | Return the parent's process id. If the parent process has already exited,\n\ |
| 5563 | Windows machines will still return its id; others systems will return the id\n\ |
| 5564 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5565 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5566 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5567 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5568 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5569 | #ifdef MS_WINDOWS |
| 5570 | return win32_getppid(); |
| 5571 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5572 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5573 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5574 | } |
| 5575 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5576 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5577 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5578 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5579 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5580 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5581 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5582 | |
| 5583 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5584 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5585 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5586 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5587 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5588 | wchar_t user_name[UNLEN + 1]; |
| 5589 | DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]); |
| 5590 | |
| 5591 | if (GetUserNameW(user_name, &num_chars)) { |
| 5592 | /* num_chars is the number of unicode chars plus null terminator */ |
| 5593 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5594 | } |
| 5595 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5596 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5597 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5598 | char *name; |
| 5599 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5600 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5601 | errno = 0; |
| 5602 | name = getlogin(); |
| 5603 | if (name == NULL) { |
| 5604 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5605 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5606 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5607 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5608 | } |
| 5609 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5610 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5611 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5612 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5613 | return result; |
| 5614 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5615 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5616 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5617 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5618 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5619 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5620 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5621 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5622 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5623 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5624 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5625 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5626 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5627 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5628 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5629 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5630 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5631 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5632 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5633 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5634 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5635 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5636 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5637 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5638 | pid_t pid; |
| 5639 | int sig; |
| 5640 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 5641 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5642 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5643 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 5644 | APIRET rc; |
| 5645 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5646 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5647 | |
| 5648 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 5649 | APIRET rc; |
| 5650 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5651 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5652 | |
| 5653 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5654 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5655 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5656 | if (kill(pid, sig) == -1) |
| 5657 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5658 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5659 | Py_INCREF(Py_None); |
| 5660 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5661 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5662 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5663 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5664 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5665 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5666 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5667 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5668 | |
| 5669 | static PyObject * |
| 5670 | posix_killpg(PyObject *self, PyObject *args) |
| 5671 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5672 | int sig; |
| 5673 | pid_t pgid; |
| 5674 | /* XXX some man pages make the `pgid` parameter an int, others |
| 5675 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 5676 | take the same type. Moreover, pid_t is always at least as wide as |
| 5677 | int (else compilation of this module fails), which is safe. */ |
| 5678 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5679 | return NULL; |
| 5680 | if (killpg(pgid, sig) == -1) |
| 5681 | return posix_error(); |
| 5682 | Py_INCREF(Py_None); |
| 5683 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5684 | } |
| 5685 | #endif |
| 5686 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5687 | #ifdef MS_WINDOWS |
| 5688 | PyDoc_STRVAR(win32_kill__doc__, |
| 5689 | "kill(pid, sig)\n\n\ |
| 5690 | Kill a process with a signal."); |
| 5691 | |
| 5692 | static PyObject * |
| 5693 | win32_kill(PyObject *self, PyObject *args) |
| 5694 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 5695 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5696 | DWORD pid, sig, err; |
| 5697 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5698 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5699 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 5700 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5701 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5702 | /* Console processes which share a common console can be sent CTRL+C or |
| 5703 | CTRL+BREAK events, provided they handle said events. */ |
| 5704 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 5705 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 5706 | err = GetLastError(); |
| 5707 | PyErr_SetFromWindowsErr(err); |
| 5708 | } |
| 5709 | else |
| 5710 | Py_RETURN_NONE; |
| 5711 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5712 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5713 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 5714 | attempt to open and terminate the process. */ |
| 5715 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 5716 | if (handle == NULL) { |
| 5717 | err = GetLastError(); |
| 5718 | return PyErr_SetFromWindowsErr(err); |
| 5719 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5720 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5721 | if (TerminateProcess(handle, sig) == 0) { |
| 5722 | err = GetLastError(); |
| 5723 | result = PyErr_SetFromWindowsErr(err); |
| 5724 | } else { |
| 5725 | Py_INCREF(Py_None); |
| 5726 | result = Py_None; |
| 5727 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5728 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5729 | CloseHandle(handle); |
| 5730 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5731 | } |
| 5732 | #endif /* MS_WINDOWS */ |
| 5733 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5734 | #ifdef HAVE_PLOCK |
| 5735 | |
| 5736 | #ifdef HAVE_SYS_LOCK_H |
| 5737 | #include <sys/lock.h> |
| 5738 | #endif |
| 5739 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5740 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5741 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5742 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5743 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5744 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5745 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5746 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5747 | int op; |
| 5748 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 5749 | return NULL; |
| 5750 | if (plock(op) == -1) |
| 5751 | return posix_error(); |
| 5752 | Py_INCREF(Py_None); |
| 5753 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5754 | } |
| 5755 | #endif |
| 5756 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5757 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5758 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5759 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5760 | Set the current process's user id."); |
| 5761 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5762 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5763 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5764 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5765 | long uid_arg; |
| 5766 | uid_t uid; |
| 5767 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5768 | return NULL; |
| 5769 | uid = uid_arg; |
| 5770 | if (uid != uid_arg) { |
| 5771 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5772 | return NULL; |
| 5773 | } |
| 5774 | if (setuid(uid) < 0) |
| 5775 | return posix_error(); |
| 5776 | Py_INCREF(Py_None); |
| 5777 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5778 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5779 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5780 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5781 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5782 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5783 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5784 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5785 | Set the current process's effective user id."); |
| 5786 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5787 | static PyObject * |
| 5788 | posix_seteuid (PyObject *self, PyObject *args) |
| 5789 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5790 | long euid_arg; |
| 5791 | uid_t euid; |
| 5792 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 5793 | return NULL; |
| 5794 | euid = euid_arg; |
| 5795 | if (euid != euid_arg) { |
| 5796 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5797 | return NULL; |
| 5798 | } |
| 5799 | if (seteuid(euid) < 0) { |
| 5800 | return posix_error(); |
| 5801 | } else { |
| 5802 | Py_INCREF(Py_None); |
| 5803 | return Py_None; |
| 5804 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5805 | } |
| 5806 | #endif /* HAVE_SETEUID */ |
| 5807 | |
| 5808 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5809 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5810 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5811 | Set the current process's effective group id."); |
| 5812 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5813 | static PyObject * |
| 5814 | posix_setegid (PyObject *self, PyObject *args) |
| 5815 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5816 | long egid_arg; |
| 5817 | gid_t egid; |
| 5818 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 5819 | return NULL; |
| 5820 | egid = egid_arg; |
| 5821 | if (egid != egid_arg) { |
| 5822 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5823 | return NULL; |
| 5824 | } |
| 5825 | if (setegid(egid) < 0) { |
| 5826 | return posix_error(); |
| 5827 | } else { |
| 5828 | Py_INCREF(Py_None); |
| 5829 | return Py_None; |
| 5830 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5831 | } |
| 5832 | #endif /* HAVE_SETEGID */ |
| 5833 | |
| 5834 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5835 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5836 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5837 | Set the current process's real and effective user ids."); |
| 5838 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5839 | static PyObject * |
| 5840 | posix_setreuid (PyObject *self, PyObject *args) |
| 5841 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5842 | long ruid_arg, euid_arg; |
| 5843 | uid_t ruid, euid; |
| 5844 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 5845 | return NULL; |
| 5846 | if (ruid_arg == -1) |
| 5847 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 5848 | else |
| 5849 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 5850 | if (euid_arg == -1) |
| 5851 | euid = (uid_t)-1; |
| 5852 | else |
| 5853 | euid = euid_arg; |
| 5854 | if ((euid_arg != -1 && euid != euid_arg) || |
| 5855 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 5856 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5857 | return NULL; |
| 5858 | } |
| 5859 | if (setreuid(ruid, euid) < 0) { |
| 5860 | return posix_error(); |
| 5861 | } else { |
| 5862 | Py_INCREF(Py_None); |
| 5863 | return Py_None; |
| 5864 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5865 | } |
| 5866 | #endif /* HAVE_SETREUID */ |
| 5867 | |
| 5868 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5869 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5870 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5871 | Set the current process's real and effective group ids."); |
| 5872 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5873 | static PyObject * |
| 5874 | posix_setregid (PyObject *self, PyObject *args) |
| 5875 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5876 | long rgid_arg, egid_arg; |
| 5877 | gid_t rgid, egid; |
| 5878 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 5879 | return NULL; |
| 5880 | if (rgid_arg == -1) |
| 5881 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 5882 | else |
| 5883 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 5884 | if (egid_arg == -1) |
| 5885 | egid = (gid_t)-1; |
| 5886 | else |
| 5887 | egid = egid_arg; |
| 5888 | if ((egid_arg != -1 && egid != egid_arg) || |
| 5889 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 5890 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5891 | return NULL; |
| 5892 | } |
| 5893 | if (setregid(rgid, egid) < 0) { |
| 5894 | return posix_error(); |
| 5895 | } else { |
| 5896 | Py_INCREF(Py_None); |
| 5897 | return Py_None; |
| 5898 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5899 | } |
| 5900 | #endif /* HAVE_SETREGID */ |
| 5901 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5902 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5903 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5904 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5905 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5906 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5907 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5908 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5909 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5910 | long gid_arg; |
| 5911 | gid_t gid; |
| 5912 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 5913 | return NULL; |
| 5914 | gid = gid_arg; |
| 5915 | if (gid != gid_arg) { |
| 5916 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5917 | return NULL; |
| 5918 | } |
| 5919 | if (setgid(gid) < 0) |
| 5920 | return posix_error(); |
| 5921 | Py_INCREF(Py_None); |
| 5922 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5923 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5924 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5925 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5926 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5927 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5928 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5929 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5930 | |
| 5931 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 5932 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5933 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5934 | int i, len; |
| 5935 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5936 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5937 | if (!PySequence_Check(groups)) { |
| 5938 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5939 | return NULL; |
| 5940 | } |
| 5941 | len = PySequence_Size(groups); |
| 5942 | if (len > MAX_GROUPS) { |
| 5943 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5944 | return NULL; |
| 5945 | } |
| 5946 | for(i = 0; i < len; i++) { |
| 5947 | PyObject *elem; |
| 5948 | elem = PySequence_GetItem(groups, i); |
| 5949 | if (!elem) |
| 5950 | return NULL; |
| 5951 | if (!PyLong_Check(elem)) { |
| 5952 | PyErr_SetString(PyExc_TypeError, |
| 5953 | "groups must be integers"); |
| 5954 | Py_DECREF(elem); |
| 5955 | return NULL; |
| 5956 | } else { |
| 5957 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5958 | if (PyErr_Occurred()) { |
| 5959 | PyErr_SetString(PyExc_TypeError, |
| 5960 | "group id too big"); |
| 5961 | Py_DECREF(elem); |
| 5962 | return NULL; |
| 5963 | } |
| 5964 | grouplist[i] = x; |
| 5965 | /* read back the value to see if it fitted in gid_t */ |
| 5966 | if (grouplist[i] != x) { |
| 5967 | PyErr_SetString(PyExc_TypeError, |
| 5968 | "group id too big"); |
| 5969 | Py_DECREF(elem); |
| 5970 | return NULL; |
| 5971 | } |
| 5972 | } |
| 5973 | Py_DECREF(elem); |
| 5974 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5975 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5976 | if (setgroups(len, grouplist) < 0) |
| 5977 | return posix_error(); |
| 5978 | Py_INCREF(Py_None); |
| 5979 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5980 | } |
| 5981 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5982 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5983 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 5984 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 5985 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5986 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5987 | PyObject *result; |
| 5988 | static PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5989 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5990 | if (pid == -1) |
| 5991 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5992 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5993 | if (struct_rusage == NULL) { |
| 5994 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 5995 | if (m == NULL) |
| 5996 | return NULL; |
| 5997 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5998 | Py_DECREF(m); |
| 5999 | if (struct_rusage == NULL) |
| 6000 | return NULL; |
| 6001 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6002 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6003 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6004 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6005 | if (!result) |
| 6006 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6007 | |
| 6008 | #ifndef doubletime |
| 6009 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6010 | #endif |
| 6011 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6012 | PyStructSequence_SET_ITEM(result, 0, |
| 6013 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 6014 | PyStructSequence_SET_ITEM(result, 1, |
| 6015 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6016 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6017 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6018 | SET_INT(result, 2, ru->ru_maxrss); |
| 6019 | SET_INT(result, 3, ru->ru_ixrss); |
| 6020 | SET_INT(result, 4, ru->ru_idrss); |
| 6021 | SET_INT(result, 5, ru->ru_isrss); |
| 6022 | SET_INT(result, 6, ru->ru_minflt); |
| 6023 | SET_INT(result, 7, ru->ru_majflt); |
| 6024 | SET_INT(result, 8, ru->ru_nswap); |
| 6025 | SET_INT(result, 9, ru->ru_inblock); |
| 6026 | SET_INT(result, 10, ru->ru_oublock); |
| 6027 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6028 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6029 | SET_INT(result, 13, ru->ru_nsignals); |
| 6030 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6031 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6032 | #undef SET_INT |
| 6033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6034 | if (PyErr_Occurred()) { |
| 6035 | Py_DECREF(result); |
| 6036 | return NULL; |
| 6037 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6038 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6039 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6040 | } |
| 6041 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6042 | |
| 6043 | #ifdef HAVE_WAIT3 |
| 6044 | PyDoc_STRVAR(posix_wait3__doc__, |
| 6045 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 6046 | Wait for completion of a child process."); |
| 6047 | |
| 6048 | static PyObject * |
| 6049 | posix_wait3(PyObject *self, PyObject *args) |
| 6050 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6051 | pid_t pid; |
| 6052 | int options; |
| 6053 | struct rusage ru; |
| 6054 | WAIT_TYPE status; |
| 6055 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6056 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6057 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 6058 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6059 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6060 | Py_BEGIN_ALLOW_THREADS |
| 6061 | pid = wait3(&status, options, &ru); |
| 6062 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6063 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6064 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6065 | } |
| 6066 | #endif /* HAVE_WAIT3 */ |
| 6067 | |
| 6068 | #ifdef HAVE_WAIT4 |
| 6069 | PyDoc_STRVAR(posix_wait4__doc__, |
| 6070 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 6071 | Wait for completion of a given child process."); |
| 6072 | |
| 6073 | static PyObject * |
| 6074 | posix_wait4(PyObject *self, PyObject *args) |
| 6075 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6076 | pid_t pid; |
| 6077 | int options; |
| 6078 | struct rusage ru; |
| 6079 | WAIT_TYPE status; |
| 6080 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6081 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6082 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
| 6083 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6084 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6085 | Py_BEGIN_ALLOW_THREADS |
| 6086 | pid = wait4(pid, &status, options, &ru); |
| 6087 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6088 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6089 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6090 | } |
| 6091 | #endif /* HAVE_WAIT4 */ |
| 6092 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6093 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6094 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6095 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6096 | Wait for the completion of one or more child processes.\n\n\ |
| 6097 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6098 | id specifies the pid to wait on.\n\ |
| 6099 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6100 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6101 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6102 | no children in a waitable state."); |
| 6103 | |
| 6104 | static PyObject * |
| 6105 | posix_waitid(PyObject *self, PyObject *args) |
| 6106 | { |
| 6107 | PyObject *result; |
| 6108 | idtype_t idtype; |
| 6109 | id_t id; |
| 6110 | int options, res; |
| 6111 | siginfo_t si; |
| 6112 | si.si_pid = 0; |
| 6113 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6114 | return NULL; |
| 6115 | Py_BEGIN_ALLOW_THREADS |
| 6116 | res = waitid(idtype, id, &si, options); |
| 6117 | Py_END_ALLOW_THREADS |
| 6118 | if (res == -1) |
| 6119 | return posix_error(); |
| 6120 | |
| 6121 | if (si.si_pid == 0) |
| 6122 | Py_RETURN_NONE; |
| 6123 | |
| 6124 | result = PyStructSequence_New(&WaitidResultType); |
| 6125 | if (!result) |
| 6126 | return NULL; |
| 6127 | |
| 6128 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6129 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6130 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6131 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6132 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6133 | if (PyErr_Occurred()) { |
| 6134 | Py_DECREF(result); |
| 6135 | return NULL; |
| 6136 | } |
| 6137 | |
| 6138 | return result; |
| 6139 | } |
| 6140 | #endif |
| 6141 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6142 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6143 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6144 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6145 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6146 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6147 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6148 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6149 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6150 | pid_t pid; |
| 6151 | int options; |
| 6152 | WAIT_TYPE status; |
| 6153 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6154 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6155 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6156 | return NULL; |
| 6157 | Py_BEGIN_ALLOW_THREADS |
| 6158 | pid = waitpid(pid, &status, options); |
| 6159 | Py_END_ALLOW_THREADS |
| 6160 | if (pid == -1) |
| 6161 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6162 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6163 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6164 | } |
| 6165 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6166 | #elif defined(HAVE_CWAIT) |
| 6167 | |
| 6168 | /* 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] | 6169 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6170 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6171 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6172 | |
| 6173 | static PyObject * |
| 6174 | posix_waitpid(PyObject *self, PyObject *args) |
| 6175 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6176 | Py_intptr_t pid; |
| 6177 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6178 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6179 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6180 | return NULL; |
| 6181 | Py_BEGIN_ALLOW_THREADS |
| 6182 | pid = _cwait(&status, pid, options); |
| 6183 | Py_END_ALLOW_THREADS |
| 6184 | if (pid == -1) |
| 6185 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6186 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6187 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6188 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6189 | } |
| 6190 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6191 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6192 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6193 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6194 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6195 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6196 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6197 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6198 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6199 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6200 | pid_t pid; |
| 6201 | WAIT_TYPE status; |
| 6202 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6203 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6204 | Py_BEGIN_ALLOW_THREADS |
| 6205 | pid = wait(&status); |
| 6206 | Py_END_ALLOW_THREADS |
| 6207 | if (pid == -1) |
| 6208 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6209 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6210 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6211 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6212 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6213 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6214 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6215 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6216 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6217 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6218 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6219 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6220 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6221 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6222 | #ifdef HAVE_LSTAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6223 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6224 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6225 | #ifdef MS_WINDOWS |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 6226 | return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6227 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6228 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6229 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6230 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6231 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6234 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6235 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6236 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6237 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6238 | 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] | 6239 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6240 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6241 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6242 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6243 | PyObject* v; |
| 6244 | char buf[MAXPATHLEN]; |
| 6245 | PyObject *opath; |
| 6246 | char *path; |
| 6247 | int n; |
| 6248 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6249 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6250 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 6251 | PyUnicode_FSConverter, &opath)) |
| 6252 | return NULL; |
| 6253 | path = PyBytes_AsString(opath); |
| 6254 | v = PySequence_GetItem(args, 0); |
| 6255 | if (v == NULL) { |
| 6256 | Py_DECREF(opath); |
| 6257 | return NULL; |
| 6258 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6259 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6260 | if (PyUnicode_Check(v)) { |
| 6261 | arg_is_unicode = 1; |
| 6262 | } |
| 6263 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6264 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6265 | Py_BEGIN_ALLOW_THREADS |
| 6266 | n = readlink(path, buf, (int) sizeof buf); |
| 6267 | Py_END_ALLOW_THREADS |
| 6268 | if (n < 0) |
| 6269 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6270 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6271 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6272 | if (arg_is_unicode) |
| 6273 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 6274 | else |
| 6275 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6276 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6277 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6278 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6279 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6280 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6281 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6282 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6283 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6284 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6285 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6286 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6287 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6288 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6289 | } |
| 6290 | #endif /* HAVE_SYMLINK */ |
| 6291 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6292 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6293 | |
| 6294 | PyDoc_STRVAR(win_readlink__doc__, |
| 6295 | "readlink(path) -> path\n\n\ |
| 6296 | Return a string representing the path to which the symbolic link points."); |
| 6297 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6298 | /* Windows readlink implementation */ |
| 6299 | static PyObject * |
| 6300 | win_readlink(PyObject *self, PyObject *args) |
| 6301 | { |
| 6302 | wchar_t *path; |
| 6303 | DWORD n_bytes_returned; |
| 6304 | DWORD io_result; |
| 6305 | PyObject *result; |
| 6306 | HANDLE reparse_point_handle; |
| 6307 | |
| 6308 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6309 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6310 | wchar_t *print_name; |
| 6311 | |
| 6312 | if (!PyArg_ParseTuple(args, |
| 6313 | "u:readlink", |
| 6314 | &path)) |
| 6315 | return NULL; |
| 6316 | |
| 6317 | /* First get a handle to the reparse point */ |
| 6318 | Py_BEGIN_ALLOW_THREADS |
| 6319 | reparse_point_handle = CreateFileW( |
| 6320 | path, |
| 6321 | 0, |
| 6322 | 0, |
| 6323 | 0, |
| 6324 | OPEN_EXISTING, |
| 6325 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6326 | 0); |
| 6327 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6328 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6329 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 6330 | { |
| 6331 | return win32_error_unicode("readlink", path); |
| 6332 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6333 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6334 | Py_BEGIN_ALLOW_THREADS |
| 6335 | /* New call DeviceIoControl to read the reparse point */ |
| 6336 | io_result = DeviceIoControl( |
| 6337 | reparse_point_handle, |
| 6338 | FSCTL_GET_REPARSE_POINT, |
| 6339 | 0, 0, /* in buffer */ |
| 6340 | target_buffer, sizeof(target_buffer), |
| 6341 | &n_bytes_returned, |
| 6342 | 0 /* we're not using OVERLAPPED_IO */ |
| 6343 | ); |
| 6344 | CloseHandle(reparse_point_handle); |
| 6345 | Py_END_ALLOW_THREADS |
| 6346 | |
| 6347 | if (io_result==0) |
| 6348 | { |
| 6349 | return win32_error_unicode("readlink", path); |
| 6350 | } |
| 6351 | |
| 6352 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6353 | { |
| 6354 | PyErr_SetString(PyExc_ValueError, |
| 6355 | "not a symbolic link"); |
| 6356 | return NULL; |
| 6357 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6358 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6359 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6360 | |
| 6361 | result = PyUnicode_FromWideChar(print_name, |
| 6362 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6363 | return result; |
| 6364 | } |
| 6365 | |
| 6366 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6367 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6368 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6369 | |
| 6370 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6371 | static int has_CreateSymbolicLinkW = 0; |
| 6372 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 6373 | static int |
| 6374 | check_CreateSymbolicLinkW() |
| 6375 | { |
| 6376 | HINSTANCE hKernel32; |
| 6377 | /* only recheck */ |
| 6378 | if (has_CreateSymbolicLinkW) |
| 6379 | return has_CreateSymbolicLinkW; |
| 6380 | hKernel32 = GetModuleHandle("KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6381 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6382 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6383 | if (Py_CreateSymbolicLinkW) |
| 6384 | has_CreateSymbolicLinkW = 1; |
| 6385 | return has_CreateSymbolicLinkW; |
| 6386 | } |
| 6387 | |
| 6388 | PyDoc_STRVAR(win_symlink__doc__, |
| 6389 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 6390 | Create a symbolic link pointing to src named dst.\n\ |
| 6391 | target_is_directory is required if the target is to be interpreted as\n\ |
| 6392 | a directory.\n\ |
| 6393 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 6394 | NotImplementedError otherwise."); |
| 6395 | |
| 6396 | static PyObject * |
| 6397 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6398 | { |
| 6399 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
| 6400 | PyObject *src, *dest; |
| 6401 | int target_is_directory = 0; |
| 6402 | DWORD res; |
| 6403 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6404 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6405 | if (!check_CreateSymbolicLinkW()) |
| 6406 | { |
| 6407 | /* raise NotImplementedError */ |
| 6408 | return PyErr_Format(PyExc_NotImplementedError, |
| 6409 | "CreateSymbolicLinkW not found"); |
| 6410 | } |
| 6411 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink", |
| 6412 | kwlist, &src, &dest, &target_is_directory)) |
| 6413 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6414 | |
| 6415 | if (win32_can_symlink == 0) |
| 6416 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 6417 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6418 | if (!convert_to_unicode(&src)) { return NULL; } |
| 6419 | if (!convert_to_unicode(&dest)) { |
| 6420 | Py_DECREF(src); |
| 6421 | return NULL; |
| 6422 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6423 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6424 | /* if src is a directory, ensure target_is_directory==1 */ |
| 6425 | if( |
| 6426 | GetFileAttributesExW( |
| 6427 | PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info |
| 6428 | )) |
| 6429 | { |
| 6430 | target_is_directory = target_is_directory || |
| 6431 | (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 6432 | } |
| 6433 | |
| 6434 | Py_BEGIN_ALLOW_THREADS |
| 6435 | res = Py_CreateSymbolicLinkW( |
| 6436 | PyUnicode_AsUnicode(dest), |
| 6437 | PyUnicode_AsUnicode(src), |
| 6438 | target_is_directory); |
| 6439 | Py_END_ALLOW_THREADS |
| 6440 | Py_DECREF(src); |
| 6441 | Py_DECREF(dest); |
| 6442 | if (!res) |
| 6443 | { |
| 6444 | return win32_error_unicode("symlink", PyUnicode_AsUnicode(src)); |
| 6445 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6446 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6447 | Py_INCREF(Py_None); |
| 6448 | return Py_None; |
| 6449 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6450 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6451 | |
| 6452 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6453 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6454 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6455 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6456 | { |
| 6457 | ULONG value = 0; |
| 6458 | |
| 6459 | Py_BEGIN_ALLOW_THREADS |
| 6460 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6461 | Py_END_ALLOW_THREADS |
| 6462 | |
| 6463 | return value; |
| 6464 | } |
| 6465 | |
| 6466 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6467 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6468 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6469 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6470 | return Py_BuildValue("ddddd", |
| 6471 | (double)0 /* t.tms_utime / HZ */, |
| 6472 | (double)0 /* t.tms_stime / HZ */, |
| 6473 | (double)0 /* t.tms_cutime / HZ */, |
| 6474 | (double)0 /* t.tms_cstime / HZ */, |
| 6475 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6476 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6477 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 6478 | #define NEED_TICKS_PER_SECOND |
| 6479 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6480 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6481 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6482 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6483 | struct tms t; |
| 6484 | clock_t c; |
| 6485 | errno = 0; |
| 6486 | c = times(&t); |
| 6487 | if (c == (clock_t) -1) |
| 6488 | return posix_error(); |
| 6489 | return Py_BuildValue("ddddd", |
| 6490 | (double)t.tms_utime / ticks_per_second, |
| 6491 | (double)t.tms_stime / ticks_per_second, |
| 6492 | (double)t.tms_cutime / ticks_per_second, |
| 6493 | (double)t.tms_cstime / ticks_per_second, |
| 6494 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6495 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6496 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6497 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6498 | |
| 6499 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6500 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6501 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6502 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6503 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6504 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6505 | FILETIME create, exit, kernel, user; |
| 6506 | HANDLE hProc; |
| 6507 | hProc = GetCurrentProcess(); |
| 6508 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6509 | /* The fields of a FILETIME structure are the hi and lo part |
| 6510 | of a 64-bit value expressed in 100 nanosecond units. |
| 6511 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6512 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6513 | */ |
| 6514 | return Py_BuildValue( |
| 6515 | "ddddd", |
| 6516 | (double)(user.dwHighDateTime*429.4967296 + |
| 6517 | user.dwLowDateTime*1e-7), |
| 6518 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6519 | kernel.dwLowDateTime*1e-7), |
| 6520 | (double)0, |
| 6521 | (double)0, |
| 6522 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6523 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6524 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6525 | |
| 6526 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6527 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6528 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6529 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6530 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6531 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6532 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6533 | #ifdef HAVE_GETSID |
| 6534 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6535 | "getsid(pid) -> sid\n\n\ |
| 6536 | Call the system call getsid()."); |
| 6537 | |
| 6538 | static PyObject * |
| 6539 | posix_getsid(PyObject *self, PyObject *args) |
| 6540 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6541 | pid_t pid; |
| 6542 | int sid; |
| 6543 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 6544 | return NULL; |
| 6545 | sid = getsid(pid); |
| 6546 | if (sid < 0) |
| 6547 | return posix_error(); |
| 6548 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6549 | } |
| 6550 | #endif /* HAVE_GETSID */ |
| 6551 | |
| 6552 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6553 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6554 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6555 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6556 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6558 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6559 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6560 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6561 | if (setsid() < 0) |
| 6562 | return posix_error(); |
| 6563 | Py_INCREF(Py_None); |
| 6564 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6565 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6566 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6567 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6568 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6569 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6570 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6571 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6572 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6573 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6574 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6575 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6576 | pid_t pid; |
| 6577 | int pgrp; |
| 6578 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6579 | return NULL; |
| 6580 | if (setpgid(pid, pgrp) < 0) |
| 6581 | return posix_error(); |
| 6582 | Py_INCREF(Py_None); |
| 6583 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6584 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6585 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6586 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6587 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6588 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6589 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6590 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6591 | 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] | 6592 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6593 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6594 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6595 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6596 | int fd; |
| 6597 | pid_t pgid; |
| 6598 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6599 | return NULL; |
| 6600 | pgid = tcgetpgrp(fd); |
| 6601 | if (pgid < 0) |
| 6602 | return posix_error(); |
| 6603 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6604 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6605 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6606 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6607 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6608 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6609 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6610 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6611 | 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] | 6612 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6613 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6614 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6615 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6616 | int fd; |
| 6617 | pid_t pgid; |
| 6618 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6619 | return NULL; |
| 6620 | if (tcsetpgrp(fd, pgid) < 0) |
| 6621 | return posix_error(); |
| 6622 | Py_INCREF(Py_None); |
| 6623 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6624 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6625 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6626 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6627 | /* Functions acting on file descriptors */ |
| 6628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6629 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6630 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6631 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6632 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6633 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6634 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6635 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6636 | PyObject *ofile; |
| 6637 | char *file; |
| 6638 | int flag; |
| 6639 | int mode = 0777; |
| 6640 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6641 | |
| 6642 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6643 | PyUnicodeObject *po; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6644 | if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6645 | Py_BEGIN_ALLOW_THREADS |
| 6646 | /* PyUnicode_AS_UNICODE OK without thread |
| 6647 | lock as it is a simple dereference. */ |
| 6648 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 6649 | Py_END_ALLOW_THREADS |
| 6650 | if (fd < 0) |
| 6651 | return posix_error(); |
| 6652 | return PyLong_FromLong((long)fd); |
| 6653 | } |
| 6654 | /* Drop the argument parsing error as narrow strings |
| 6655 | are also valid. */ |
| 6656 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6657 | #endif |
| 6658 | |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6659 | if (!PyArg_ParseTuple(args, "O&i|i:open", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6660 | PyUnicode_FSConverter, &ofile, |
| 6661 | &flag, &mode)) |
| 6662 | return NULL; |
| 6663 | file = PyBytes_AsString(ofile); |
| 6664 | Py_BEGIN_ALLOW_THREADS |
| 6665 | fd = open(file, flag, mode); |
| 6666 | Py_END_ALLOW_THREADS |
| 6667 | if (fd < 0) |
| 6668 | return posix_error_with_allocated_filename(ofile); |
| 6669 | Py_DECREF(ofile); |
| 6670 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6671 | } |
| 6672 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6674 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6675 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6676 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6677 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6678 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6679 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6680 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6681 | int fd, res; |
| 6682 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6683 | return NULL; |
| 6684 | if (!_PyVerify_fd(fd)) |
| 6685 | return posix_error(); |
| 6686 | Py_BEGIN_ALLOW_THREADS |
| 6687 | res = close(fd); |
| 6688 | Py_END_ALLOW_THREADS |
| 6689 | if (res < 0) |
| 6690 | return posix_error(); |
| 6691 | Py_INCREF(Py_None); |
| 6692 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6695 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6696 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6697 | "closerange(fd_low, fd_high)\n\n\ |
| 6698 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6699 | |
| 6700 | static PyObject * |
| 6701 | posix_closerange(PyObject *self, PyObject *args) |
| 6702 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6703 | int fd_from, fd_to, i; |
| 6704 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6705 | return NULL; |
| 6706 | Py_BEGIN_ALLOW_THREADS |
| 6707 | for (i = fd_from; i < fd_to; i++) |
| 6708 | if (_PyVerify_fd(i)) |
| 6709 | close(i); |
| 6710 | Py_END_ALLOW_THREADS |
| 6711 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6712 | } |
| 6713 | |
| 6714 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6715 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6716 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6717 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6718 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6719 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6720 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6721 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6722 | int fd; |
| 6723 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6724 | return NULL; |
| 6725 | if (!_PyVerify_fd(fd)) |
| 6726 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6727 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6728 | if (fd < 0) |
| 6729 | return posix_error(); |
| 6730 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6734 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6735 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6736 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6737 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6738 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6739 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6740 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6741 | int fd, fd2, res; |
| 6742 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6743 | return NULL; |
| 6744 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6745 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6746 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6747 | if (res < 0) |
| 6748 | return posix_error(); |
| 6749 | Py_INCREF(Py_None); |
| 6750 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6753 | #ifdef HAVE_LOCKF |
| 6754 | PyDoc_STRVAR(posix_lockf__doc__, |
| 6755 | "lockf(fd, cmd, len)\n\n\ |
| 6756 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 6757 | fd is an open file descriptor.\n\ |
| 6758 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 6759 | F_TEST.\n\ |
| 6760 | len specifies the section of the file to lock."); |
| 6761 | |
| 6762 | static PyObject * |
| 6763 | posix_lockf(PyObject *self, PyObject *args) |
| 6764 | { |
| 6765 | int fd, cmd, res; |
| 6766 | off_t len; |
| 6767 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 6768 | &fd, &cmd, _parse_off_t, &len)) |
| 6769 | return NULL; |
| 6770 | |
| 6771 | Py_BEGIN_ALLOW_THREADS |
| 6772 | res = lockf(fd, cmd, len); |
| 6773 | Py_END_ALLOW_THREADS |
| 6774 | |
| 6775 | if (res < 0) |
| 6776 | return posix_error(); |
| 6777 | |
| 6778 | Py_RETURN_NONE; |
| 6779 | } |
| 6780 | #endif |
| 6781 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6782 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6783 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6784 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6785 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6786 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6787 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6788 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6789 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6790 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6791 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6792 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6793 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6794 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6795 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6796 | PyObject *posobj; |
| 6797 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6798 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6799 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6800 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6801 | switch (how) { |
| 6802 | case 0: how = SEEK_SET; break; |
| 6803 | case 1: how = SEEK_CUR; break; |
| 6804 | case 2: how = SEEK_END; break; |
| 6805 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6806 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6807 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6808 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6809 | pos = PyLong_AsLong(posobj); |
| 6810 | #else |
| 6811 | pos = PyLong_AsLongLong(posobj); |
| 6812 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6813 | if (PyErr_Occurred()) |
| 6814 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6815 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6816 | if (!_PyVerify_fd(fd)) |
| 6817 | return posix_error(); |
| 6818 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6819 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6820 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6821 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6822 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6823 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6824 | Py_END_ALLOW_THREADS |
| 6825 | if (res < 0) |
| 6826 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6827 | |
| 6828 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6829 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6830 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6831 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6832 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6833 | } |
| 6834 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6836 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6837 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6838 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6839 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6840 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6841 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6842 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6843 | int fd, size; |
| 6844 | Py_ssize_t n; |
| 6845 | PyObject *buffer; |
| 6846 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 6847 | return NULL; |
| 6848 | if (size < 0) { |
| 6849 | errno = EINVAL; |
| 6850 | return posix_error(); |
| 6851 | } |
| 6852 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 6853 | if (buffer == NULL) |
| 6854 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6855 | if (!_PyVerify_fd(fd)) { |
| 6856 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6857 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6858 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6859 | Py_BEGIN_ALLOW_THREADS |
| 6860 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 6861 | Py_END_ALLOW_THREADS |
| 6862 | if (n < 0) { |
| 6863 | Py_DECREF(buffer); |
| 6864 | return posix_error(); |
| 6865 | } |
| 6866 | if (n != size) |
| 6867 | _PyBytes_Resize(&buffer, n); |
| 6868 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6869 | } |
| 6870 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6871 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 6872 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6873 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6874 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 6875 | { |
| 6876 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6877 | Py_ssize_t blen, total = 0; |
| 6878 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6879 | *iov = PyMem_New(struct iovec, cnt); |
| 6880 | if (*iov == NULL) { |
| 6881 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6882 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6883 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6884 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6885 | *buf = PyMem_New(Py_buffer, cnt); |
| 6886 | if (*buf == NULL) { |
| 6887 | PyMem_Del(*iov); |
| 6888 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6889 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6890 | } |
| 6891 | |
| 6892 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6893 | PyObject *item = PySequence_GetItem(seq, i); |
| 6894 | if (item == NULL) |
| 6895 | goto fail; |
| 6896 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 6897 | Py_DECREF(item); |
| 6898 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6899 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6900 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6901 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6902 | blen = (*buf)[i].len; |
| 6903 | (*iov)[i].iov_len = blen; |
| 6904 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6905 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6906 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6907 | |
| 6908 | fail: |
| 6909 | PyMem_Del(*iov); |
| 6910 | for (j = 0; j < i; j++) { |
| 6911 | PyBuffer_Release(&(*buf)[j]); |
| 6912 | } |
| 6913 | PyMem_Del(*buf); |
| 6914 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6915 | } |
| 6916 | |
| 6917 | static void |
| 6918 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 6919 | { |
| 6920 | int i; |
| 6921 | PyMem_Del(iov); |
| 6922 | for (i = 0; i < cnt; i++) { |
| 6923 | PyBuffer_Release(&buf[i]); |
| 6924 | } |
| 6925 | PyMem_Del(buf); |
| 6926 | } |
| 6927 | #endif |
| 6928 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6929 | #ifdef HAVE_READV |
| 6930 | PyDoc_STRVAR(posix_readv__doc__, |
| 6931 | "readv(fd, buffers) -> bytesread\n\n\ |
| 6932 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 6933 | is an arbitrary sequence of writable buffers.\n\ |
| 6934 | Returns the total number of bytes read."); |
| 6935 | |
| 6936 | static PyObject * |
| 6937 | posix_readv(PyObject *self, PyObject *args) |
| 6938 | { |
| 6939 | int fd, cnt; |
| 6940 | Py_ssize_t n; |
| 6941 | PyObject *seq; |
| 6942 | struct iovec *iov; |
| 6943 | Py_buffer *buf; |
| 6944 | |
| 6945 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 6946 | return NULL; |
| 6947 | if (!PySequence_Check(seq)) { |
| 6948 | PyErr_SetString(PyExc_TypeError, |
| 6949 | "readv() arg 2 must be a sequence"); |
| 6950 | return NULL; |
| 6951 | } |
| 6952 | cnt = PySequence_Size(seq); |
| 6953 | |
| 6954 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 6955 | return NULL; |
| 6956 | |
| 6957 | Py_BEGIN_ALLOW_THREADS |
| 6958 | n = readv(fd, iov, cnt); |
| 6959 | Py_END_ALLOW_THREADS |
| 6960 | |
| 6961 | iov_cleanup(iov, buf, cnt); |
| 6962 | return PyLong_FromSsize_t(n); |
| 6963 | } |
| 6964 | #endif |
| 6965 | |
| 6966 | #ifdef HAVE_PREAD |
| 6967 | PyDoc_STRVAR(posix_pread__doc__, |
| 6968 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 6969 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 6970 | to buffersize number of bytes. The file offset remains unchanged."); |
| 6971 | |
| 6972 | static PyObject * |
| 6973 | posix_pread(PyObject *self, PyObject *args) |
| 6974 | { |
| 6975 | int fd, size; |
| 6976 | off_t offset; |
| 6977 | Py_ssize_t n; |
| 6978 | PyObject *buffer; |
| 6979 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 6980 | return NULL; |
| 6981 | |
| 6982 | if (size < 0) { |
| 6983 | errno = EINVAL; |
| 6984 | return posix_error(); |
| 6985 | } |
| 6986 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 6987 | if (buffer == NULL) |
| 6988 | return NULL; |
| 6989 | if (!_PyVerify_fd(fd)) { |
| 6990 | Py_DECREF(buffer); |
| 6991 | return posix_error(); |
| 6992 | } |
| 6993 | Py_BEGIN_ALLOW_THREADS |
| 6994 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 6995 | Py_END_ALLOW_THREADS |
| 6996 | if (n < 0) { |
| 6997 | Py_DECREF(buffer); |
| 6998 | return posix_error(); |
| 6999 | } |
| 7000 | if (n != size) |
| 7001 | _PyBytes_Resize(&buffer, n); |
| 7002 | return buffer; |
| 7003 | } |
| 7004 | #endif |
| 7005 | |
| 7006 | PyDoc_STRVAR(posix_write__doc__, |
| 7007 | "write(fd, string) -> byteswritten\n\n\ |
| 7008 | Write a string to a file descriptor."); |
| 7009 | |
| 7010 | static PyObject * |
| 7011 | posix_write(PyObject *self, PyObject *args) |
| 7012 | { |
| 7013 | Py_buffer pbuf; |
| 7014 | int fd; |
| 7015 | Py_ssize_t size, len; |
| 7016 | |
| 7017 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7018 | return NULL; |
| 7019 | if (!_PyVerify_fd(fd)) { |
| 7020 | PyBuffer_Release(&pbuf); |
| 7021 | return posix_error(); |
| 7022 | } |
| 7023 | len = pbuf.len; |
| 7024 | Py_BEGIN_ALLOW_THREADS |
| 7025 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7026 | if (len > INT_MAX) |
| 7027 | len = INT_MAX; |
| 7028 | size = write(fd, pbuf.buf, (int)len); |
| 7029 | #else |
| 7030 | size = write(fd, pbuf.buf, len); |
| 7031 | #endif |
| 7032 | Py_END_ALLOW_THREADS |
| 7033 | PyBuffer_Release(&pbuf); |
| 7034 | if (size < 0) |
| 7035 | return posix_error(); |
| 7036 | return PyLong_FromSsize_t(size); |
| 7037 | } |
| 7038 | |
| 7039 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7040 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7041 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7042 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7043 | -> byteswritten\n\ |
| 7044 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7045 | |
| 7046 | static PyObject * |
| 7047 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7048 | { |
| 7049 | int in, out; |
| 7050 | Py_ssize_t ret; |
| 7051 | off_t offset; |
| 7052 | |
| 7053 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7054 | #ifndef __APPLE__ |
| 7055 | Py_ssize_t len; |
| 7056 | #endif |
| 7057 | PyObject *headers = NULL, *trailers = NULL; |
| 7058 | Py_buffer *hbuf, *tbuf; |
| 7059 | off_t sbytes; |
| 7060 | struct sf_hdtr sf; |
| 7061 | int flags = 0; |
| 7062 | sf.headers = NULL; |
| 7063 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7064 | static char *keywords[] = {"out", "in", |
| 7065 | "offset", "count", |
| 7066 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7067 | |
| 7068 | #ifdef __APPLE__ |
| 7069 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7070 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7071 | #else |
| 7072 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7073 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7074 | #endif |
| 7075 | &headers, &trailers, &flags)) |
| 7076 | return NULL; |
| 7077 | if (headers != NULL) { |
| 7078 | if (!PySequence_Check(headers)) { |
| 7079 | PyErr_SetString(PyExc_TypeError, |
| 7080 | "sendfile() headers must be a sequence or None"); |
| 7081 | return NULL; |
| 7082 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7083 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7084 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7085 | if (sf.hdr_cnt > 0 && |
| 7086 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7087 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7088 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7089 | #ifdef __APPLE__ |
| 7090 | sbytes += i; |
| 7091 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7092 | } |
| 7093 | } |
| 7094 | if (trailers != NULL) { |
| 7095 | if (!PySequence_Check(trailers)) { |
| 7096 | PyErr_SetString(PyExc_TypeError, |
| 7097 | "sendfile() trailers must be a sequence or None"); |
| 7098 | return NULL; |
| 7099 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7100 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7101 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7102 | if (sf.trl_cnt > 0 && |
| 7103 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7104 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7105 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7106 | #ifdef __APPLE__ |
| 7107 | sbytes += i; |
| 7108 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7109 | } |
| 7110 | } |
| 7111 | |
| 7112 | Py_BEGIN_ALLOW_THREADS |
| 7113 | #ifdef __APPLE__ |
| 7114 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7115 | #else |
| 7116 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7117 | #endif |
| 7118 | Py_END_ALLOW_THREADS |
| 7119 | |
| 7120 | if (sf.headers != NULL) |
| 7121 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7122 | if (sf.trailers != NULL) |
| 7123 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7124 | |
| 7125 | if (ret < 0) { |
| 7126 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7127 | if (sbytes != 0) { |
| 7128 | // some data has been sent |
| 7129 | goto done; |
| 7130 | } |
| 7131 | else { |
| 7132 | // no data has been sent; upper application is supposed |
| 7133 | // to retry on EAGAIN or EBUSY |
| 7134 | return posix_error(); |
| 7135 | } |
| 7136 | } |
| 7137 | return posix_error(); |
| 7138 | } |
| 7139 | goto done; |
| 7140 | |
| 7141 | done: |
| 7142 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7143 | return Py_BuildValue("l", sbytes); |
| 7144 | #else |
| 7145 | return Py_BuildValue("L", sbytes); |
| 7146 | #endif |
| 7147 | |
| 7148 | #else |
| 7149 | Py_ssize_t count; |
| 7150 | PyObject *offobj; |
| 7151 | static char *keywords[] = {"out", "in", |
| 7152 | "offset", "count", NULL}; |
| 7153 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7154 | keywords, &out, &in, &offobj, &count)) |
| 7155 | return NULL; |
| 7156 | #ifdef linux |
| 7157 | if (offobj == Py_None) { |
| 7158 | Py_BEGIN_ALLOW_THREADS |
| 7159 | ret = sendfile(out, in, NULL, count); |
| 7160 | Py_END_ALLOW_THREADS |
| 7161 | if (ret < 0) |
| 7162 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7163 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7164 | } |
| 7165 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7166 | if (!_parse_off_t(offobj, &offset)) |
| 7167 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7168 | Py_BEGIN_ALLOW_THREADS |
| 7169 | ret = sendfile(out, in, &offset, count); |
| 7170 | Py_END_ALLOW_THREADS |
| 7171 | if (ret < 0) |
| 7172 | return posix_error(); |
| 7173 | return Py_BuildValue("n", ret); |
| 7174 | #endif |
| 7175 | } |
| 7176 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7178 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7179 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7180 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7181 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7182 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7183 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7184 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7185 | int fd; |
| 7186 | STRUCT_STAT st; |
| 7187 | int res; |
| 7188 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 7189 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7190 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7191 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7192 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7193 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7194 | if (!_PyVerify_fd(fd)) |
| 7195 | return posix_error(); |
| 7196 | Py_BEGIN_ALLOW_THREADS |
| 7197 | res = FSTAT(fd, &st); |
| 7198 | Py_END_ALLOW_THREADS |
| 7199 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7200 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7201 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7202 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7203 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7204 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7205 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7206 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7207 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7208 | } |
| 7209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7210 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7211 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7212 | 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] | 7213 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7214 | |
| 7215 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7216 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7217 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7218 | int fd; |
| 7219 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7220 | return NULL; |
| 7221 | if (!_PyVerify_fd(fd)) |
| 7222 | return PyBool_FromLong(0); |
| 7223 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7224 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7225 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7226 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7227 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7228 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7229 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7230 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7231 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7232 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7233 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7234 | #if defined(PYOS_OS2) |
| 7235 | HFILE read, write; |
| 7236 | APIRET rc; |
| 7237 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7238 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7239 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7240 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7241 | |
| 7242 | return Py_BuildValue("(ii)", read, write); |
| 7243 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7244 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7245 | int fds[2]; |
| 7246 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7247 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7248 | if (res != 0) |
| 7249 | return posix_error(); |
| 7250 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7251 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7252 | HANDLE read, write; |
| 7253 | int read_fd, write_fd; |
| 7254 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7255 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7256 | if (!ok) |
| 7257 | return win32_error("CreatePipe", NULL); |
| 7258 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7259 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7260 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7261 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7262 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7263 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7264 | #endif /* HAVE_PIPE */ |
| 7265 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7266 | #ifdef HAVE_PIPE2 |
| 7267 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7268 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7269 | Create a pipe with flags set atomically.\n\ |
| 7270 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7271 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7272 | "); |
| 7273 | |
| 7274 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7275 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7276 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7277 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7278 | int fds[2]; |
| 7279 | int res; |
| 7280 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7281 | flags = PyLong_AsLong(arg); |
| 7282 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7283 | return NULL; |
| 7284 | |
| 7285 | res = pipe2(fds, flags); |
| 7286 | if (res != 0) |
| 7287 | return posix_error(); |
| 7288 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7289 | } |
| 7290 | #endif /* HAVE_PIPE2 */ |
| 7291 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7292 | #ifdef HAVE_WRITEV |
| 7293 | PyDoc_STRVAR(posix_writev__doc__, |
| 7294 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7295 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7296 | arbitrary sequence of buffers.\n\ |
| 7297 | Returns the total bytes written."); |
| 7298 | |
| 7299 | static PyObject * |
| 7300 | posix_writev(PyObject *self, PyObject *args) |
| 7301 | { |
| 7302 | int fd, cnt; |
| 7303 | Py_ssize_t res; |
| 7304 | PyObject *seq; |
| 7305 | struct iovec *iov; |
| 7306 | Py_buffer *buf; |
| 7307 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7308 | return NULL; |
| 7309 | if (!PySequence_Check(seq)) { |
| 7310 | PyErr_SetString(PyExc_TypeError, |
| 7311 | "writev() arg 2 must be a sequence"); |
| 7312 | return NULL; |
| 7313 | } |
| 7314 | cnt = PySequence_Size(seq); |
| 7315 | |
| 7316 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7317 | return NULL; |
| 7318 | } |
| 7319 | |
| 7320 | Py_BEGIN_ALLOW_THREADS |
| 7321 | res = writev(fd, iov, cnt); |
| 7322 | Py_END_ALLOW_THREADS |
| 7323 | |
| 7324 | iov_cleanup(iov, buf, cnt); |
| 7325 | return PyLong_FromSsize_t(res); |
| 7326 | } |
| 7327 | #endif |
| 7328 | |
| 7329 | #ifdef HAVE_PWRITE |
| 7330 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7331 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7332 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7333 | offset unchanged."); |
| 7334 | |
| 7335 | static PyObject * |
| 7336 | posix_pwrite(PyObject *self, PyObject *args) |
| 7337 | { |
| 7338 | Py_buffer pbuf; |
| 7339 | int fd; |
| 7340 | off_t offset; |
| 7341 | Py_ssize_t size; |
| 7342 | |
| 7343 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7344 | return NULL; |
| 7345 | |
| 7346 | if (!_PyVerify_fd(fd)) { |
| 7347 | PyBuffer_Release(&pbuf); |
| 7348 | return posix_error(); |
| 7349 | } |
| 7350 | Py_BEGIN_ALLOW_THREADS |
| 7351 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7352 | Py_END_ALLOW_THREADS |
| 7353 | PyBuffer_Release(&pbuf); |
| 7354 | if (size < 0) |
| 7355 | return posix_error(); |
| 7356 | return PyLong_FromSsize_t(size); |
| 7357 | } |
| 7358 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7359 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7360 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7361 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7362 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7363 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7364 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7365 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7366 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7367 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7368 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7369 | char *filename; |
| 7370 | int mode = 0666; |
| 7371 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7372 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 7373 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7374 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7375 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7376 | Py_BEGIN_ALLOW_THREADS |
| 7377 | res = mkfifo(filename, mode); |
| 7378 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7379 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7380 | if (res < 0) |
| 7381 | return posix_error(); |
| 7382 | Py_INCREF(Py_None); |
| 7383 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7384 | } |
| 7385 | #endif |
| 7386 | |
| 7387 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7388 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7389 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7390 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7391 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7392 | named filename. mode specifies both the permissions to use and the\n\ |
| 7393 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7394 | 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] | 7395 | device defines the newly created device special file (probably using\n\ |
| 7396 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7397 | |
| 7398 | |
| 7399 | static PyObject * |
| 7400 | posix_mknod(PyObject *self, PyObject *args) |
| 7401 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7402 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7403 | char *filename; |
| 7404 | int mode = 0600; |
| 7405 | int device = 0; |
| 7406 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7407 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 7408 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7409 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7410 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7411 | Py_BEGIN_ALLOW_THREADS |
| 7412 | res = mknod(filename, mode, device); |
| 7413 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7414 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7415 | if (res < 0) |
| 7416 | return posix_error(); |
| 7417 | Py_INCREF(Py_None); |
| 7418 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7419 | } |
| 7420 | #endif |
| 7421 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7422 | #ifdef HAVE_DEVICE_MACROS |
| 7423 | PyDoc_STRVAR(posix_major__doc__, |
| 7424 | "major(device) -> major number\n\ |
| 7425 | Extracts a device major number from a raw device number."); |
| 7426 | |
| 7427 | static PyObject * |
| 7428 | posix_major(PyObject *self, PyObject *args) |
| 7429 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7430 | int device; |
| 7431 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7432 | return NULL; |
| 7433 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7434 | } |
| 7435 | |
| 7436 | PyDoc_STRVAR(posix_minor__doc__, |
| 7437 | "minor(device) -> minor number\n\ |
| 7438 | Extracts a device minor number from a raw device number."); |
| 7439 | |
| 7440 | static PyObject * |
| 7441 | posix_minor(PyObject *self, PyObject *args) |
| 7442 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7443 | int device; |
| 7444 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7445 | return NULL; |
| 7446 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7447 | } |
| 7448 | |
| 7449 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7450 | "makedev(major, minor) -> device number\n\ |
| 7451 | Composes a raw device number from the major and minor device numbers."); |
| 7452 | |
| 7453 | static PyObject * |
| 7454 | posix_makedev(PyObject *self, PyObject *args) |
| 7455 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7456 | int major, minor; |
| 7457 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7458 | return NULL; |
| 7459 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7460 | } |
| 7461 | #endif /* device macros */ |
| 7462 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7463 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7464 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7465 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7466 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7467 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7468 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7469 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7470 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7471 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7472 | int fd; |
| 7473 | off_t length; |
| 7474 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7475 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7476 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7477 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7478 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7479 | Py_BEGIN_ALLOW_THREADS |
| 7480 | res = ftruncate(fd, length); |
| 7481 | Py_END_ALLOW_THREADS |
| 7482 | if (res < 0) |
| 7483 | return posix_error(); |
| 7484 | Py_INCREF(Py_None); |
| 7485 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7486 | } |
| 7487 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7488 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7489 | #ifdef HAVE_TRUNCATE |
| 7490 | PyDoc_STRVAR(posix_truncate__doc__, |
| 7491 | "truncate(path, length)\n\n\ |
| 7492 | Truncate the file given by path to length bytes."); |
| 7493 | |
| 7494 | static PyObject * |
| 7495 | posix_truncate(PyObject *self, PyObject *args) |
| 7496 | { |
| 7497 | PyObject *opath; |
| 7498 | const char *path; |
| 7499 | off_t length; |
| 7500 | int res; |
| 7501 | |
| 7502 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 7503 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 7504 | return NULL; |
| 7505 | path = PyBytes_AsString(opath); |
| 7506 | |
| 7507 | Py_BEGIN_ALLOW_THREADS |
| 7508 | res = truncate(path, length); |
| 7509 | Py_END_ALLOW_THREADS |
| 7510 | Py_DECREF(opath); |
| 7511 | if (res < 0) |
| 7512 | return posix_error(); |
| 7513 | Py_RETURN_NONE; |
| 7514 | } |
| 7515 | #endif |
| 7516 | |
| 7517 | #ifdef HAVE_POSIX_FALLOCATE |
| 7518 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 7519 | "posix_fallocate(fd, offset, len)\n\n\ |
| 7520 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 7521 | starting from offset and continuing for len bytes."); |
| 7522 | |
| 7523 | static PyObject * |
| 7524 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 7525 | { |
| 7526 | off_t len, offset; |
| 7527 | int res, fd; |
| 7528 | |
| 7529 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 7530 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 7531 | return NULL; |
| 7532 | |
| 7533 | Py_BEGIN_ALLOW_THREADS |
| 7534 | res = posix_fallocate(fd, offset, len); |
| 7535 | Py_END_ALLOW_THREADS |
| 7536 | if (res != 0) { |
| 7537 | errno = res; |
| 7538 | return posix_error(); |
| 7539 | } |
| 7540 | Py_RETURN_NONE; |
| 7541 | } |
| 7542 | #endif |
| 7543 | |
| 7544 | #ifdef HAVE_POSIX_FADVISE |
| 7545 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 7546 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 7547 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 7548 | the kernel to make optimizations.\n\ |
| 7549 | The advice applies to the region of the file specified by fd starting at\n\ |
| 7550 | offset and continuing for len bytes.\n\ |
| 7551 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 7552 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 7553 | POSIX_FADV_DONTNEED."); |
| 7554 | |
| 7555 | static PyObject * |
| 7556 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 7557 | { |
| 7558 | off_t len, offset; |
| 7559 | int res, fd, advice; |
| 7560 | |
| 7561 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 7562 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 7563 | return NULL; |
| 7564 | |
| 7565 | Py_BEGIN_ALLOW_THREADS |
| 7566 | res = posix_fadvise(fd, offset, len, advice); |
| 7567 | Py_END_ALLOW_THREADS |
| 7568 | if (res != 0) { |
| 7569 | errno = res; |
| 7570 | return posix_error(); |
| 7571 | } |
| 7572 | Py_RETURN_NONE; |
| 7573 | } |
| 7574 | #endif |
| 7575 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7576 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7577 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7578 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7579 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7580 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7581 | /* Save putenv() parameters as values here, so we can collect them when they |
| 7582 | * get re-set with another call for the same key. */ |
| 7583 | static PyObject *posix_putenv_garbage; |
| 7584 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7585 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7586 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7587 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7588 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7589 | wchar_t *s1, *s2; |
| 7590 | wchar_t *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7591 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7592 | PyObject *os1, *os2; |
| 7593 | char *s1, *s2; |
| 7594 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7595 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7596 | PyObject *newstr = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7597 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7598 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7599 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7600 | if (!PyArg_ParseTuple(args, |
| 7601 | "uu:putenv", |
| 7602 | &s1, &s2)) |
| 7603 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7604 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7605 | if (!PyArg_ParseTuple(args, |
| 7606 | "O&O&:putenv", |
| 7607 | PyUnicode_FSConverter, &os1, |
| 7608 | PyUnicode_FSConverter, &os2)) |
| 7609 | return NULL; |
| 7610 | s1 = PyBytes_AsString(os1); |
| 7611 | s2 = PyBytes_AsString(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7612 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7613 | |
| 7614 | #if defined(PYOS_OS2) |
| 7615 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 7616 | APIRET rc; |
| 7617 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7618 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7619 | if (rc != NO_ERROR) { |
| 7620 | os2_error(rc); |
| 7621 | goto error; |
| 7622 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7623 | |
| 7624 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 7625 | APIRET rc; |
| 7626 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7627 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7628 | if (rc != NO_ERROR) { |
| 7629 | os2_error(rc); |
| 7630 | goto error; |
| 7631 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7632 | } else { |
| 7633 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7634 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 7635 | /* len includes space for a trailing \0; the size arg to |
| 7636 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7637 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7638 | len = wcslen(s1) + wcslen(s2) + 2; |
| 7639 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7640 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7641 | len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7642 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7643 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7644 | if (newstr == NULL) { |
| 7645 | PyErr_NoMemory(); |
| 7646 | goto error; |
| 7647 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7648 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7649 | newenv = PyUnicode_AsUnicode(newstr); |
| 7650 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 7651 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7652 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7653 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7654 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7655 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7656 | newenv = PyBytes_AS_STRING(newstr); |
| 7657 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 7658 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7659 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7660 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7661 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7662 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7663 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7664 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 7665 | * this will cause previous value to be collected. This has to |
| 7666 | * happen after the real putenv() call because the old value |
| 7667 | * was still accessible until then. */ |
| 7668 | if (PyDict_SetItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7669 | #ifdef MS_WINDOWS |
| 7670 | PyTuple_GET_ITEM(args, 0), |
| 7671 | #else |
| 7672 | os1, |
| 7673 | #endif |
| 7674 | newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7675 | /* really not much we can do; just leak */ |
| 7676 | PyErr_Clear(); |
| 7677 | } |
| 7678 | else { |
| 7679 | Py_DECREF(newstr); |
| 7680 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7681 | |
| 7682 | #if defined(PYOS_OS2) |
| 7683 | } |
| 7684 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7685 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7686 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7687 | Py_DECREF(os1); |
| 7688 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7689 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7690 | Py_RETURN_NONE; |
| 7691 | |
| 7692 | error: |
| 7693 | #ifndef MS_WINDOWS |
| 7694 | Py_DECREF(os1); |
| 7695 | Py_DECREF(os2); |
| 7696 | #endif |
| 7697 | Py_XDECREF(newstr); |
| 7698 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7699 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7700 | #endif /* putenv */ |
| 7701 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7702 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7703 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7704 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7705 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7706 | |
| 7707 | static PyObject * |
| 7708 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7709 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7710 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7711 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7712 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7713 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 7714 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7715 | #else |
| 7716 | PyObject *os1; |
| 7717 | char *s1; |
| 7718 | |
| 7719 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
| 7720 | PyUnicode_FSConverter, &os1)) |
| 7721 | return NULL; |
| 7722 | s1 = PyBytes_AsString(os1); |
| 7723 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7724 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7725 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7726 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7727 | /* Remove the key from posix_putenv_garbage; |
| 7728 | * this will cause it to be collected. This has to |
| 7729 | * happen after the real unsetenv() call because the |
| 7730 | * old value was still accessible until then. |
| 7731 | */ |
| 7732 | if (PyDict_DelItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7733 | #ifdef MS_WINDOWS |
| 7734 | PyTuple_GET_ITEM(args, 0) |
| 7735 | #else |
| 7736 | os1 |
| 7737 | #endif |
| 7738 | )) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7739 | /* really not much we can do; just leak */ |
| 7740 | PyErr_Clear(); |
| 7741 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7742 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7743 | #ifndef MS_WINDOWS |
| 7744 | Py_DECREF(os1); |
| 7745 | #endif |
| 7746 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7747 | } |
| 7748 | #endif /* unsetenv */ |
| 7749 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7750 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7751 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7752 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7753 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7754 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7755 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7756 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7757 | int code; |
| 7758 | char *message; |
| 7759 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7760 | return NULL; |
| 7761 | message = strerror(code); |
| 7762 | if (message == NULL) { |
| 7763 | PyErr_SetString(PyExc_ValueError, |
| 7764 | "strerror() argument out of range"); |
| 7765 | return NULL; |
| 7766 | } |
| 7767 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7768 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7769 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7770 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7771 | #ifdef HAVE_SYS_WAIT_H |
| 7772 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7773 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7774 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7775 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7776 | 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] | 7777 | |
| 7778 | static PyObject * |
| 7779 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7780 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7781 | WAIT_TYPE status; |
| 7782 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7783 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7785 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7786 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7787 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7788 | } |
| 7789 | #endif /* WCOREDUMP */ |
| 7790 | |
| 7791 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7792 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7793 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7794 | 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] | 7795 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7796 | |
| 7797 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7798 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7799 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7800 | WAIT_TYPE status; |
| 7801 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7802 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7803 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7804 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7806 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7807 | } |
| 7808 | #endif /* WIFCONTINUED */ |
| 7809 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7810 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7811 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7812 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7813 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7814 | |
| 7815 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7816 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7817 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7818 | WAIT_TYPE status; |
| 7819 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7821 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7822 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7823 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7824 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7825 | } |
| 7826 | #endif /* WIFSTOPPED */ |
| 7827 | |
| 7828 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7829 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7830 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7831 | 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] | 7832 | |
| 7833 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7834 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7835 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7836 | WAIT_TYPE status; |
| 7837 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7838 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7839 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7840 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7841 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7842 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7843 | } |
| 7844 | #endif /* WIFSIGNALED */ |
| 7845 | |
| 7846 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7847 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7848 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7849 | 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] | 7850 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7851 | |
| 7852 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7853 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7854 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7855 | WAIT_TYPE status; |
| 7856 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7857 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7858 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7859 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7860 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7861 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7862 | } |
| 7863 | #endif /* WIFEXITED */ |
| 7864 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7865 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7866 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7867 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7868 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7869 | |
| 7870 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7871 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7872 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7873 | WAIT_TYPE status; |
| 7874 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7875 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7876 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 7877 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7878 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7879 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7880 | } |
| 7881 | #endif /* WEXITSTATUS */ |
| 7882 | |
| 7883 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7884 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7885 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7886 | 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] | 7887 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7888 | |
| 7889 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7890 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7891 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7892 | WAIT_TYPE status; |
| 7893 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7894 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7895 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 7896 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7897 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7898 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7899 | } |
| 7900 | #endif /* WTERMSIG */ |
| 7901 | |
| 7902 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7903 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7904 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7905 | Return the signal that stopped the process that provided\n\ |
| 7906 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7907 | |
| 7908 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7909 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7910 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7911 | WAIT_TYPE status; |
| 7912 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7913 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7914 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 7915 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7916 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7917 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7918 | } |
| 7919 | #endif /* WSTOPSIG */ |
| 7920 | |
| 7921 | #endif /* HAVE_SYS_WAIT_H */ |
| 7922 | |
| 7923 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7924 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 7925 | #ifdef _SCO_DS |
| 7926 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 7927 | needed definitions in sys/statvfs.h */ |
| 7928 | #define _SVID3 |
| 7929 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7930 | #include <sys/statvfs.h> |
| 7931 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7932 | static PyObject* |
| 7933 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7934 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 7935 | if (v == NULL) |
| 7936 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7937 | |
| 7938 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7939 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 7940 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 7941 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 7942 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 7943 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 7944 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 7945 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 7946 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 7947 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 7948 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7949 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7950 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 7951 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 7952 | PyStructSequence_SET_ITEM(v, 2, |
| 7953 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 7954 | PyStructSequence_SET_ITEM(v, 3, |
| 7955 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 7956 | PyStructSequence_SET_ITEM(v, 4, |
| 7957 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 7958 | PyStructSequence_SET_ITEM(v, 5, |
| 7959 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 7960 | PyStructSequence_SET_ITEM(v, 6, |
| 7961 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 7962 | PyStructSequence_SET_ITEM(v, 7, |
| 7963 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 7964 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 7965 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7966 | #endif |
| 7967 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7968 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7969 | } |
| 7970 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7971 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7972 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7973 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7974 | |
| 7975 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7976 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7977 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7978 | int fd, res; |
| 7979 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7980 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7981 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 7982 | return NULL; |
| 7983 | Py_BEGIN_ALLOW_THREADS |
| 7984 | res = fstatvfs(fd, &st); |
| 7985 | Py_END_ALLOW_THREADS |
| 7986 | if (res != 0) |
| 7987 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7988 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7989 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7990 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7991 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7992 | |
| 7993 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7994 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7995 | #include <sys/statvfs.h> |
| 7996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7997 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7998 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7999 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8000 | |
| 8001 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8002 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8003 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8004 | char *path; |
| 8005 | int res; |
| 8006 | struct statvfs st; |
| 8007 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
| 8008 | return NULL; |
| 8009 | Py_BEGIN_ALLOW_THREADS |
| 8010 | res = statvfs(path, &st); |
| 8011 | Py_END_ALLOW_THREADS |
| 8012 | if (res != 0) |
| 8013 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8014 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8015 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8016 | } |
| 8017 | #endif /* HAVE_STATVFS */ |
| 8018 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8019 | #ifdef MS_WINDOWS |
| 8020 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8021 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8022 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8023 | |
| 8024 | static PyObject * |
| 8025 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8026 | { |
| 8027 | BOOL retval; |
| 8028 | ULARGE_INTEGER _, total, free; |
| 8029 | LPCTSTR path; |
| 8030 | |
| 8031 | if (! PyArg_ParseTuple(args, "s", &path)) |
| 8032 | return NULL; |
| 8033 | |
| 8034 | Py_BEGIN_ALLOW_THREADS |
| 8035 | retval = GetDiskFreeSpaceEx(path, &_, &total, &free); |
| 8036 | Py_END_ALLOW_THREADS |
| 8037 | if (retval == 0) |
| 8038 | return PyErr_SetFromWindowsErr(0); |
| 8039 | |
| 8040 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8041 | } |
| 8042 | #endif |
| 8043 | |
| 8044 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8045 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8046 | * It maps strings representing configuration variable names to |
| 8047 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8048 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8049 | * rarely-used constants. There are three separate tables that use |
| 8050 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8051 | * |
| 8052 | * This code is always included, even if none of the interfaces that |
| 8053 | * need it are included. The #if hackery needed to avoid it would be |
| 8054 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8055 | */ |
| 8056 | struct constdef { |
| 8057 | char *name; |
| 8058 | long value; |
| 8059 | }; |
| 8060 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8061 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8062 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8063 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8064 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8065 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8066 | *valuep = PyLong_AS_LONG(arg); |
| 8067 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8068 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8069 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8070 | /* look up the value in the table using a binary search */ |
| 8071 | size_t lo = 0; |
| 8072 | size_t mid; |
| 8073 | size_t hi = tablesize; |
| 8074 | int cmp; |
| 8075 | const char *confname; |
| 8076 | if (!PyUnicode_Check(arg)) { |
| 8077 | PyErr_SetString(PyExc_TypeError, |
| 8078 | "configuration names must be strings or integers"); |
| 8079 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8080 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8081 | confname = _PyUnicode_AsString(arg); |
| 8082 | if (confname == NULL) |
| 8083 | return 0; |
| 8084 | while (lo < hi) { |
| 8085 | mid = (lo + hi) / 2; |
| 8086 | cmp = strcmp(confname, table[mid].name); |
| 8087 | if (cmp < 0) |
| 8088 | hi = mid; |
| 8089 | else if (cmp > 0) |
| 8090 | lo = mid + 1; |
| 8091 | else { |
| 8092 | *valuep = table[mid].value; |
| 8093 | return 1; |
| 8094 | } |
| 8095 | } |
| 8096 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8097 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8098 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8099 | } |
| 8100 | |
| 8101 | |
| 8102 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8103 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8104 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8105 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8106 | #endif |
| 8107 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8108 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8109 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8110 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8111 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8112 | #endif |
| 8113 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8114 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8115 | #endif |
| 8116 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8117 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8118 | #endif |
| 8119 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8120 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8121 | #endif |
| 8122 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8123 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8124 | #endif |
| 8125 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8126 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8127 | #endif |
| 8128 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8129 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8130 | #endif |
| 8131 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8132 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8133 | #endif |
| 8134 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8135 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8136 | #endif |
| 8137 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8138 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8139 | #endif |
| 8140 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8141 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8142 | #endif |
| 8143 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8144 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8145 | #endif |
| 8146 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8147 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8148 | #endif |
| 8149 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8150 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8151 | #endif |
| 8152 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8153 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8154 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8155 | #ifdef _PC_ACL_ENABLED |
| 8156 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8157 | #endif |
| 8158 | #ifdef _PC_MIN_HOLE_SIZE |
| 8159 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8160 | #endif |
| 8161 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8162 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8163 | #endif |
| 8164 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8165 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8166 | #endif |
| 8167 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8168 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8169 | #endif |
| 8170 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8171 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8172 | #endif |
| 8173 | #ifdef _PC_REC_XFER_ALIGN |
| 8174 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8175 | #endif |
| 8176 | #ifdef _PC_SYMLINK_MAX |
| 8177 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8178 | #endif |
| 8179 | #ifdef _PC_XATTR_ENABLED |
| 8180 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8181 | #endif |
| 8182 | #ifdef _PC_XATTR_EXISTS |
| 8183 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8184 | #endif |
| 8185 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8186 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8187 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8188 | }; |
| 8189 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8190 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8191 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8192 | { |
| 8193 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8194 | sizeof(posix_constants_pathconf) |
| 8195 | / sizeof(struct constdef)); |
| 8196 | } |
| 8197 | #endif |
| 8198 | |
| 8199 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8200 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8201 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8202 | 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] | 8203 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8204 | |
| 8205 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8206 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8207 | { |
| 8208 | PyObject *result = NULL; |
| 8209 | int name, fd; |
| 8210 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8211 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8212 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8213 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8214 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8215 | errno = 0; |
| 8216 | limit = fpathconf(fd, name); |
| 8217 | if (limit == -1 && errno != 0) |
| 8218 | posix_error(); |
| 8219 | else |
| 8220 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8221 | } |
| 8222 | return result; |
| 8223 | } |
| 8224 | #endif |
| 8225 | |
| 8226 | |
| 8227 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8228 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8229 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8230 | 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] | 8231 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8232 | |
| 8233 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8234 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8235 | { |
| 8236 | PyObject *result = NULL; |
| 8237 | int name; |
| 8238 | char *path; |
| 8239 | |
| 8240 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 8241 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8242 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8243 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8244 | errno = 0; |
| 8245 | limit = pathconf(path, name); |
| 8246 | if (limit == -1 && errno != 0) { |
| 8247 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8248 | /* could be a path or name problem */ |
| 8249 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8250 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8251 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8252 | } |
| 8253 | else |
| 8254 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8255 | } |
| 8256 | return result; |
| 8257 | } |
| 8258 | #endif |
| 8259 | |
| 8260 | #ifdef HAVE_CONFSTR |
| 8261 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8262 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8263 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8264 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8265 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8266 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8267 | #endif |
| 8268 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8269 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8270 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8271 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8272 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8273 | #endif |
| 8274 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8275 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8276 | #endif |
| 8277 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8278 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8279 | #endif |
| 8280 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8281 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8282 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8283 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8284 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8285 | #endif |
| 8286 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8287 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8288 | #endif |
| 8289 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8290 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8291 | #endif |
| 8292 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8293 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8294 | #endif |
| 8295 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8296 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8297 | #endif |
| 8298 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8299 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8300 | #endif |
| 8301 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8302 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8303 | #endif |
| 8304 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8305 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8306 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8307 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8308 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8309 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8310 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8311 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8312 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8313 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8314 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8315 | #endif |
| 8316 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8317 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8318 | #endif |
| 8319 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8320 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8321 | #endif |
| 8322 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8323 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8324 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8325 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8326 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8327 | #endif |
| 8328 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8329 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8330 | #endif |
| 8331 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8332 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8333 | #endif |
| 8334 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8335 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8336 | #endif |
| 8337 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8338 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8339 | #endif |
| 8340 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8341 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8342 | #endif |
| 8343 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8344 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8345 | #endif |
| 8346 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8347 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8348 | #endif |
| 8349 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8350 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8351 | #endif |
| 8352 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8353 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8354 | #endif |
| 8355 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8356 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8357 | #endif |
| 8358 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8359 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8360 | #endif |
| 8361 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8362 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8363 | #endif |
| 8364 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8365 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8366 | #endif |
| 8367 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8368 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8369 | #endif |
| 8370 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8371 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8372 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8373 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8374 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8375 | #endif |
| 8376 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8377 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8378 | #endif |
| 8379 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8380 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8381 | #endif |
| 8382 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8383 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8384 | #endif |
| 8385 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8386 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8387 | #endif |
| 8388 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8389 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8390 | #endif |
| 8391 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8392 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8393 | #endif |
| 8394 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8395 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8396 | #endif |
| 8397 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8398 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8399 | #endif |
| 8400 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8401 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8402 | #endif |
| 8403 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8404 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8405 | #endif |
| 8406 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8407 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8408 | #endif |
| 8409 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8410 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8411 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8412 | }; |
| 8413 | |
| 8414 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8415 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8416 | { |
| 8417 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8418 | sizeof(posix_constants_confstr) |
| 8419 | / sizeof(struct constdef)); |
| 8420 | } |
| 8421 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8422 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8423 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8424 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8425 | |
| 8426 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8427 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8428 | { |
| 8429 | PyObject *result = NULL; |
| 8430 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8431 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8432 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8433 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8434 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8435 | return NULL; |
| 8436 | |
| 8437 | errno = 0; |
| 8438 | len = confstr(name, buffer, sizeof(buffer)); |
| 8439 | if (len == 0) { |
| 8440 | if (errno) { |
| 8441 | posix_error(); |
| 8442 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8443 | } |
| 8444 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8445 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8446 | } |
| 8447 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8448 | |
| 8449 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8450 | char *buf = PyMem_Malloc(len); |
| 8451 | if (buf == NULL) |
| 8452 | return PyErr_NoMemory(); |
| 8453 | confstr(name, buf, len); |
| 8454 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8455 | PyMem_Free(buf); |
| 8456 | } |
| 8457 | else |
| 8458 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8459 | return result; |
| 8460 | } |
| 8461 | #endif |
| 8462 | |
| 8463 | |
| 8464 | #ifdef HAVE_SYSCONF |
| 8465 | static struct constdef posix_constants_sysconf[] = { |
| 8466 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8467 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8468 | #endif |
| 8469 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8470 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8471 | #endif |
| 8472 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8473 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8474 | #endif |
| 8475 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8476 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8477 | #endif |
| 8478 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8479 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8480 | #endif |
| 8481 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8482 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8483 | #endif |
| 8484 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8485 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8486 | #endif |
| 8487 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8488 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8489 | #endif |
| 8490 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8491 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8492 | #endif |
| 8493 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8494 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8495 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8496 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8497 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8498 | #endif |
| 8499 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8500 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8501 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8502 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8503 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8504 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8505 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8506 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8507 | #endif |
| 8508 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8509 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8510 | #endif |
| 8511 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8512 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8513 | #endif |
| 8514 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8515 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8516 | #endif |
| 8517 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8518 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8519 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8520 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8521 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8522 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8523 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8524 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8525 | #endif |
| 8526 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8527 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8528 | #endif |
| 8529 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8530 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8531 | #endif |
| 8532 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8533 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8534 | #endif |
| 8535 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8536 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8537 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8538 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8539 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8540 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8541 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8542 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8543 | #endif |
| 8544 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8545 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8546 | #endif |
| 8547 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8548 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8549 | #endif |
| 8550 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8551 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8552 | #endif |
| 8553 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8554 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8555 | #endif |
| 8556 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8557 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8558 | #endif |
| 8559 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8560 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8561 | #endif |
| 8562 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8563 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8564 | #endif |
| 8565 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8566 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8567 | #endif |
| 8568 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8569 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8570 | #endif |
| 8571 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8572 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8573 | #endif |
| 8574 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8575 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8576 | #endif |
| 8577 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8578 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8579 | #endif |
| 8580 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8581 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8582 | #endif |
| 8583 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8584 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8585 | #endif |
| 8586 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8587 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8588 | #endif |
| 8589 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8590 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8591 | #endif |
| 8592 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8593 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8594 | #endif |
| 8595 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8596 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8597 | #endif |
| 8598 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8599 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8600 | #endif |
| 8601 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8602 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8603 | #endif |
| 8604 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8605 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8606 | #endif |
| 8607 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8608 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8609 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8610 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8611 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8612 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8613 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8614 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8615 | #endif |
| 8616 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8617 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8618 | #endif |
| 8619 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8620 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8621 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8622 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8623 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8624 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8625 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8626 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8627 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8628 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8629 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8630 | #endif |
| 8631 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8632 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8633 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8634 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8635 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8636 | #endif |
| 8637 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8638 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8639 | #endif |
| 8640 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8641 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8642 | #endif |
| 8643 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8644 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8645 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8646 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8647 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8648 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8649 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8650 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8651 | #endif |
| 8652 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8653 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8654 | #endif |
| 8655 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8656 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8657 | #endif |
| 8658 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8659 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8660 | #endif |
| 8661 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8662 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8663 | #endif |
| 8664 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8665 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8666 | #endif |
| 8667 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8668 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8669 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8670 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8671 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8672 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8673 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8674 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8675 | #endif |
| 8676 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8677 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8678 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8679 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8680 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8681 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8682 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8683 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8684 | #endif |
| 8685 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8686 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8687 | #endif |
| 8688 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8689 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8690 | #endif |
| 8691 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8692 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8693 | #endif |
| 8694 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8695 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8696 | #endif |
| 8697 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8698 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8699 | #endif |
| 8700 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8701 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8702 | #endif |
| 8703 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8704 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8705 | #endif |
| 8706 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8707 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8708 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8709 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8710 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8711 | #endif |
| 8712 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8713 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8714 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8715 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8716 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8717 | #endif |
| 8718 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8719 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8720 | #endif |
| 8721 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8722 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8723 | #endif |
| 8724 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8725 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8726 | #endif |
| 8727 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8728 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8729 | #endif |
| 8730 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8731 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8732 | #endif |
| 8733 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8734 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8735 | #endif |
| 8736 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8737 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8738 | #endif |
| 8739 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8740 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8741 | #endif |
| 8742 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8743 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8744 | #endif |
| 8745 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8746 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8747 | #endif |
| 8748 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8749 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8750 | #endif |
| 8751 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8752 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8753 | #endif |
| 8754 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8755 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8756 | #endif |
| 8757 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8758 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8759 | #endif |
| 8760 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8761 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8762 | #endif |
| 8763 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8764 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8765 | #endif |
| 8766 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8767 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8768 | #endif |
| 8769 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8770 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8771 | #endif |
| 8772 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8773 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8774 | #endif |
| 8775 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8776 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8777 | #endif |
| 8778 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8779 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8780 | #endif |
| 8781 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8782 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8783 | #endif |
| 8784 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8785 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8786 | #endif |
| 8787 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8788 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8789 | #endif |
| 8790 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8791 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8792 | #endif |
| 8793 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8794 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8795 | #endif |
| 8796 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8797 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8798 | #endif |
| 8799 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8800 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8801 | #endif |
| 8802 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8803 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8804 | #endif |
| 8805 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8806 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8807 | #endif |
| 8808 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8809 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8810 | #endif |
| 8811 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8812 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8813 | #endif |
| 8814 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8815 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8816 | #endif |
| 8817 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8818 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8819 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8820 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8821 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8822 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8823 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8824 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8825 | #endif |
| 8826 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8827 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8828 | #endif |
| 8829 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8830 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8831 | #endif |
| 8832 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8833 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8834 | #endif |
| 8835 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8836 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8837 | #endif |
| 8838 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8839 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8840 | #endif |
| 8841 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8842 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8843 | #endif |
| 8844 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8845 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8846 | #endif |
| 8847 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8848 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8849 | #endif |
| 8850 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8851 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8852 | #endif |
| 8853 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8854 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8855 | #endif |
| 8856 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8857 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8858 | #endif |
| 8859 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8860 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8861 | #endif |
| 8862 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8863 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8864 | #endif |
| 8865 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8866 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8867 | #endif |
| 8868 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8869 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8870 | #endif |
| 8871 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8872 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8873 | #endif |
| 8874 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8875 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8876 | #endif |
| 8877 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8878 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8879 | #endif |
| 8880 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8881 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8882 | #endif |
| 8883 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8884 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8885 | #endif |
| 8886 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8887 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8888 | #endif |
| 8889 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8890 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8891 | #endif |
| 8892 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8893 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8894 | #endif |
| 8895 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8896 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8897 | #endif |
| 8898 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8899 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8900 | #endif |
| 8901 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8902 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8903 | #endif |
| 8904 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8905 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8906 | #endif |
| 8907 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8908 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8909 | #endif |
| 8910 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8911 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8912 | #endif |
| 8913 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8914 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8915 | #endif |
| 8916 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8917 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8918 | #endif |
| 8919 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8920 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8921 | #endif |
| 8922 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8923 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8924 | #endif |
| 8925 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8926 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8927 | #endif |
| 8928 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8929 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8930 | #endif |
| 8931 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8932 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8933 | #endif |
| 8934 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8935 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8936 | #endif |
| 8937 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8938 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8939 | #endif |
| 8940 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8941 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8942 | #endif |
| 8943 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8944 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8945 | #endif |
| 8946 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8947 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8948 | #endif |
| 8949 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8950 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8951 | #endif |
| 8952 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8953 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8954 | #endif |
| 8955 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8956 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8957 | #endif |
| 8958 | }; |
| 8959 | |
| 8960 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8961 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8962 | { |
| 8963 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 8964 | sizeof(posix_constants_sysconf) |
| 8965 | / sizeof(struct constdef)); |
| 8966 | } |
| 8967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8968 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8969 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8970 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8971 | |
| 8972 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8973 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8974 | { |
| 8975 | PyObject *result = NULL; |
| 8976 | int name; |
| 8977 | |
| 8978 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 8979 | int value; |
| 8980 | |
| 8981 | errno = 0; |
| 8982 | value = sysconf(name); |
| 8983 | if (value == -1 && errno != 0) |
| 8984 | posix_error(); |
| 8985 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8986 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8987 | } |
| 8988 | return result; |
| 8989 | } |
| 8990 | #endif |
| 8991 | |
| 8992 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8993 | /* This code is used to ensure that the tables of configuration value names |
| 8994 | * are in sorted order as required by conv_confname(), and also to build the |
| 8995 | * the exported dictionaries that are used to publish information about the |
| 8996 | * names available on the host platform. |
| 8997 | * |
| 8998 | * Sorting the table at runtime ensures that the table is properly ordered |
| 8999 | * when used, even for platforms we're not able to test on. It also makes |
| 9000 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9001 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9002 | |
| 9003 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9004 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9005 | { |
| 9006 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9007 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9008 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9009 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9010 | |
| 9011 | return strcmp(c1->name, c2->name); |
| 9012 | } |
| 9013 | |
| 9014 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9015 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9016 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9017 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9018 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9019 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9020 | |
| 9021 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9022 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9023 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9024 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9025 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9026 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9027 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9028 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9029 | Py_XDECREF(o); |
| 9030 | Py_DECREF(d); |
| 9031 | return -1; |
| 9032 | } |
| 9033 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9034 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9035 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9036 | } |
| 9037 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9038 | /* Return -1 on failure, 0 on success. */ |
| 9039 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9040 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9041 | { |
| 9042 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9043 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9044 | sizeof(posix_constants_pathconf) |
| 9045 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9046 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9047 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9048 | #endif |
| 9049 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9050 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9051 | sizeof(posix_constants_confstr) |
| 9052 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9053 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9054 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9055 | #endif |
| 9056 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9057 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9058 | sizeof(posix_constants_sysconf) |
| 9059 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9060 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9061 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9062 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9063 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9064 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9065 | |
| 9066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9067 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9068 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9069 | 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] | 9070 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9071 | |
| 9072 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9073 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9074 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9075 | abort(); |
| 9076 | /*NOTREACHED*/ |
| 9077 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9078 | return NULL; |
| 9079 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9080 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9081 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9082 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9083 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9084 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9085 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9086 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9087 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9088 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9089 | application (if any) its extension is associated.\n\ |
| 9090 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9091 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9092 | \n\ |
| 9093 | startfile returns as soon as the associated application is launched.\n\ |
| 9094 | There is no option to wait for the application to close, and no way\n\ |
| 9095 | to retrieve the application's exit status.\n\ |
| 9096 | \n\ |
| 9097 | The filepath is relative to the current directory. If you want to use\n\ |
| 9098 | 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] | 9099 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9100 | |
| 9101 | static PyObject * |
| 9102 | win32_startfile(PyObject *self, PyObject *args) |
| 9103 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9104 | PyObject *ofilepath; |
| 9105 | char *filepath; |
| 9106 | char *operation = NULL; |
| 9107 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9108 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9109 | PyObject *unipath, *woperation = NULL; |
| 9110 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9111 | &unipath, &operation)) { |
| 9112 | PyErr_Clear(); |
| 9113 | goto normal; |
| 9114 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9115 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9116 | if (operation) { |
| 9117 | woperation = PyUnicode_DecodeASCII(operation, |
| 9118 | strlen(operation), NULL); |
| 9119 | if (!woperation) { |
| 9120 | PyErr_Clear(); |
| 9121 | operation = NULL; |
| 9122 | goto normal; |
| 9123 | } |
| 9124 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9125 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9126 | Py_BEGIN_ALLOW_THREADS |
| 9127 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 9128 | PyUnicode_AS_UNICODE(unipath), |
| 9129 | NULL, NULL, SW_SHOWNORMAL); |
| 9130 | Py_END_ALLOW_THREADS |
| 9131 | |
| 9132 | Py_XDECREF(woperation); |
| 9133 | if (rc <= (HINSTANCE)32) { |
| 9134 | PyObject *errval = win32_error_unicode("startfile", |
| 9135 | PyUnicode_AS_UNICODE(unipath)); |
| 9136 | return errval; |
| 9137 | } |
| 9138 | Py_INCREF(Py_None); |
| 9139 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9140 | |
| 9141 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9142 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9143 | PyUnicode_FSConverter, &ofilepath, |
| 9144 | &operation)) |
| 9145 | return NULL; |
| 9146 | filepath = PyBytes_AsString(ofilepath); |
| 9147 | Py_BEGIN_ALLOW_THREADS |
| 9148 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9149 | NULL, NULL, SW_SHOWNORMAL); |
| 9150 | Py_END_ALLOW_THREADS |
| 9151 | if (rc <= (HINSTANCE)32) { |
| 9152 | PyObject *errval = win32_error("startfile", filepath); |
| 9153 | Py_DECREF(ofilepath); |
| 9154 | return errval; |
| 9155 | } |
| 9156 | Py_DECREF(ofilepath); |
| 9157 | Py_INCREF(Py_None); |
| 9158 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9159 | } |
| 9160 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9161 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9162 | #ifdef HAVE_GETLOADAVG |
| 9163 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9164 | "getloadavg() -> (float, float, float)\n\n\ |
| 9165 | Return the number of processes in the system run queue averaged over\n\ |
| 9166 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9167 | was unobtainable"); |
| 9168 | |
| 9169 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9170 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9171 | { |
| 9172 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9173 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9174 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9175 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9176 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9177 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9178 | } |
| 9179 | #endif |
| 9180 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9181 | #ifdef MS_WINDOWS |
| 9182 | |
| 9183 | PyDoc_STRVAR(win32_urandom__doc__, |
| 9184 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9185 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9186 | |
| 9187 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 9188 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 9189 | DWORD dwFlags ); |
| 9190 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 9191 | BYTE *pbBuffer ); |
| 9192 | |
| 9193 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 9194 | /* This handle is never explicitly released. Instead, the operating |
| 9195 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9196 | static HCRYPTPROV hCryptProv = 0; |
| 9197 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 9198 | static PyObject* |
| 9199 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9200 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9201 | int howMany; |
| 9202 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9203 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9204 | /* Read arguments */ |
| 9205 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9206 | return NULL; |
| 9207 | if (howMany < 0) |
| 9208 | return PyErr_Format(PyExc_ValueError, |
| 9209 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9210 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9211 | if (hCryptProv == 0) { |
| 9212 | HINSTANCE hAdvAPI32 = NULL; |
| 9213 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9214 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9215 | /* Obtain handle to the DLL containing CryptoAPI |
| 9216 | This should not fail */ |
| 9217 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 9218 | if(hAdvAPI32 == NULL) |
| 9219 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9221 | /* Obtain pointers to the CryptoAPI functions |
| 9222 | This will fail on some early versions of Win95 */ |
| 9223 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 9224 | hAdvAPI32, |
| 9225 | "CryptAcquireContextA"); |
| 9226 | if (pCryptAcquireContext == NULL) |
| 9227 | return PyErr_Format(PyExc_NotImplementedError, |
| 9228 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9229 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9230 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 9231 | hAdvAPI32, "CryptGenRandom"); |
| 9232 | if (pCryptGenRandom == NULL) |
| 9233 | return PyErr_Format(PyExc_NotImplementedError, |
| 9234 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9235 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9236 | /* Acquire context */ |
| 9237 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 9238 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 9239 | return win32_error("CryptAcquireContext", NULL); |
| 9240 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9241 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9242 | /* Allocate bytes */ |
| 9243 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9244 | if (result != NULL) { |
| 9245 | /* Get random data */ |
| 9246 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 9247 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 9248 | PyBytes_AS_STRING(result))) { |
| 9249 | Py_DECREF(result); |
| 9250 | return win32_error("CryptGenRandom", NULL); |
| 9251 | } |
| 9252 | } |
| 9253 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9254 | } |
| 9255 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9256 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9257 | PyDoc_STRVAR(device_encoding__doc__, |
| 9258 | "device_encoding(fd) -> str\n\n\ |
| 9259 | Return a string describing the encoding of the device\n\ |
| 9260 | if the output is a terminal; else return None."); |
| 9261 | |
| 9262 | static PyObject * |
| 9263 | device_encoding(PyObject *self, PyObject *args) |
| 9264 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9265 | int fd; |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9266 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 9267 | UINT cp; |
| 9268 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9269 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9270 | return NULL; |
| 9271 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 9272 | Py_INCREF(Py_None); |
| 9273 | return Py_None; |
| 9274 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9275 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9276 | if (fd == 0) |
| 9277 | cp = GetConsoleCP(); |
| 9278 | else if (fd == 1 || fd == 2) |
| 9279 | cp = GetConsoleOutputCP(); |
| 9280 | else |
| 9281 | cp = 0; |
| 9282 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 9283 | has no console */ |
| 9284 | if (cp != 0) |
| 9285 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9286 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9287 | { |
| 9288 | char *codeset = nl_langinfo(CODESET); |
| 9289 | if (codeset != NULL && codeset[0] != 0) |
| 9290 | return PyUnicode_FromString(codeset); |
| 9291 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9292 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9293 | Py_INCREF(Py_None); |
| 9294 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9295 | } |
| 9296 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9297 | #ifdef __VMS |
| 9298 | /* Use openssl random routine */ |
| 9299 | #include <openssl/rand.h> |
| 9300 | PyDoc_STRVAR(vms_urandom__doc__, |
| 9301 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9302 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9303 | |
| 9304 | static PyObject* |
| 9305 | vms_urandom(PyObject *self, PyObject *args) |
| 9306 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9307 | int howMany; |
| 9308 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9309 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9310 | /* Read arguments */ |
| 9311 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9312 | return NULL; |
| 9313 | if (howMany < 0) |
| 9314 | return PyErr_Format(PyExc_ValueError, |
| 9315 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9316 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9317 | /* Allocate bytes */ |
| 9318 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9319 | if (result != NULL) { |
| 9320 | /* Get random data */ |
| 9321 | if (RAND_pseudo_bytes((unsigned char*) |
| 9322 | PyBytes_AS_STRING(result), |
| 9323 | howMany) < 0) { |
| 9324 | Py_DECREF(result); |
| 9325 | return PyErr_Format(PyExc_ValueError, |
| 9326 | "RAND_pseudo_bytes"); |
| 9327 | } |
| 9328 | } |
| 9329 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9330 | } |
| 9331 | #endif |
| 9332 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9333 | #ifdef HAVE_SETRESUID |
| 9334 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9335 | "setresuid(ruid, euid, suid)\n\n\ |
| 9336 | Set the current process's real, effective, and saved user ids."); |
| 9337 | |
| 9338 | static PyObject* |
| 9339 | posix_setresuid (PyObject *self, PyObject *args) |
| 9340 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9341 | /* We assume uid_t is no larger than a long. */ |
| 9342 | long ruid, euid, suid; |
| 9343 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9344 | return NULL; |
| 9345 | if (setresuid(ruid, euid, suid) < 0) |
| 9346 | return posix_error(); |
| 9347 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9348 | } |
| 9349 | #endif |
| 9350 | |
| 9351 | #ifdef HAVE_SETRESGID |
| 9352 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9353 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9354 | Set the current process's real, effective, and saved group ids."); |
| 9355 | |
| 9356 | static PyObject* |
| 9357 | posix_setresgid (PyObject *self, PyObject *args) |
| 9358 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9359 | /* We assume uid_t is no larger than a long. */ |
| 9360 | long rgid, egid, sgid; |
| 9361 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9362 | return NULL; |
| 9363 | if (setresgid(rgid, egid, sgid) < 0) |
| 9364 | return posix_error(); |
| 9365 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9366 | } |
| 9367 | #endif |
| 9368 | |
| 9369 | #ifdef HAVE_GETRESUID |
| 9370 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9371 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9372 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9373 | |
| 9374 | static PyObject* |
| 9375 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9376 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9377 | uid_t ruid, euid, suid; |
| 9378 | long l_ruid, l_euid, l_suid; |
| 9379 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9380 | return posix_error(); |
| 9381 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9382 | l_ruid = ruid; |
| 9383 | l_euid = euid; |
| 9384 | l_suid = suid; |
| 9385 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9386 | } |
| 9387 | #endif |
| 9388 | |
| 9389 | #ifdef HAVE_GETRESGID |
| 9390 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9391 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9392 | 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] | 9393 | |
| 9394 | static PyObject* |
| 9395 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9396 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9397 | uid_t rgid, egid, sgid; |
| 9398 | long l_rgid, l_egid, l_sgid; |
| 9399 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9400 | return posix_error(); |
| 9401 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9402 | l_rgid = rgid; |
| 9403 | l_egid = egid; |
| 9404 | l_sgid = sgid; |
| 9405 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9406 | } |
| 9407 | #endif |
| 9408 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9409 | /* Posix *at family of functions: |
| 9410 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 9411 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 9412 | unlinkat, utimensat, mkfifoat */ |
| 9413 | |
| 9414 | #ifdef HAVE_FACCESSAT |
| 9415 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 9416 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 9417 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9418 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 9419 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 9420 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9421 | is interpreted relative to the current working directory."); |
| 9422 | |
| 9423 | static PyObject * |
| 9424 | posix_faccessat(PyObject *self, PyObject *args) |
| 9425 | { |
| 9426 | PyObject *opath; |
| 9427 | char *path; |
| 9428 | int mode; |
| 9429 | int res; |
| 9430 | int dirfd, flags = 0; |
| 9431 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 9432 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9433 | return NULL; |
| 9434 | path = PyBytes_AsString(opath); |
| 9435 | Py_BEGIN_ALLOW_THREADS |
| 9436 | res = faccessat(dirfd, path, mode, flags); |
| 9437 | Py_END_ALLOW_THREADS |
| 9438 | Py_DECREF(opath); |
| 9439 | return PyBool_FromLong(res == 0); |
| 9440 | } |
| 9441 | #endif |
| 9442 | |
| 9443 | #ifdef HAVE_FCHMODAT |
| 9444 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 9445 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 9446 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9447 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9448 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9449 | is interpreted relative to the current working directory."); |
| 9450 | |
| 9451 | static PyObject * |
| 9452 | posix_fchmodat(PyObject *self, PyObject *args) |
| 9453 | { |
| 9454 | int dirfd, mode, res; |
| 9455 | int flags = 0; |
| 9456 | PyObject *opath; |
| 9457 | char *path; |
| 9458 | |
| 9459 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 9460 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9461 | return NULL; |
| 9462 | |
| 9463 | path = PyBytes_AsString(opath); |
| 9464 | |
| 9465 | Py_BEGIN_ALLOW_THREADS |
| 9466 | res = fchmodat(dirfd, path, mode, flags); |
| 9467 | Py_END_ALLOW_THREADS |
| 9468 | Py_DECREF(opath); |
| 9469 | if (res < 0) |
| 9470 | return posix_error(); |
| 9471 | Py_RETURN_NONE; |
| 9472 | } |
| 9473 | #endif /* HAVE_FCHMODAT */ |
| 9474 | |
| 9475 | #ifdef HAVE_FCHOWNAT |
| 9476 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 9477 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 9478 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9479 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9480 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9481 | is interpreted relative to the current working directory."); |
| 9482 | |
| 9483 | static PyObject * |
| 9484 | posix_fchownat(PyObject *self, PyObject *args) |
| 9485 | { |
| 9486 | PyObject *opath; |
| 9487 | int dirfd, res; |
| 9488 | long uid, gid; |
| 9489 | int flags = 0; |
| 9490 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9491 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9492 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 9493 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 9494 | return NULL; |
| 9495 | |
| 9496 | path = PyBytes_AsString(opath); |
| 9497 | |
| 9498 | Py_BEGIN_ALLOW_THREADS |
| 9499 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 9500 | Py_END_ALLOW_THREADS |
| 9501 | Py_DECREF(opath); |
| 9502 | if (res < 0) |
| 9503 | return posix_error(); |
| 9504 | Py_RETURN_NONE; |
| 9505 | } |
| 9506 | #endif /* HAVE_FCHOWNAT */ |
| 9507 | |
| 9508 | #ifdef HAVE_FSTATAT |
| 9509 | PyDoc_STRVAR(posix_fstatat__doc__, |
| 9510 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
| 9511 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9512 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9513 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9514 | is interpreted relative to the current working directory."); |
| 9515 | |
| 9516 | static PyObject * |
| 9517 | posix_fstatat(PyObject *self, PyObject *args) |
| 9518 | { |
| 9519 | PyObject *opath; |
| 9520 | char *path; |
| 9521 | STRUCT_STAT st; |
| 9522 | int dirfd, res, flags = 0; |
| 9523 | |
| 9524 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 9525 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9526 | return NULL; |
| 9527 | path = PyBytes_AsString(opath); |
| 9528 | |
| 9529 | Py_BEGIN_ALLOW_THREADS |
| 9530 | res = fstatat(dirfd, path, &st, flags); |
| 9531 | Py_END_ALLOW_THREADS |
| 9532 | Py_DECREF(opath); |
| 9533 | if (res != 0) |
| 9534 | return posix_error(); |
| 9535 | |
| 9536 | return _pystat_fromstructstat(&st); |
| 9537 | } |
| 9538 | #endif |
| 9539 | |
| 9540 | #ifdef HAVE_FUTIMESAT |
| 9541 | PyDoc_STRVAR(posix_futimesat__doc__, |
| 9542 | "futimesat(dirfd, path, (atime, mtime))\n\ |
| 9543 | futimesat(dirfd, path, None)\n\n\ |
| 9544 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9545 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9546 | is interpreted relative to the current working directory."); |
| 9547 | |
| 9548 | static PyObject * |
| 9549 | posix_futimesat(PyObject *self, PyObject *args) |
| 9550 | { |
| 9551 | PyObject *opath; |
| 9552 | char *path; |
| 9553 | int res, dirfd; |
| 9554 | PyObject* arg; |
| 9555 | |
| 9556 | struct timeval buf[2]; |
| 9557 | |
| 9558 | if (!PyArg_ParseTuple(args, "iO&O:futimesat", |
| 9559 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 9560 | return NULL; |
| 9561 | path = PyBytes_AsString(opath); |
| 9562 | if (arg == Py_None) { |
| 9563 | /* optional time values not given */ |
| 9564 | Py_BEGIN_ALLOW_THREADS |
| 9565 | res = futimesat(dirfd, path, NULL); |
| 9566 | Py_END_ALLOW_THREADS |
| 9567 | } |
| 9568 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 9569 | PyErr_SetString(PyExc_TypeError, |
| 9570 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 9571 | Py_DECREF(opath); |
| 9572 | return NULL; |
| 9573 | } |
| 9574 | else { |
| 9575 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Victor Stinner | 6ee7a57 | 2011-06-17 15:18:16 +0200 | [diff] [blame] | 9576 | &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9577 | Py_DECREF(opath); |
| 9578 | return NULL; |
| 9579 | } |
| 9580 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Victor Stinner | 6ee7a57 | 2011-06-17 15:18:16 +0200 | [diff] [blame] | 9581 | &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9582 | Py_DECREF(opath); |
| 9583 | return NULL; |
| 9584 | } |
| 9585 | Py_BEGIN_ALLOW_THREADS |
| 9586 | res = futimesat(dirfd, path, buf); |
| 9587 | Py_END_ALLOW_THREADS |
| 9588 | } |
| 9589 | Py_DECREF(opath); |
| 9590 | if (res < 0) { |
| 9591 | return posix_error(); |
| 9592 | } |
| 9593 | Py_RETURN_NONE; |
| 9594 | } |
| 9595 | #endif |
| 9596 | |
| 9597 | #ifdef HAVE_LINKAT |
| 9598 | PyDoc_STRVAR(posix_linkat__doc__, |
| 9599 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 9600 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 9601 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 9602 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 9603 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 9604 | srcpath is interpreted relative to the current working directory. This\n\ |
| 9605 | also applies for dstpath."); |
| 9606 | |
| 9607 | static PyObject * |
| 9608 | posix_linkat(PyObject *self, PyObject *args) |
| 9609 | { |
| 9610 | PyObject *osrc, *odst; |
| 9611 | char *src, *dst; |
| 9612 | int res, srcfd, dstfd; |
| 9613 | int flags = 0; |
| 9614 | |
| 9615 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 9616 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 9617 | return NULL; |
| 9618 | src = PyBytes_AsString(osrc); |
| 9619 | dst = PyBytes_AsString(odst); |
| 9620 | Py_BEGIN_ALLOW_THREADS |
| 9621 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 9622 | Py_END_ALLOW_THREADS |
| 9623 | Py_DECREF(osrc); |
| 9624 | Py_DECREF(odst); |
| 9625 | if (res < 0) |
| 9626 | return posix_error(); |
| 9627 | Py_RETURN_NONE; |
| 9628 | } |
| 9629 | #endif /* HAVE_LINKAT */ |
| 9630 | |
| 9631 | #ifdef HAVE_MKDIRAT |
| 9632 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 9633 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 9634 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9635 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9636 | is interpreted relative to the current working directory."); |
| 9637 | |
| 9638 | static PyObject * |
| 9639 | posix_mkdirat(PyObject *self, PyObject *args) |
| 9640 | { |
| 9641 | int res, dirfd; |
| 9642 | PyObject *opath; |
| 9643 | char *path; |
| 9644 | int mode = 0777; |
| 9645 | |
| 9646 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 9647 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9648 | return NULL; |
| 9649 | path = PyBytes_AsString(opath); |
| 9650 | Py_BEGIN_ALLOW_THREADS |
| 9651 | res = mkdirat(dirfd, path, mode); |
| 9652 | Py_END_ALLOW_THREADS |
| 9653 | Py_DECREF(opath); |
| 9654 | if (res < 0) |
| 9655 | return posix_error(); |
| 9656 | Py_RETURN_NONE; |
| 9657 | } |
| 9658 | #endif |
| 9659 | |
| 9660 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 9661 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 9662 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 9663 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9664 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9665 | is interpreted relative to the current working directory."); |
| 9666 | |
| 9667 | static PyObject * |
| 9668 | posix_mknodat(PyObject *self, PyObject *args) |
| 9669 | { |
| 9670 | PyObject *opath; |
| 9671 | char *filename; |
| 9672 | int mode = 0600; |
| 9673 | int device = 0; |
| 9674 | int res, dirfd; |
| 9675 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 9676 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 9677 | return NULL; |
| 9678 | filename = PyBytes_AS_STRING(opath); |
| 9679 | Py_BEGIN_ALLOW_THREADS |
| 9680 | res = mknodat(dirfd, filename, mode, device); |
| 9681 | Py_END_ALLOW_THREADS |
| 9682 | Py_DECREF(opath); |
| 9683 | if (res < 0) |
| 9684 | return posix_error(); |
| 9685 | Py_RETURN_NONE; |
| 9686 | } |
| 9687 | #endif |
| 9688 | |
| 9689 | #ifdef HAVE_OPENAT |
| 9690 | PyDoc_STRVAR(posix_openat__doc__, |
| 9691 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 9692 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9693 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9694 | is interpreted relative to the current working directory."); |
| 9695 | |
| 9696 | static PyObject * |
| 9697 | posix_openat(PyObject *self, PyObject *args) |
| 9698 | { |
| 9699 | PyObject *ofile; |
| 9700 | char *file; |
| 9701 | int flag, dirfd, fd; |
| 9702 | int mode = 0777; |
| 9703 | |
| 9704 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 9705 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 9706 | &flag, &mode)) |
| 9707 | return NULL; |
| 9708 | file = PyBytes_AsString(ofile); |
| 9709 | Py_BEGIN_ALLOW_THREADS |
| 9710 | fd = openat(dirfd, file, flag, mode); |
| 9711 | Py_END_ALLOW_THREADS |
| 9712 | Py_DECREF(ofile); |
| 9713 | if (fd < 0) |
| 9714 | return posix_error(); |
| 9715 | return PyLong_FromLong((long)fd); |
| 9716 | } |
| 9717 | #endif |
| 9718 | |
| 9719 | #ifdef HAVE_READLINKAT |
| 9720 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 9721 | "readlinkat(dirfd, path) -> path\n\n\ |
| 9722 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9723 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9724 | is interpreted relative to the current working directory."); |
| 9725 | |
| 9726 | static PyObject * |
| 9727 | posix_readlinkat(PyObject *self, PyObject *args) |
| 9728 | { |
| 9729 | PyObject *v, *opath; |
| 9730 | char buf[MAXPATHLEN]; |
| 9731 | char *path; |
| 9732 | int n, dirfd; |
| 9733 | int arg_is_unicode = 0; |
| 9734 | |
| 9735 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 9736 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 9737 | return NULL; |
| 9738 | path = PyBytes_AsString(opath); |
| 9739 | v = PySequence_GetItem(args, 1); |
| 9740 | if (v == NULL) { |
| 9741 | Py_DECREF(opath); |
| 9742 | return NULL; |
| 9743 | } |
| 9744 | |
| 9745 | if (PyUnicode_Check(v)) { |
| 9746 | arg_is_unicode = 1; |
| 9747 | } |
| 9748 | Py_DECREF(v); |
| 9749 | |
| 9750 | Py_BEGIN_ALLOW_THREADS |
| 9751 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 9752 | Py_END_ALLOW_THREADS |
| 9753 | Py_DECREF(opath); |
| 9754 | if (n < 0) |
| 9755 | return posix_error(); |
| 9756 | |
| 9757 | if (arg_is_unicode) |
| 9758 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 9759 | else |
| 9760 | return PyBytes_FromStringAndSize(buf, n); |
| 9761 | } |
| 9762 | #endif /* HAVE_READLINKAT */ |
| 9763 | |
| 9764 | #ifdef HAVE_RENAMEAT |
| 9765 | PyDoc_STRVAR(posix_renameat__doc__, |
| 9766 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 9767 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 9768 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 9769 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 9770 | oldpath is interpreted relative to the current working directory. This\n\ |
| 9771 | also applies for newpath."); |
| 9772 | |
| 9773 | static PyObject * |
| 9774 | posix_renameat(PyObject *self, PyObject *args) |
| 9775 | { |
| 9776 | int res; |
| 9777 | PyObject *opathold, *opathnew; |
| 9778 | char *opath, *npath; |
| 9779 | int oldfd, newfd; |
| 9780 | |
| 9781 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 9782 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 9783 | return NULL; |
| 9784 | opath = PyBytes_AsString(opathold); |
| 9785 | npath = PyBytes_AsString(opathnew); |
| 9786 | Py_BEGIN_ALLOW_THREADS |
| 9787 | res = renameat(oldfd, opath, newfd, npath); |
| 9788 | Py_END_ALLOW_THREADS |
| 9789 | Py_DECREF(opathold); |
| 9790 | Py_DECREF(opathnew); |
| 9791 | if (res < 0) |
| 9792 | return posix_error(); |
| 9793 | Py_RETURN_NONE; |
| 9794 | } |
| 9795 | #endif |
| 9796 | |
| 9797 | #if HAVE_SYMLINKAT |
| 9798 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 9799 | "symlinkat(src, dstfd, dst)\n\n\ |
| 9800 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 9801 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 9802 | is interpreted relative to the current working directory."); |
| 9803 | |
| 9804 | static PyObject * |
| 9805 | posix_symlinkat(PyObject *self, PyObject *args) |
| 9806 | { |
| 9807 | int res, dstfd; |
| 9808 | PyObject *osrc, *odst; |
| 9809 | char *src, *dst; |
| 9810 | |
| 9811 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 9812 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 9813 | return NULL; |
| 9814 | src = PyBytes_AsString(osrc); |
| 9815 | dst = PyBytes_AsString(odst); |
| 9816 | Py_BEGIN_ALLOW_THREADS |
| 9817 | res = symlinkat(src, dstfd, dst); |
| 9818 | Py_END_ALLOW_THREADS |
| 9819 | Py_DECREF(osrc); |
| 9820 | Py_DECREF(odst); |
| 9821 | if (res < 0) |
| 9822 | return posix_error(); |
| 9823 | Py_RETURN_NONE; |
| 9824 | } |
| 9825 | #endif /* HAVE_SYMLINKAT */ |
| 9826 | |
| 9827 | #ifdef HAVE_UNLINKAT |
| 9828 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 9829 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 9830 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9831 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 9832 | specified, unlinkat() behaves like rmdir().\n\ |
| 9833 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9834 | is interpreted relative to the current working directory."); |
| 9835 | |
| 9836 | static PyObject * |
| 9837 | posix_unlinkat(PyObject *self, PyObject *args) |
| 9838 | { |
| 9839 | int dirfd, res, flags = 0; |
| 9840 | PyObject *opath; |
| 9841 | char *path; |
| 9842 | |
| 9843 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 9844 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9845 | return NULL; |
| 9846 | path = PyBytes_AsString(opath); |
| 9847 | Py_BEGIN_ALLOW_THREADS |
| 9848 | res = unlinkat(dirfd, path, flags); |
| 9849 | Py_END_ALLOW_THREADS |
| 9850 | Py_DECREF(opath); |
| 9851 | if (res < 0) |
| 9852 | return posix_error(); |
| 9853 | Py_RETURN_NONE; |
| 9854 | } |
| 9855 | #endif |
| 9856 | |
| 9857 | #ifdef HAVE_UTIMENSAT |
| 9858 | PyDoc_STRVAR(posix_utimensat__doc__, |
| 9859 | "utimensat(dirfd, path, (atime_sec, atime_nsec),\n\ |
| 9860 | (mtime_sec, mtime_nsec), flags)\n\ |
| 9861 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 9862 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 9863 | relative, it is taken as relative to dirfd.\n\ |
| 9864 | The second form sets atime and mtime to the current time.\n\ |
| 9865 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9866 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9867 | is interpreted relative to the current working directory.\n\ |
| 9868 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 9869 | current time.\n\ |
| 9870 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 9871 | |
| 9872 | static PyObject * |
| 9873 | posix_utimensat(PyObject *self, PyObject *args) |
| 9874 | { |
| 9875 | PyObject *opath; |
| 9876 | char *path; |
| 9877 | int res, dirfd, flags = 0; |
| 9878 | PyObject *atime, *mtime; |
| 9879 | |
| 9880 | struct timespec buf[2]; |
| 9881 | |
| 9882 | if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat", |
| 9883 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 9884 | return NULL; |
| 9885 | path = PyBytes_AsString(opath); |
| 9886 | if (atime == Py_None && mtime == Py_None) { |
| 9887 | /* optional time values not given */ |
| 9888 | Py_BEGIN_ALLOW_THREADS |
| 9889 | res = utimensat(dirfd, path, NULL, flags); |
| 9890 | Py_END_ALLOW_THREADS |
| 9891 | } |
| 9892 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 9893 | PyErr_SetString(PyExc_TypeError, |
| 9894 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 9895 | Py_DECREF(opath); |
| 9896 | return NULL; |
| 9897 | } |
| 9898 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 9899 | PyErr_SetString(PyExc_TypeError, |
| 9900 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 9901 | Py_DECREF(opath); |
| 9902 | return NULL; |
| 9903 | } |
| 9904 | else { |
| 9905 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 9906 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 9907 | Py_DECREF(opath); |
| 9908 | return NULL; |
| 9909 | } |
| 9910 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 9911 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 9912 | Py_DECREF(opath); |
| 9913 | return NULL; |
| 9914 | } |
| 9915 | Py_BEGIN_ALLOW_THREADS |
| 9916 | res = utimensat(dirfd, path, buf, flags); |
| 9917 | Py_END_ALLOW_THREADS |
| 9918 | } |
| 9919 | Py_DECREF(opath); |
| 9920 | if (res < 0) { |
| 9921 | return posix_error(); |
| 9922 | } |
| 9923 | Py_RETURN_NONE; |
| 9924 | } |
| 9925 | #endif |
| 9926 | |
| 9927 | #ifdef HAVE_MKFIFOAT |
| 9928 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 9929 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 9930 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9931 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9932 | is interpreted relative to the current working directory."); |
| 9933 | |
| 9934 | static PyObject * |
| 9935 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 9936 | { |
| 9937 | PyObject *opath; |
| 9938 | char *filename; |
| 9939 | int mode = 0666; |
| 9940 | int res, dirfd; |
| 9941 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 9942 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9943 | return NULL; |
| 9944 | filename = PyBytes_AS_STRING(opath); |
| 9945 | Py_BEGIN_ALLOW_THREADS |
| 9946 | res = mkfifoat(dirfd, filename, mode); |
| 9947 | Py_END_ALLOW_THREADS |
| 9948 | Py_DECREF(opath); |
| 9949 | if (res < 0) |
| 9950 | return posix_error(); |
| 9951 | Py_RETURN_NONE; |
| 9952 | } |
| 9953 | #endif |
| 9954 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9955 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9956 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9957 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9958 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9959 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9960 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 9961 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9962 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 9963 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9964 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9965 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9966 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9967 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9968 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9969 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9970 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9971 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9972 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9973 | #endif /* HAVE_LCHMOD */ |
| 9974 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9975 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 9976 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 9977 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9978 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 9979 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 9980 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9981 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 9982 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 9983 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9984 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 9985 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9986 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9987 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9988 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 9989 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9990 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 9991 | METH_NOARGS, posix_getcwd__doc__}, |
| 9992 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 9993 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 9994 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9995 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9996 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9997 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9998 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 9999 | #ifdef HAVE_FDOPENDIR |
| 10000 | {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__}, |
| 10001 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10002 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 10003 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10004 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10005 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10006 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10007 | #ifdef HAVE_GETPRIORITY |
| 10008 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10009 | #endif /* HAVE_GETPRIORITY */ |
| 10010 | #ifdef HAVE_SETPRIORITY |
| 10011 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10012 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10013 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10014 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10015 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10016 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10017 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10018 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10019 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 10020 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 10021 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10022 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10023 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10024 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10025 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10026 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10027 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10028 | win_symlink__doc__}, |
| 10029 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10030 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10031 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10032 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10033 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10034 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10035 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10036 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10037 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 10038 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 10039 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10040 | #ifdef HAVE_FUTIMES |
| 10041 | {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__}, |
| 10042 | #endif |
| 10043 | #ifdef HAVE_LUTIMES |
| 10044 | {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__}, |
| 10045 | #endif |
| 10046 | #ifdef HAVE_FUTIMENS |
| 10047 | {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__}, |
| 10048 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10049 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10050 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10051 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10052 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10053 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10054 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 10055 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10056 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10057 | #ifdef HAVE_FEXECVE |
| 10058 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 10059 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10060 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10061 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10062 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10063 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10064 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10065 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10066 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10067 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10068 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10069 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10070 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10071 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10072 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10073 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10074 | #ifdef HAVE_SCHED_H |
| 10075 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10076 | {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10077 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10078 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10079 | #endif |
| 10080 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10081 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10082 | #endif |
| 10083 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10084 | {"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] | 10085 | #endif |
| 10086 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10087 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10088 | #endif |
| 10089 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10090 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10091 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10092 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10093 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10094 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10095 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10096 | #endif |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10097 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10098 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10099 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10100 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10101 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10102 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10103 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10104 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10105 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10106 | #endif /* HAVE_GETEGID */ |
| 10107 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10108 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10109 | #endif /* HAVE_GETEUID */ |
| 10110 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10111 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10112 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10113 | #ifdef HAVE_GETGROUPLIST |
| 10114 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10115 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10116 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10117 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10118 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10119 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10120 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10121 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10122 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10123 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10124 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10125 | #endif /* HAVE_GETPPID */ |
| 10126 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10127 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10128 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10129 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10130 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10131 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10132 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10133 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10134 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10135 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10136 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10137 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10138 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10139 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10140 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10141 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10142 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10143 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 10144 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10145 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10146 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10147 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10148 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10149 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10150 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10151 | #endif /* HAVE_SETEUID */ |
| 10152 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10153 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10154 | #endif /* HAVE_SETEGID */ |
| 10155 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10156 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10157 | #endif /* HAVE_SETREUID */ |
| 10158 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10159 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10160 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10161 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10162 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10163 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10164 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10165 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10166 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10167 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10168 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10169 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10170 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10171 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10172 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10173 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10174 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10175 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10176 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10177 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10178 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10179 | #ifdef HAVE_WAIT3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10180 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10181 | #endif /* HAVE_WAIT3 */ |
| 10182 | #ifdef HAVE_WAIT4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10183 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10184 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10185 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10186 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10187 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10188 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10189 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10190 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10191 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10192 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10193 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10194 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10195 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10196 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10197 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10198 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10199 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10200 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10201 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10202 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10203 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10204 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10205 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10206 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 10207 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10208 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10209 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10210 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10211 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10212 | #ifdef HAVE_LOCKF |
| 10213 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10214 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10215 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10216 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10217 | #ifdef HAVE_READV |
| 10218 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10219 | #endif |
| 10220 | #ifdef HAVE_PREAD |
| 10221 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10222 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10223 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10224 | #ifdef HAVE_WRITEV |
| 10225 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10226 | #endif |
| 10227 | #ifdef HAVE_PWRITE |
| 10228 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10229 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10230 | #ifdef HAVE_SENDFILE |
| 10231 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10232 | posix_sendfile__doc__}, |
| 10233 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10234 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 10235 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10236 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10237 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10238 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10239 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10240 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10241 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10242 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10243 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10244 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10245 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10246 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10247 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10248 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10249 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10250 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10251 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10252 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10253 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10254 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10255 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10256 | #ifdef HAVE_TRUNCATE |
| 10257 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 10258 | #endif |
| 10259 | #ifdef HAVE_POSIX_FALLOCATE |
| 10260 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10261 | #endif |
| 10262 | #ifdef HAVE_POSIX_FADVISE |
| 10263 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10264 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10265 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10266 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10267 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10268 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10269 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10270 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10271 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10272 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10273 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10274 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10275 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10276 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10277 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10278 | #ifdef HAVE_SYNC |
| 10279 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10280 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10281 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10282 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10283 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10284 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10285 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10286 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10287 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10288 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10289 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10290 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10291 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10292 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10293 | #endif /* WIFSTOPPED */ |
| 10294 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10295 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10296 | #endif /* WIFSIGNALED */ |
| 10297 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10298 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10299 | #endif /* WIFEXITED */ |
| 10300 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10301 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10302 | #endif /* WEXITSTATUS */ |
| 10303 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10304 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10305 | #endif /* WTERMSIG */ |
| 10306 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10307 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10308 | #endif /* WSTOPSIG */ |
| 10309 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10310 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10311 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10312 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10313 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10314 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10315 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10316 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10317 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10318 | #endif |
| 10319 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10320 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10321 | #endif |
| 10322 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10323 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10324 | #endif |
| 10325 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10326 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10327 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10328 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10329 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10330 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10331 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 10332 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10333 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10334 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10335 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10336 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10337 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10338 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10339 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10340 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10341 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10342 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10343 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10344 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10345 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10346 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10347 | #endif |
| 10348 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10349 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10350 | #endif |
| 10351 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10352 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10353 | #endif |
| 10354 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10355 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10356 | #endif |
| 10357 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10358 | /* posix *at family of functions */ |
| 10359 | #ifdef HAVE_FACCESSAT |
| 10360 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 10361 | #endif |
| 10362 | #ifdef HAVE_FCHMODAT |
| 10363 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 10364 | #endif /* HAVE_FCHMODAT */ |
| 10365 | #ifdef HAVE_FCHOWNAT |
| 10366 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 10367 | #endif /* HAVE_FCHOWNAT */ |
| 10368 | #ifdef HAVE_FSTATAT |
| 10369 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
| 10370 | #endif |
| 10371 | #ifdef HAVE_FUTIMESAT |
| 10372 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 10373 | #endif |
| 10374 | #ifdef HAVE_LINKAT |
| 10375 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 10376 | #endif /* HAVE_LINKAT */ |
| 10377 | #ifdef HAVE_MKDIRAT |
| 10378 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 10379 | #endif |
| 10380 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 10381 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 10382 | #endif |
| 10383 | #ifdef HAVE_OPENAT |
| 10384 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 10385 | #endif |
| 10386 | #ifdef HAVE_READLINKAT |
| 10387 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 10388 | #endif /* HAVE_READLINKAT */ |
| 10389 | #ifdef HAVE_RENAMEAT |
| 10390 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 10391 | #endif |
| 10392 | #if HAVE_SYMLINKAT |
| 10393 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 10394 | #endif /* HAVE_SYMLINKAT */ |
| 10395 | #ifdef HAVE_UNLINKAT |
| 10396 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 10397 | #endif |
| 10398 | #ifdef HAVE_UTIMENSAT |
| 10399 | {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__}, |
| 10400 | #endif |
| 10401 | #ifdef HAVE_MKFIFOAT |
| 10402 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 10403 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10404 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10405 | }; |
| 10406 | |
| 10407 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10408 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10409 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10410 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10411 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10412 | } |
| 10413 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10414 | #if defined(PYOS_OS2) |
| 10415 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10416 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10417 | { |
| 10418 | APIRET rc; |
| 10419 | ULONG values[QSV_MAX+1]; |
| 10420 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 10421 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10422 | |
| 10423 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 10424 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10425 | Py_END_ALLOW_THREADS |
| 10426 | |
| 10427 | if (rc != NO_ERROR) { |
| 10428 | os2_error(rc); |
| 10429 | return -1; |
| 10430 | } |
| 10431 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10432 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 10433 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 10434 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 10435 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 10436 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 10437 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 10438 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10439 | |
| 10440 | switch (values[QSV_VERSION_MINOR]) { |
| 10441 | case 0: ver = "2.00"; break; |
| 10442 | case 10: ver = "2.10"; break; |
| 10443 | case 11: ver = "2.11"; break; |
| 10444 | case 30: ver = "3.00"; break; |
| 10445 | case 40: ver = "4.00"; break; |
| 10446 | case 50: ver = "5.00"; break; |
| 10447 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 10448 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10449 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 10450 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10451 | ver = &tmp[0]; |
| 10452 | } |
| 10453 | |
| 10454 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10455 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10456 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10457 | |
| 10458 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 10459 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 10460 | tmp[1] = ':'; |
| 10461 | tmp[2] = '\0'; |
| 10462 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10463 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10464 | } |
| 10465 | #endif |
| 10466 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10467 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10468 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10469 | enable_symlink() |
| 10470 | { |
| 10471 | HANDLE tok; |
| 10472 | TOKEN_PRIVILEGES tok_priv; |
| 10473 | LUID luid; |
| 10474 | int meth_idx = 0; |
| 10475 | |
| 10476 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10477 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10478 | |
| 10479 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10480 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10481 | |
| 10482 | tok_priv.PrivilegeCount = 1; |
| 10483 | tok_priv.Privileges[0].Luid = luid; |
| 10484 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 10485 | |
| 10486 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 10487 | sizeof(TOKEN_PRIVILEGES), |
| 10488 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10489 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10490 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10491 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 10492 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10493 | } |
| 10494 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 10495 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10496 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 10497 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10498 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10499 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10500 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10501 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10502 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10503 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10504 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10505 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10506 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10507 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10508 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10509 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10510 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10511 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10512 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10513 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10514 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10515 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10516 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10517 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10518 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10519 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10520 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10521 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10522 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10523 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10524 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10525 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10526 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10527 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10528 | #endif |
| 10529 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10530 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10531 | #endif |
| 10532 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10533 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10534 | #endif |
| 10535 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10536 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10537 | #endif |
| 10538 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10539 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10540 | #endif |
| 10541 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10542 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10543 | #endif |
| 10544 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10545 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10546 | #endif |
| 10547 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10548 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10549 | #endif |
| 10550 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10551 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10552 | #endif |
| 10553 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10554 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10555 | #endif |
| 10556 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10557 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10558 | #endif |
| 10559 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10560 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10561 | #endif |
| 10562 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10563 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10564 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10565 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10566 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10567 | #endif |
| 10568 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10569 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 10570 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10571 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10572 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10573 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10574 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10575 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10576 | #endif |
| 10577 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10578 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 10579 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10580 | #ifdef PRIO_PROCESS |
| 10581 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 10582 | #endif |
| 10583 | #ifdef PRIO_PGRP |
| 10584 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 10585 | #endif |
| 10586 | #ifdef PRIO_USER |
| 10587 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 10588 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 10589 | #ifdef O_CLOEXEC |
| 10590 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 10591 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10592 | /* posix - constants for *at functions */ |
| 10593 | #ifdef AT_SYMLINK_NOFOLLOW |
| 10594 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 10595 | #endif |
| 10596 | #ifdef AT_EACCESS |
| 10597 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 10598 | #endif |
| 10599 | #ifdef AT_FDCWD |
| 10600 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 10601 | #endif |
| 10602 | #ifdef AT_REMOVEDIR |
| 10603 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 10604 | #endif |
| 10605 | #ifdef AT_SYMLINK_FOLLOW |
| 10606 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 10607 | #endif |
| 10608 | #ifdef UTIME_NOW |
| 10609 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 10610 | #endif |
| 10611 | #ifdef UTIME_OMIT |
| 10612 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 10613 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10614 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10615 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10616 | /* MS Windows */ |
| 10617 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10618 | /* Don't inherit in child processes. */ |
| 10619 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10620 | #endif |
| 10621 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10622 | /* Optimize for short life (keep in memory). */ |
| 10623 | /* MS forgot to define this one with a non-underscore form too. */ |
| 10624 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10625 | #endif |
| 10626 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10627 | /* Automatically delete when last handle is closed. */ |
| 10628 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10629 | #endif |
| 10630 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10631 | /* Optimize for random access. */ |
| 10632 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10633 | #endif |
| 10634 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10635 | /* Optimize for sequential access. */ |
| 10636 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10637 | #endif |
| 10638 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10639 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 10640 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10641 | /* Send a SIGIO signal whenever input or output |
| 10642 | becomes available on file descriptor */ |
| 10643 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 10644 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10645 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10646 | /* Direct disk access. */ |
| 10647 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10648 | #endif |
| 10649 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10650 | /* Must be a directory. */ |
| 10651 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10652 | #endif |
| 10653 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10654 | /* Do not follow links. */ |
| 10655 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 10656 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 10657 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10658 | /* Do not update the access time. */ |
| 10659 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 10660 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10661 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10662 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10663 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10664 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10665 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10666 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10667 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10668 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10669 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10670 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10671 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10672 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10673 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10674 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10675 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10676 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10677 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10678 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10679 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10680 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10681 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10682 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10683 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10684 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10685 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10686 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10687 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10688 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10689 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10690 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10691 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10692 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10693 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10694 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10695 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10696 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10697 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10698 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10699 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10700 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10701 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10702 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10703 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10704 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10705 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10706 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10707 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10708 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10709 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10710 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10711 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10712 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 10713 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 10714 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 10715 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 10716 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 10717 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 10718 | #endif /* ST_RDONLY */ |
| 10719 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 10720 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 10721 | #endif /* ST_NOSUID */ |
| 10722 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10723 | /* FreeBSD sendfile() constants */ |
| 10724 | #ifdef SF_NODISKIO |
| 10725 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 10726 | #endif |
| 10727 | #ifdef SF_MNOWAIT |
| 10728 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 10729 | #endif |
| 10730 | #ifdef SF_SYNC |
| 10731 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 10732 | #endif |
| 10733 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10734 | /* constants for posix_fadvise */ |
| 10735 | #ifdef POSIX_FADV_NORMAL |
| 10736 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 10737 | #endif |
| 10738 | #ifdef POSIX_FADV_SEQUENTIAL |
| 10739 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 10740 | #endif |
| 10741 | #ifdef POSIX_FADV_RANDOM |
| 10742 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 10743 | #endif |
| 10744 | #ifdef POSIX_FADV_NOREUSE |
| 10745 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 10746 | #endif |
| 10747 | #ifdef POSIX_FADV_WILLNEED |
| 10748 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 10749 | #endif |
| 10750 | #ifdef POSIX_FADV_DONTNEED |
| 10751 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 10752 | #endif |
| 10753 | |
| 10754 | /* constants for waitid */ |
| 10755 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 10756 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 10757 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 10758 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 10759 | #endif |
| 10760 | #ifdef WEXITED |
| 10761 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 10762 | #endif |
| 10763 | #ifdef WNOWAIT |
| 10764 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 10765 | #endif |
| 10766 | #ifdef WSTOPPED |
| 10767 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 10768 | #endif |
| 10769 | #ifdef CLD_EXITED |
| 10770 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 10771 | #endif |
| 10772 | #ifdef CLD_DUMPED |
| 10773 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 10774 | #endif |
| 10775 | #ifdef CLD_TRAPPED |
| 10776 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 10777 | #endif |
| 10778 | #ifdef CLD_CONTINUED |
| 10779 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 10780 | #endif |
| 10781 | |
| 10782 | /* constants for lockf */ |
| 10783 | #ifdef F_LOCK |
| 10784 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 10785 | #endif |
| 10786 | #ifdef F_TLOCK |
| 10787 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 10788 | #endif |
| 10789 | #ifdef F_ULOCK |
| 10790 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 10791 | #endif |
| 10792 | #ifdef F_TEST |
| 10793 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 10794 | #endif |
| 10795 | |
| 10796 | /* constants for futimens */ |
| 10797 | #ifdef UTIME_NOW |
| 10798 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 10799 | #endif |
| 10800 | #ifdef UTIME_OMIT |
| 10801 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 10802 | #endif |
| 10803 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 10804 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 10805 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10806 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 10807 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 10808 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 10809 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 10810 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 10811 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 10812 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 10813 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 10814 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 10815 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 10816 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 10817 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 10818 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 10819 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 10820 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 10821 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 10822 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 10823 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 10824 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 10825 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 10826 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10827 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 10828 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 10829 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 10830 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 10831 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 10832 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 10833 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 10834 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10835 | #ifdef HAVE_SCHED_H |
| 10836 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 10837 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 10838 | #ifdef SCHED_SPORADIC |
| 10839 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 10840 | #endif |
| 10841 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
| 10842 | #ifdef SCHED_BATCH |
| 10843 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 10844 | #endif |
| 10845 | #ifdef SCHED_IDLE |
| 10846 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 10847 | #endif |
| 10848 | #ifdef SCHED_RESET_ON_FORK |
| 10849 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 10850 | #endif |
| 10851 | #endif |
| 10852 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10853 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10854 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10855 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10856 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10857 | } |
| 10858 | |
| 10859 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10860 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10861 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 10862 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 10863 | |
| 10864 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10865 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 10866 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 10867 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 10868 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10869 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 10870 | #define MODNAME "posix" |
| 10871 | #endif |
| 10872 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10873 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10874 | PyModuleDef_HEAD_INIT, |
| 10875 | MODNAME, |
| 10876 | posix__doc__, |
| 10877 | -1, |
| 10878 | posix_methods, |
| 10879 | NULL, |
| 10880 | NULL, |
| 10881 | NULL, |
| 10882 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 10883 | }; |
| 10884 | |
| 10885 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 10886 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 10887 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10888 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10889 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10890 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10891 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10892 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10893 | #endif |
| 10894 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10895 | m = PyModule_Create(&posixmodule); |
| 10896 | if (m == NULL) |
| 10897 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10898 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10899 | /* Initialize environ dictionary */ |
| 10900 | v = convertenviron(); |
| 10901 | Py_XINCREF(v); |
| 10902 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 10903 | return NULL; |
| 10904 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10905 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10906 | if (all_ins(m)) |
| 10907 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10908 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10909 | if (setup_confname_tables(m)) |
| 10910 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10912 | Py_INCREF(PyExc_OSError); |
| 10913 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 10914 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10915 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10916 | if (PyType_Ready(&cpu_set_type) < 0) |
| 10917 | return NULL; |
| 10918 | Py_INCREF(&cpu_set_type); |
| 10919 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10920 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10921 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 10922 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10923 | if (posix_putenv_garbage == NULL) |
| 10924 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 10925 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10926 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10927 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10928 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10929 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 10930 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 10931 | #endif |
| 10932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10933 | stat_result_desc.name = MODNAME ".stat_result"; |
| 10934 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 10935 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 10936 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 10937 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 10938 | structseq_new = StatResultType.tp_new; |
| 10939 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10940 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10941 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 10942 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 10943 | #ifdef NEED_TICKS_PER_SECOND |
| 10944 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10945 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 10946 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10947 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 10948 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10949 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 10950 | # endif |
| 10951 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10952 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 10953 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10954 | sched_param_desc.name = MODNAME ".sched_param"; |
| 10955 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 10956 | SchedParamType.tp_new = sched_param_new; |
| 10957 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10958 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10959 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10960 | Py_INCREF((PyObject*) &WaitidResultType); |
| 10961 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 10962 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10963 | Py_INCREF((PyObject*) &StatResultType); |
| 10964 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 10965 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 10966 | PyModule_AddObject(m, "statvfs_result", |
| 10967 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame^] | 10968 | |
| 10969 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10970 | Py_INCREF(&SchedParamType); |
| 10971 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame^] | 10972 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10973 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10974 | |
| 10975 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10976 | /* |
| 10977 | * Step 2 of weak-linking support on Mac OS X. |
| 10978 | * |
| 10979 | * The code below removes functions that are not available on the |
| 10980 | * currently active platform. |
| 10981 | * |
| 10982 | * This block allow one to use a python binary that was build on |
| 10983 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 10984 | * OSX 10.4. |
| 10985 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10986 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10987 | if (fstatvfs == NULL) { |
| 10988 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 10989 | return NULL; |
| 10990 | } |
| 10991 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10992 | #endif /* HAVE_FSTATVFS */ |
| 10993 | |
| 10994 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10995 | if (statvfs == NULL) { |
| 10996 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 10997 | return NULL; |
| 10998 | } |
| 10999 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11000 | #endif /* HAVE_STATVFS */ |
| 11001 | |
| 11002 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11003 | if (lchown == NULL) { |
| 11004 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11005 | return NULL; |
| 11006 | } |
| 11007 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11008 | #endif /* HAVE_LCHOWN */ |
| 11009 | |
| 11010 | |
| 11011 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11012 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11013 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11014 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11015 | |
| 11016 | #ifdef __cplusplus |
| 11017 | } |
| 11018 | #endif |